[clang] 374fffe - Fix out-of-bounds access to std::unique_ptr<T[]> (#111581)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 05:15:10 PDT 2024


Author: Alexander Kornienko
Date: 2024-10-09T14:15:06+02:00
New Revision: 374fffe015b48676fb3785167d363454a2f4dd1f

URL: https://github.com/llvm/llvm-project/commit/374fffe015b48676fb3785167d363454a2f4dd1f
DIFF: https://github.com/llvm/llvm-project/commit/374fffe015b48676fb3785167d363454a2f4dd1f.diff

LOG: Fix out-of-bounds access to std::unique_ptr<T[]> (#111581)

This manifested as an assertion failure in Clang built against libc++
with
hardening enabled (e.g.
-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG):
`libcxx/include/__memory/unique_ptr.h:596: assertion
__checker_.__in_bounds(std::__to_address(__ptr_), __i) failed:
unique_ptr<T[]>::operator[](index): index out of range`

Added: 
    clang/test/Frontend/highlight-text.c

Modified: 
    clang/lib/Frontend/TextDiagnostic.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index a264836a54398f..4119ce6048d45d 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1252,10 +1252,10 @@ highlightLines(StringRef FileData, unsigned StartLineNumber,
     for (unsigned I = 0; I <= Spelling.size(); ++I) {
       // This line is done.
       if (I == Spelling.size() || isVerticalWhitespace(Spelling[I])) {
-        SmallVector<TextDiagnostic::StyleRange> &LineRanges =
-            SnippetRanges[L - StartLineNumber];
-
         if (L >= StartLineNumber) {
+          SmallVector<TextDiagnostic::StyleRange> &LineRanges =
+              SnippetRanges[L - StartLineNumber];
+
           if (L == TokenStartLine) // First line
             appendStyle(LineRanges, T, StartCol, LineLength);
           else if (L == TokenEndLine) // Last line

diff  --git a/clang/test/Frontend/highlight-text.c b/clang/test/Frontend/highlight-text.c
new file mode 100644
index 00000000000000..a81d26caa4c24c
--- /dev/null
+++ b/clang/test/Frontend/highlight-text.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -fsyntax-only %s 2> %t
+// RUN: FileCheck < %t %s
+#define F (1 << 99)
+
+#define M \
+F | F
+
+int a = M;
+// CHECK: :8:9: warning: shift count >= width of type [-Wshift-count-overflow]
+// CHECK-NEXT:     8 | int a = M;
+// CHECK-NEXT:       |         ^
+// CHECK-NEXT: :5:11: note: expanded from macro 'M'
+// CHECK-NEXT:     5 | #define M \
+// CHECK-NEXT:       |           ^
+// CHECK-NEXT: :3:14: note: expanded from macro '\
+// CHECK-NEXT: F'
+// CHECK-NEXT:     3 | #define F (1 << 99)
+// CHECK-NEXT:       |              ^  ~~
+// CHECK-NEXT: :8:9: warning: shift count >= width of type [-Wshift-count-overflow]
+// CHECK-NEXT:     8 | int a = M;
+// CHECK-NEXT:       |         ^
+// CHECK-NEXT: :6:5: note: expanded from macro 'M'
+// CHECK-NEXT:     6 | F | F
+// CHECK-NEXT:       |     ^
+// CHECK-NEXT: :3:14: note: expanded from macro 'F'
+// CHECK-NEXT:     3 | #define F (1 << 99)
+// CHECK-NEXT:       |              ^  ~~


        


More information about the cfe-commits mailing list