Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[clang][ExprConst] Check for array size of initlists #138673

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented May 6, 2025

Fixes #138653

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 6, 2025
@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Fixes #138653


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

1 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+5)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5950f461e4b2..46a87fd6c7e1c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11788,6 +11788,11 @@ bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
   LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
                           << NumEltsToInit << ".\n");
 
+  if (!Info.CheckArraySize(ExprToVisit->getExprLoc(),
+                           CAT->getNumAddressingBits(Info.Ctx), NumEltsToInit,
+                           /*Diag=*/true))
+    return false;
+
   Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
 
   // If the array was previously zero-initialized, preserve the

@tbaederr
Copy link
Contributor Author

tbaederr commented May 6, 2025

That fixes compile-time computation, but this example still breaks in codegen:

#include <array>
using namespace std;
constexpr size_t kMemoryChunk = 1024 * 8;
constexpr size_t kNumberOfIterations = 2000000;
constexpr size_t kThreadsNumber = 2 * kMemoryChunk;
struct S {};

int main() {
    array<S, kThreadsNumber * kNumberOfIterations> futures{};
}

here:

SmallVector<llvm::Constant *, 16> Elts;
if (fillC && fillC->isNullValue())
Elts.reserve(NumInitableElts + 1);
else
Elts.reserve(NumElements);

because NumElements is 32768000000.

Edit: Hmm, but that's only if S is empty.

@tbaederr
Copy link
Contributor Author

tbaederr commented May 6, 2025

I have not added the test since I thought it might take too long, but it's probably not a problem since we do the check before even touching the array...

If I could come up with a reproducer that doesn't require <future>...

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

It would be nice to also have tests to make sure we do emit the warning if the array is too large

@Sirraide
Copy link
Member

Sirraide commented May 7, 2025

I have not added the test since I thought it might take too long, but it's probably not a problem since we do the check before even touching the array...

Can’t you set the eval steps to like 100 or sth to avoid it taking too long or am I missing something here?

@tbaederr
Copy link
Contributor Author

tbaederr commented May 7, 2025

That comment is obsolete, I've added two test cases that work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
4 participants