Skip to content

[FileCheck] Fix parsing empty global and pseudo variable names #82595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 2, 2024

Conversation

kovdan01
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Feb 22, 2024

@llvm/pr-subscribers-testing-tools

Author: Daniil Kovalev (kovdan01)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/82595.diff

1 Files Affected:

  • (modified) llvm/lib/FileCheck/FileCheck.cpp (+6)
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 6d3a2b9cf46f7c..bb1855f599a1a3 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -297,6 +297,12 @@ Pattern::parseVariable(StringRef &Str, const SourceMgr &SM) {
   if (Str[0] == '$' || IsPseudo)
     ++I;
 
+  if (I == Str.size())
+    return ErrorDiagnostic::get(SM, Str,
+                                StringRef("empty ") +
+                                    (IsPseudo ? "pseudo " : "global ") +
+                                    "variable name");
+
   if (!isValidVarNameStart(Str[I++]))
     return ErrorDiagnostic::get(SM, Str, "invalid variable name");
 

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases?

@kovdan01
Copy link
Contributor Author

Thanks @jh7370, forgot to include test cases in the commit initially. See efd7bd8 for test cases

; CHECK-PSEUDO: b[[@]]
; CHECK-ERROR-PSEUDO:[[DIR]]/empty-variable-name.txt:18:20: error: empty pseudo variable name
; CHECK-ERROR-PSEUDO-NEXT:; CHECK-PSEUDO: b{{\[\[@\]\]}}
; CHECK-ERROR-PSEUDO-NEXT: ^
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks, see 6974c78. Also used Str.slice(I, StringRef::npos) to point to ] instead of @.

; CHECK-GLOBAL: c[[$]]
; CHECK-ERROR-GLOBAL:[[DIR]]/empty-variable-name.txt:29:20: error: empty global variable name
; CHECK-ERROR-GLOBAL-NEXT:; CHECK-GLOBAL: c{{\[\[\$\]\]}}
; CHECK-ERROR-GLOBAL-NEXT: ^
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks, see 6974c78. Also used Str.slice(I, StringRef::npos) to point to ] instead of $.

@kovdan01 kovdan01 requested review from RoboTux and jh7370 February 28, 2024 23:11
Copy link
Contributor

@RoboTux RoboTux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kovdan01 kovdan01 merged commit aab3d13 into llvm:main Mar 2, 2024
kovdan01 added a commit that referenced this pull request Mar 2, 2024
kovdan01 added a commit that referenced this pull request Mar 5, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants