From ef206446f2bbcb1bacc73d7611a96c457f59499f Mon Sep 17 00:00:00 2001 From: Youngsuk Kim Date: Fri, 22 Nov 2024 04:51:09 -0500 Subject: [PATCH] [clang] Warn const integer-overflow of member in temporary struct bound to rvalue reference (#117225) Fixes #46755 --------- Co-authored-by: Sirraide --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Sema/SemaChecking.cpp | 3 ++- clang/test/SemaCXX/integer-overflow.cpp | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4847437ef1f8bde..54145b28154eb45 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -580,6 +580,9 @@ Improvements to Clang's diagnostics - Improved error recovery for function call arguments with trailing commas (#GH100921). +- For an rvalue reference bound to a temporary struct with an integer member, Clang will detect constant integer overflow + in the initializer for the integer member (#GH46755). + Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 2fd990750ed212a..a49605e48676516 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -12048,7 +12048,8 @@ void Sema::CheckForIntOverflow (const Expr *E) { New && New->isArray()) { if (auto ArraySize = New->getArraySize()) Exprs.push_back(*ArraySize); - } + } else if (const auto *MTE = dyn_cast(OriginalE)) + Exprs.push_back(MTE->getSubExpr()); } while (!Exprs.empty()); } diff --git a/clang/test/SemaCXX/integer-overflow.cpp b/clang/test/SemaCXX/integer-overflow.cpp index d1cc8bee566f6b0..73a4e88ee6c098a 100644 --- a/clang/test/SemaCXX/integer-overflow.cpp +++ b/clang/test/SemaCXX/integer-overflow.cpp @@ -246,4 +246,10 @@ int m() { return 0; } } + +namespace GH46755 { +void f() { + struct { int v; } &&r = {512 * 1024 * 1024 * 1024}; // expected-warning {{overflow in expression; result is 0 with type 'int'}} +} +} #endif