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

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 8 13:27:37 PDT 2024


https://github.com/alexfh created https://github.com/llvm/llvm-project/pull/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`


>From ab7e28fb704cd3a2235cb366bc39891e50f83d03 Mon Sep 17 00:00:00 2001
From: Alexander Kornienko <alexfh at google.com>
Date: Tue, 8 Oct 2024 22:19:07 +0200
Subject: [PATCH] Fix out-of-bounds access to std::unique_ptr<T[]>

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`
---
 clang/lib/Frontend/TextDiagnostic.cpp |  6 +++---
 clang/test/Frontend/highlight-text.c  | 27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Frontend/highlight-text.c

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