Skip to content

Commit 30e0165

Browse files
committed
[clang-format] Fix an off-by-1 bug with -length option
Also validate the argument value. Fixes #56245
1 parent 897ccdd commit 30e0165

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

clang/test/Format/multiple-inputs-error.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: cp %s %t-1.cpp
22
// RUN: cp %s %t-2.cpp
3-
// RUN: not clang-format 2>&1 >/dev/null -offset=1 -length=0 %t-1.cpp %t-2.cpp |FileCheck %s
3+
// RUN: not clang-format 2>&1 >/dev/null -offset=1 -length=1 %t-1.cpp %t-2.cpp |FileCheck %s
44
// RUN: not clang-format 2>&1 >/dev/null -lines=1:1 %t-1.cpp %t-2.cpp |FileCheck %s -check-prefix=CHECK-LINE
55
// CHECK: error: -offset, -length and -lines can only be used for single file.
66
// CHECK-LINE: error: -offset, -length and -lines can only be used for single file.

clang/test/Format/ranges.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: grep -Ev "// *[A-Z-]+:" %s \
2-
// RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \
1+
// RUN: grep -Ev "// *[A-Z0-9-]+:" %s \
2+
// RUN: | clang-format -style=LLVM -offset=2 -length=1 -offset=28 -length=1 -offset=35 -length=8 \
33
// RUN: | FileCheck -strict-whitespace %s
44
// CHECK: {{^int\ \*i;$}}
55
int*i;
@@ -9,3 +9,12 @@ int * i;
99

1010
// CHECK: {{^int\ \*i;$}}
1111
int * i;
12+
13+
// CHECK: int I;
14+
// CHECK-NEXT: int J ;
15+
int I ;
16+
int J ;
17+
18+
// RUN: not clang-format -length=0 < %s 2>&1 \
19+
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK0 %s
20+
// CHECK0: error: length should be at least 1

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static bool fillRanges(MemoryBuffer *Code,
283283
Lengths.push_back(Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) -
284284
Offsets[0]);
285285
} else if (Offsets.size() != Lengths.size()) {
286-
errs() << "error: number of -offset and -length arguments must match.\n";
286+
errs() << "error: number of -offset and -length arguments must match\n";
287287
return true;
288288
}
289289
for (unsigned I = 0, E = Offsets.size(); I < E; ++I) {
@@ -293,12 +293,16 @@ static bool fillRanges(MemoryBuffer *Code,
293293
return true;
294294
}
295295
const auto Length = Lengths[I];
296+
if (Length == 0) {
297+
errs() << "error: length should be at least 1\n";
298+
return true;
299+
}
296300
if (Offset + Length > Code->getBufferSize()) {
297301
errs() << "error: invalid length " << Length << ", offset + length ("
298-
<< Offset + Length << ") is outside the file.\n";
302+
<< Offset + Length << ") is outside the file\n";
299303
return true;
300304
}
301-
Ranges.push_back(tooling::Range(Offset, Length));
305+
Ranges.push_back(tooling::Range(Offset, Length - 1));
302306
}
303307
return false;
304308
}

0 commit comments

Comments
 (0)