]> granicus.if.org Git - clang/commitdiff
[lex] Provide a valid token when __has_include is found outside of a pp directive
authorBenjamin Kramer <benny.kra@googlemail.com>
Sun, 29 Mar 2015 19:05:27 +0000 (19:05 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sun, 29 Mar 2015 19:05:27 +0000 (19:05 +0000)
ExpandBuiltinMacro would strip the identifier and downstream users crash
when they encounter an identifier token with nullptr identifier info.

Found by afl-fuzz.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@233497 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/PPMacroExpansion.cpp
test/SemaCXX/typo-correction.cpp

index f2baddd0b8349f70d7fb6703c093ac76dfee0ff3..3ceba05401a1d49a7242198f386733cabfdb8c03 100644 (file)
@@ -1073,6 +1073,9 @@ static bool EvaluateHasIncludeCommon(Token &Tok,
   // These expressions are only allowed within a preprocessor directive.
   if (!PP.isParsingIfOrElifDirective()) {
     PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
+    // Return a valid identifier token.
+    assert(Tok.is(tok::identifier));
+    Tok.setIdentifierInfo(II);
     return false;
   }
 
index 324900150c7f27639ca73778abb7d5f2dab645e7..174b1403e2dbc5015b04774c6634b603f691a3c7 100644 (file)
@@ -634,3 +634,9 @@ namespace testArraySubscriptIndex {
     }
   };
 }
+
+namespace crash_has_include {
+int has_include(int); // expected-note {{'has_include' declared here}}
+// expected-error@+1 {{__has_include must be used within a preprocessing directive}}
+int foo = __has_include(42); // expected-error {{use of undeclared identifier '__has_include'; did you mean 'has_include'?}}
+}