Skip to content

Conversation

@tbaederr
Copy link
Contributor

... for dynamic memory allocation. This happens when the requested array size is too large.

Fixes #152951

... for dynamic memory allocation. This happens when the requested array size is too large. Fixes llvm#152951
@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 Sep 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

... for dynamic memory allocation. This happens when the requested array size is too large.

Fixes #152951


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.h (+3)
  • (modified) clang/test/AST/ByteCode/new-delete.cpp (+13)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 3bc1a67feeba2..72288ed97db21 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -3534,6 +3534,9 @@ inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc, if (!CheckDynamicMemoryAllocation(S, OpPC)) return false; + if (!ElementDesc) + return false; + SizeT NumElements = S.Stk.pop<SizeT>(); if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(), IsNoThrow)) { diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp index af747d7a15b12..f54854070573c 100644 --- a/clang/test/AST/ByteCode/new-delete.cpp +++ b/clang/test/AST/ByteCode/new-delete.cpp @@ -1091,6 +1091,19 @@ namespace NewNegSizeNothrow { static_assert(test_nothrow_neg_size(), "expected nullptr"); } // namespace NewNegSizeNothrow +#if __SIZEOF_SIZE_T == 8 +/// We can't allocate the array here as it is too big. +/// Make sure we're not crashing by assuming an non-null +/// Descriptor. +namespace HugeAllocation { + void *p; + void foo () + { + p = new char [256][256][256][256][256]; + } +} +#endif + #else /// Make sure we reject this prior to C++20 constexpr int a() { // both-error {{never produces a constant expression}} 
@tbaederr tbaederr merged commit d94a828 into llvm:main Sep 24, 2025
13 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
... for dynamic memory allocation. This happens when the requested array size is too large. Fixes llvm#152951
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

2 participants