Skip to content

Commit 5f24643

Browse files
committed
[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.
1 parent 8e9da21 commit 5f24643

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clang/include/clang/Frontend/CommandLineSourceLoc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ namespace clang {
2424
/// A source location that has been parsed on the command line.
2525
struct ParsedSourceLocation {
2626
std::string FileName;
27+
// The 1-based line number
2728
unsigned Line;
29+
// The 1-based column number
2830
unsigned Column;
2931

3032
public:
@@ -38,7 +40,8 @@ struct ParsedSourceLocation {
3840

3941
// If both tail splits were valid integers, return success.
4042
if (!ColSplit.second.getAsInteger(10, PSL.Column) &&
41-
!LineSplit.second.getAsInteger(10, PSL.Line)) {
43+
!LineSplit.second.getAsInteger(10, PSL.Line) &&
44+
!(PSL.Column == 0 || PSL.Line == 0)) {
4245
PSL.FileName = std::string(LineSplit.first);
4346

4447
// On the command-line, stdin may be specified via "-". Inside the
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:0:1 %s -o -
2+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:1:0 %s -o -
3+
4+
// Related to #139375
5+
// Clang uses 1-based indexing for source locations given from the command-line.
6+
// Verify Clang doesn’t crash when 0 is given as line or column number.

0 commit comments

Comments
 (0)