summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2018-10-01 14:38:43 +0000
committerHaojian Wu <hokein@google.com>2018-10-01 14:38:43 +0000
commit3c5a31d0525ad2cf81e94ee5c38d45019362a272 (patch)
tree7ea3d224e10867a2aea94b60144dbe1acde068f3
parent039345a171c5e2d62ff58a6aa42586793f47339f (diff)
[Preprocessor] Fix a crash when handling non-alpha include header.
Summary: the crash is casued by an assertion in StringRef. (llvm::StringRef::front() const: Assertion `!empty()' failed.) Reviewers: sammccall Subscribers: jsji, cfe-commits Differential Revision: https://reviews.llvm.org/D52721 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@343481 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Lex/PPDirectives.cpp17
-rw-r--r--test/Preprocessor/include-nonalpha-no-crash.c3
2 files changed, 13 insertions, 7 deletions
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index 00397e1e1a..2e20a8b9db 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -1889,13 +1889,16 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
// characters
StringRef OriginalFilename = Filename;
if (!File) {
- while (!isAlphanumeric(Filename.front())) {
- Filename = Filename.drop_front();
- }
- while (!isAlphanumeric(Filename.back())) {
- Filename = Filename.drop_back();
- }
-
+ // A heuristic to correct a typo file name by removing leading and
+ // trailing non-isAlphanumeric characters.
+ auto CorrectTypoFilename = [](llvm::StringRef Filename) {
+ Filename = Filename.drop_until(isAlphanumeric);
+ while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
+ Filename = Filename.drop_back();
+ }
+ return Filename;
+ };
+ Filename = CorrectTypoFilename(Filename);
File = LookupFile(
FilenameLoc,
LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
diff --git a/test/Preprocessor/include-nonalpha-no-crash.c b/test/Preprocessor/include-nonalpha-no-crash.c
new file mode 100644
index 0000000000..31c1348f02
--- /dev/null
+++ b/test/Preprocessor/include-nonalpha-no-crash.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 %s -verify
+
+#include "./" // expected-error {{'./' file not found}}