Skip to content

Commit 3105cfe

Browse files
authored
[FileCheck] Fix parsing empty global and pseudo variable names (#83667)
Reland #82595 with fixes of build failures related to colored output. See https://lab.llvm.org/buildbot/#/builders/139/builds/60549 Use `%ProtectFileCheckOutput` to avoid colored output. Original commit message below. In `Pattern::parseVariable`, for global variables (those starting with '$') and for pseudo variables (those starting with '@') the first character is consumed before actual variable name parsing. If the name is empty, it leads to out-of-bound access to the corresponding `StringRef`. This patch adds an if statement against the case described.
1 parent 0e337c6 commit 3105cfe

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ Pattern::parseVariable(StringRef &Str, const SourceMgr &SM) {
297297
if (Str[0] == '$' || IsPseudo)
298298
++I;
299299

300+
if (I == Str.size())
301+
return ErrorDiagnostic::get(SM, Str.slice(I, StringRef::npos),
302+
StringRef("empty ") +
303+
(IsPseudo ? "pseudo " : "global ") +
304+
"variable name");
305+
300306
if (!isValidVarNameStart(Str[I++]))
301307
return ErrorDiagnostic::get(SM, Str, "invalid variable name");
302308

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
a
2+
3+
; RUN: %ProtectFileCheckOutput not FileCheck -input-file %s %s 2>&1 | \
4+
; RUN: FileCheck -check-prefix CHECK-ERROR -DDIR=%S \
5+
; RUN: --match-full-lines --strict-whitespace %s
6+
7+
; CHECK: a[[]]
8+
; CHECK-ERROR:[[DIR]]{{/|\\}}empty-variable-name.txt:7:13: error: empty variable name
9+
; CHECK-ERROR-NEXT:; CHECK: a{{\[\[\]\]}}
10+
; CHECK-ERROR-NEXT: ^
11+
12+
b
13+
14+
; RUN: %ProtectFileCheckOutput not FileCheck -input-file %s -check-prefix CHECK-PSEUDO %s 2>&1 | \
15+
; RUN: FileCheck -check-prefix CHECK-ERROR-PSEUDO -DDIR=%S \
16+
; RUN: --match-full-lines --strict-whitespace %s
17+
18+
; CHECK-PSEUDO: b[[@]]
19+
; CHECK-ERROR-PSEUDO:[[DIR]]{{/|\\}}empty-variable-name.txt:18:21: error: empty pseudo variable name
20+
; CHECK-ERROR-PSEUDO-NEXT:; CHECK-PSEUDO: b{{\[\[@\]\]}}
21+
; CHECK-ERROR-PSEUDO-NEXT: ^
22+
23+
c
24+
25+
; RUN: %ProtectFileCheckOutput not FileCheck -input-file %s -check-prefix CHECK-GLOBAL %s 2>&1 | \
26+
; RUN: FileCheck -check-prefix CHECK-ERROR-GLOBAL -DDIR=%S \
27+
; RUN: --match-full-lines --strict-whitespace %s
28+
29+
; CHECK-GLOBAL: c[[$]]
30+
; CHECK-ERROR-GLOBAL:[[DIR]]{{/|\\}}empty-variable-name.txt:29:21: error: empty global variable name
31+
; CHECK-ERROR-GLOBAL-NEXT:; CHECK-GLOBAL: c{{\[\[\$\]\]}}
32+
; CHECK-ERROR-GLOBAL-NEXT: ^

0 commit comments

Comments
 (0)