-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[clang] Enforce 1-based indexing for command line source locations #139457
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
base: main
Are you sure you want to change the base?
[clang] Enforce 1-based indexing for command line source locations #139457
Conversation
Fixes llvm#139375 Clang expects command line source locations to be provided using 1-based indexing. Currently, Clang does not reject zero as invalid argument for column or line number, which can cause Clang to crash. This commit extends validation in `ParsedSourceLocation::FromString` to only accept (unsinged) non-zero integers.
@llvm/pr-subscribers-clang-driver @llvm/pr-subscribers-clang Author: Naveen Seth Hanig (naveen-seth) ChangesFixes #139375 Clang expects command line source locations to be provided using 1-based indexing. This commit extends validation in Full diff: https://github.com/llvm/llvm-project/pull/139457.diff 2 Files Affected:
diff --git a/clang/include/clang/Frontend/CommandLineSourceLoc.h b/clang/include/clang/Frontend/CommandLineSourceLoc.h
index 074800a881a89..a412d41dbb023 100644
--- a/clang/include/clang/Frontend/CommandLineSourceLoc.h
+++ b/clang/include/clang/Frontend/CommandLineSourceLoc.h
@@ -24,7 +24,9 @@ namespace clang {
/// A source location that has been parsed on the command line.
struct ParsedSourceLocation {
std::string FileName;
+ // The 1-based line number
unsigned Line;
+ // The 1-based column number
unsigned Column;
public:
@@ -38,7 +40,8 @@ struct ParsedSourceLocation {
// If both tail splits were valid integers, return success.
if (!ColSplit.second.getAsInteger(10, PSL.Column) &&
- !LineSplit.second.getAsInteger(10, PSL.Line)) {
+ !LineSplit.second.getAsInteger(10, PSL.Line) &&
+ !(PSL.Column == 0 || PSL.Line == 0)) {
PSL.FileName = std::string(LineSplit.first);
// On the command-line, stdin may be specified via "-". Inside the
diff --git a/clang/test/CodeCompletion/crash-if-zero-index.cpp b/clang/test/CodeCompletion/crash-if-zero-index.cpp
new file mode 100644
index 0000000000000..2f0eae35738e6
--- /dev/null
+++ b/clang/test/CodeCompletion/crash-if-zero-index.cpp
@@ -0,0 +1,6 @@
+// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:0:1 %s -o -
+// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:1:0 %s -o -
+
+// Related to #139375
+// Clang uses 1-based indexing for source locations given from the command-line.
+// Verify Clang doesn’t crash when 0 is given as line or column number.
|
|
||
// Related to #139375 | ||
// Clang uses 1-based indexing for source locations given from the command-line. | ||
// Verify Clang doesn’t crash when 0 is given as line or column number. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test using FileCheck that we produce a diagnostic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
Thanks for working on that. |
Thank you for the quick review! :) I added the check in For example, the following would also crash (hitting the same assert as the crash this PR fixes) touch input.cpp
clang-refactor local-rename -selection=input.cpp:1:1-1:0 -new-name=test -v input.cpp Full crash log
(And similar invalid inputs like I could move the check up to the locations where What do you think is best here? |
Fixes #139375
Clang expects command line source locations to be provided using 1-based indexing.
Currently, Clang does not reject zero as invalid argument for column or line number, which can cause Clang to crash.
This commit extends validation in
ParsedSourceLocation::FromString
to only accept (unsinged) non-zero integers.