Skip to content

[Clang] prevent assertion failure when converting vectors to int/float with invalid expressions #105727

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 11 commits into from
Aug 29, 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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ Bug Fixes to C++ Support
- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)
- Correctly handle subexpressions of an immediate invocation in the presence of implicit casts. (#GH105558)
- Clang now correctly handles direct-list-initialization of a structured bindings from an array. (#GH31813)
- Fixed an assertion failure when converting vectors to int/float with invalid expressions. (#GH105486)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9888,6 +9888,9 @@ static ExprResult convertVector(Expr *E, QualType ElementType, Sema &S) {
/// IntTy without losing precision.
static bool canConvertIntToOtherIntTy(Sema &S, ExprResult *Int,
QualType OtherIntTy) {
if (Int->get()->containsErrors())
return false;

QualType IntTy = Int->get()->getType().getUnqualifiedType();

// Reject cases where the value of the Int is unknown as that would
Expand Down Expand Up @@ -9926,6 +9929,9 @@ static bool canConvertIntToOtherIntTy(Sema &S, ExprResult *Int,
/// FloatTy without losing precision.
static bool canConvertIntTyToFloatTy(Sema &S, ExprResult *Int,
QualType FloatTy) {
if (Int->get()->containsErrors())
return false;

QualType IntTy = Int->get()->getType().getUnqualifiedType();

// Determine if the integer constant can be expressed as a floating point
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,17 @@ void test_scoped_enum_vector(EnumClass ea, v2u v2ua) {
}
#endif
}

namespace GH105486 {
__attribute__((__vector_size__(sizeof(double)))) double a;
double b = a - (long)(*0); // expected-error {{indirection requires pointer operand ('int' invalid)}} \
// expected-error {{cannot initialize a variable of type 'double' with an rvalue of type '__attribute__((__vector_size__(1 * sizeof(double)))) double' (vector of 1 'double' value)}}

__attribute__((__vector_size__(sizeof(long)))) long c;
long d = c - (long)(*0); // expected-error {{indirection requires pointer operand ('int' invalid)}} \
// expected-error {{cannot initialize a variable of type 'long' with an rvalue of type '__attribute__((__vector_size__(1 * sizeof(long)))) long' (vector of 1 'long' value)}}

const long long e = *0; // expected-error {{indirection requires pointer operand ('int' invalid)}}
double f = a - e; // expected-error {{cannot initialize a variable of type 'double' with an rvalue of type '__attribute__((__vector_size__(1 * sizeof(double)))) double' (vector of 1 'double' value)}}
int h = c - e; // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type '__attribute__((__vector_size__(1 * sizeof(long)))) long' (vector of 1 'long' value)}}
}