llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

<details>
<summary>Changes</summary>

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.

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


2 Files Affected:

- (modified) clang/include/clang/Frontend/CommandLineSourceLoc.h (+4-1) 
- (added) clang/test/CodeCompletion/crash-if-zero-index.cpp (+6) 


``````````diff
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.

``````````

</details>


https://github.com/llvm/llvm-project/pull/139457
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to