Skip to content

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

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

alexfh
Copy link
Contributor

@alexfh alexfh commented Oct 8, 2024

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

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`
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2024

@llvm/pr-subscribers-clang

Author: Alexander Kornienko (alexfh)

Changes

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&lt;T[]&gt;::operator[](index): index out of range


Full diff: https://github.com/llvm/llvm-project/pull/111581.diff

2 Files Affected:

  • (modified) clang/lib/Frontend/TextDiagnostic.cpp (+3-3)
  • (added) clang/test/Frontend/highlight-text.c (+27)
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:       |              ^  ~~

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexfh alexfh merged commit 374fffe into llvm:main Oct 9, 2024
11 checks passed
@alexfh alexfh deleted the diagnostic-highlight-crash branch October 9, 2024 12:16
@ldionne
Copy link
Member

ldionne commented Oct 9, 2024

Amazing to see this paying off!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants