Repository navigation
[clang] Enforce 1-based indexing for command line source locations - #139457
AaronBallman merged 8 commits into
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.
|
|
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? |
AaronBallman
left a comment
There was a problem hiding this comment.
Thank you for the fix! The change should come with a release note in clang/docs/ReleaseNotes.rst so users know about the improvement.
Otherwise, I think this is going in a reasonable direction.
38ca85e to
90a3beb
Compare
4184782 to
6b8ef56
Compare
4c7fca7 to
8f55b72
Compare
AaronBallman
left a comment
There was a problem hiding this comment.
Other than a nit, LGTM!
|
Thank you for reviewing! |
No need to apologize, that sort of thing comes up! :-) Do you need one of us to merge the changes on your behalf? |
|
Yes, please. |
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::FromStringto only accept (unsinged) non-zero integers.