Skip to content

[clang-format] Fix an off-by-1 bug with -length option #143302

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/test/Format/multiple-inputs-error.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: cp %s %t-1.cpp
// RUN: cp %s %t-2.cpp
// RUN: not clang-format 2>&1 >/dev/null -offset=1 -length=0 %t-1.cpp %t-2.cpp |FileCheck %s
// RUN: not clang-format 2>&1 >/dev/null -offset=1 -length=1 %t-1.cpp %t-2.cpp |FileCheck %s
// RUN: not clang-format 2>&1 >/dev/null -lines=1:1 %t-1.cpp %t-2.cpp |FileCheck %s -check-prefix=CHECK-LINE
// CHECK: error: -offset, -length and -lines can only be used for single file.
// CHECK-LINE: error: -offset, -length and -lines can only be used for single file.
Expand Down
11 changes: 10 additions & 1 deletion clang/test/Format/ranges.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: grep -Ev "// *[A-Z-]+:" %s \
// RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \
// RUN: | clang-format -style=LLVM -offset=2 -length=1 -offset=28 -length=1 -offset=35 -length=8 \
// RUN: | FileCheck -strict-whitespace %s
// CHECK: {{^int\ \*i;$}}
int*i;
Expand All @@ -9,3 +9,12 @@ int * i;

// CHECK: {{^int\ \*i;$}}
int * i;

// CHECK: int I;
// CHECK-NEXT: int J ;
int I ;
int J ;

// RUN: not clang-format -length=0 %s 2>&1 \
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK0 %s
// CHECK0: error: length should be at least 1
10 changes: 7 additions & 3 deletions clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ static bool fillRanges(MemoryBuffer *Code,
Lengths.push_back(Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) -
Offsets[0]);
} else if (Offsets.size() != Lengths.size()) {
errs() << "error: number of -offset and -length arguments must match.\n";
errs() << "error: number of -offset and -length arguments must match\n";
return true;
}
for (unsigned I = 0, E = Offsets.size(); I < E; ++I) {
Expand All @@ -293,12 +293,16 @@ static bool fillRanges(MemoryBuffer *Code,
return true;
}
const auto Length = Lengths[I];
if (Length == 0) {
errs() << "error: length should be at least 1\n";
return true;
}
if (Offset + Length > Code->getBufferSize()) {
errs() << "error: invalid length " << Length << ", offset + length ("
<< Offset + Length << ") is outside the file.\n";
<< Offset + Length << ") is outside the file\n";
return true;
}
Ranges.push_back(tooling::Range(Offset, Length));
Ranges.push_back(tooling::Range(Offset, Length - 1));
}
return false;
}
Expand Down