Skip to content

Conversation

@camc
Copy link
Contributor

@camc camc commented Oct 27, 2025

Fixes #165090

Make sure to reject invalid array pointer offsets in C.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Oct 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 27, 2025

@llvm/pr-subscribers-clang

Author: None (camc)

Changes

Fixes #165090

Make sure to reject invalid pointer offsets in C, when the pointer is to an array.


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.h (+1-1)
  • (modified) clang/test/AST/ByteCode/c.c (+6)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 89f6fbefb1907..24c50320b462e 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2281,7 +2281,7 @@ std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
     }
   }
 
-  if (Invalid && S.getLangOpts().CPlusPlus)
+  if (Invalid && (S.getLangOpts().CPlusPlus || Ptr.inArray()))
     return std::nullopt;
 
   // Offset is valid - compute it on unsigned.
diff --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index cfdc9d0d3dd86..3360d4f725b24 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -381,3 +381,9 @@ static char foo_(a) // all-warning {{definition without a prototype}}
 static void bar_(void) {
   foo_(foo_(1));
 }
+
+void foo2(void*);
+void bar2(void) {
+  int a[2][3][4][5]; // all-note {{array 'a' declared here}}
+  foo2(&a[0][4]); // all-warning {{array index 4 is past the end of the array}}
+}

@zwuis zwuis requested a review from tbaederr October 27, 2025 03:48
@camc
Copy link
Contributor Author

camc commented Oct 27, 2025

Could someone please merge this for me 😀

@tbaederr tbaederr merged commit b2da8ef into llvm:main Oct 27, 2025
14 checks passed
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
llvm#165186)

Fixes llvm#165090

Make sure to reject invalid array pointer offsets in C.

Co-authored-by: camc <pushy-crop-cartel@duck.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang][bytecode] crash when array index is past the end of array

3 participants