https://github.com/naveen-seth updated 
https://github.com/llvm/llvm-project/pull/139457

>From 5f246435b9784113ada3f82328433487391f40ab Mon Sep 17 00:00:00 2001
From: naveen-seth <naveen.ha...@outlook.com>
Date: Sun, 11 May 2025 14:24:00 +0000
Subject: [PATCH 1/2] [clang] Enforce 1-based indexing for command line source
 locations

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.
---
 clang/include/clang/Frontend/CommandLineSourceLoc.h | 5 ++++-
 clang/test/CodeCompletion/crash-if-zero-index.cpp   | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeCompletion/crash-if-zero-index.cpp

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.

>From 90a3bebcebf92e754ea8b8791ab104fb7db54316 Mon Sep 17 00:00:00 2001
From: naveen-seth <naveen.ha...@outlook.com>
Date: Tue, 13 May 2025 20:00:34 +0000
Subject: [PATCH 2/2] Test for diagnostic using FileCheck

---
 clang/test/CodeCompletion/crash-if-zero-index.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/clang/test/CodeCompletion/crash-if-zero-index.cpp 
b/clang/test/CodeCompletion/crash-if-zero-index.cpp
index 2f0eae35738e6..dec45f3048c9b 100644
--- a/clang/test/CodeCompletion/crash-if-zero-index.cpp
+++ b/clang/test/CodeCompletion/crash-if-zero-index.cpp
@@ -1,6 +1,10 @@
-// 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.
+// Verify that Clang rejects 0 as an invalid value for line or column number.
+
+// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:0:1 %s -o - 2>&1 \
+// RUN:     | FileCheck -check-prefix=CHECK-DIAG %s
+// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:1:0 %s -o - 2>&1 \
+// RUN:     | FileCheck -check-prefix=CHECK-DIAG %s
+
+// CHECK-DIAG: error: invalid value '{{.*}}' in '-code-completion-at={{.*}}'

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to