Skip to content

[Clang] Fix crash on invalid size in user-defined static_assert message #89420

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 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ Bug Fixes to C++ Support
- Fix a crash caused by defined struct in a type alias template when the structure
has fields with dependent type. Fixes (#GH75221).
- Fix the Itanium mangling of lambdas defined in a member of a local class (#GH88906)
- Fixed a crash when trying to evaluate a user-defined ``static_assert`` message whose ``size()``
function returns a large or negative value. Fixes (#GH89407).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def err_expr_not_cce : Error<
"call to 'size()'|call to 'data()'}0 is not a constant expression">;
def ext_cce_narrowing : ExtWarn<
"%select{case value|enumerator value|non-type template argument|"
"array size|explicit specifier argument|noexcept specifier argument}0 "
"%select{cannot be narrowed from type %2 to %3|"
"evaluates to %2, which cannot be narrowed to type %3}1">,
"array size|explicit specifier argument|noexcept specifier argument|"
"call to 'size()'|call to 'data()'}0 %select{cannot be narrowed from "
"type %2 to %3|evaluates to %2, which cannot be narrowed to type %3}1">,
InGroup<CXX11Narrowing>, DefaultError, SFINAEFailure;
def err_ice_not_integral : Error<
"%select{integer|integral}1 constant expression must have "
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16853,13 +16853,13 @@ bool Expr::EvaluateCharRangeAsString(std::string &Result,
if (!::EvaluateInteger(SizeExpression, SizeValue, Info))
return false;

int64_t Size = SizeValue.getExtValue();
uint64_t Size = SizeValue.getZExtValue();

if (!::EvaluatePointer(PtrExpression, String, Info))
return false;

QualType CharTy = PtrExpression->getType()->getPointeeType();
for (int64_t I = 0; I < Size; ++I) {
for (uint64_t I = 0; I < Size; ++I) {
APValue Char;
if (!handleLValueToRValueConversion(Info, PtrExpression, CharTy, String,
Char))
Expand Down
74 changes: 74 additions & 0 deletions clang/test/SemaCXX/static-assert-cxx26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,77 @@ struct Callable {
} data;
};
static_assert(false, Callable{}); // expected-error {{static assertion failed: hello}}

namespace GH89407 {
struct A {
constexpr __SIZE_TYPE__ size() const { return -1; }
constexpr const char* data() const { return ""; }
};

struct B {
constexpr long long size() const { return 18446744073709551615U; }
constexpr const char* data() const { return ""; }
};

struct C {
constexpr __int128 size() const { return -1; }
constexpr const char* data() const { return ""; }
};

struct D {
constexpr unsigned __int128 size() const { return -1; }
constexpr const char* data() const { return ""; }
};

struct E {
constexpr __SIZE_TYPE__ size() const { return 18446744073709551615U; }
constexpr const char* data() const { return ""; }
};

static_assert(true, A{}); // expected-error {{the message in this static assertion is not a constant expression}}
// expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
static_assert(true, B{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
// expected-error@-1 {{the message in this static assertion is not a constant expression}}
// expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
static_assert(true, C{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
// expected-error@-1 {{the message in this static assertion is not a constant expression}}
// expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
static_assert(true, D{}); // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type 'unsigned long'}}
// expected-error@-1 {{the message in this static assertion is not a constant expression}}
// expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
static_assert(true, E{}); // expected-error {{the message in this static assertion is not a constant expression}}
// expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}

static_assert(
false, // expected-error {{static assertion failed}}
A{} // expected-error {{the message in a static assertion must be produced by a constant expression}}
// expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
);

static_assert(
false, // expected-error {{static assertion failed}}
B{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
// expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
// expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
);

static_assert(
false, // expected-error {{static assertion failed}}
C{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
// expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
// expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
);

static_assert(
false, // expected-error {{static assertion failed}}
D{} // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type 'unsigned long'}}
// expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
// expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
);

static_assert(
false, // expected-error {{static assertion failed}}
E{} // expected-error {{the message in a static assertion must be produced by a constant expression}}
// expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
);
}