Skip to content

Commit c1248c9

Browse files
authored
[Clang] prevent assertion failure when converting vectors to int/float with invalid expressions (#105727)
Fixes #105486
1 parent 26b0bef commit c1248c9

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ Bug Fixes to C++ Support
326326
- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)
327327
- Correctly handle subexpressions of an immediate invocation in the presence of implicit casts. (#GH105558)
328328
- Clang now correctly handles direct-list-initialization of a structured bindings from an array. (#GH31813)
329+
- Fixed an assertion failure when converting vectors to int/float with invalid expressions. (#GH105486)
329330

330331
Bug Fixes to AST Handling
331332
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9888,6 +9888,9 @@ static ExprResult convertVector(Expr *E, QualType ElementType, Sema &S) {
98889888
/// IntTy without losing precision.
98899889
static bool canConvertIntToOtherIntTy(Sema &S, ExprResult *Int,
98909890
QualType OtherIntTy) {
9891+
if (Int->get()->containsErrors())
9892+
return false;
9893+
98919894
QualType IntTy = Int->get()->getType().getUnqualifiedType();
98929895

98939896
// Reject cases where the value of the Int is unknown as that would
@@ -9926,6 +9929,9 @@ static bool canConvertIntToOtherIntTy(Sema &S, ExprResult *Int,
99269929
/// FloatTy without losing precision.
99279930
static bool canConvertIntTyToFloatTy(Sema &S, ExprResult *Int,
99289931
QualType FloatTy) {
9932+
if (Int->get()->containsErrors())
9933+
return false;
9934+
99299935
QualType IntTy = Int->get()->getType().getUnqualifiedType();
99309936

99319937
// Determine if the integer constant can be expressed as a floating point

clang/test/SemaCXX/vector.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,3 +772,17 @@ void test_scoped_enum_vector(EnumClass ea, v2u v2ua) {
772772
}
773773
#endif
774774
}
775+
776+
namespace GH105486 {
777+
__attribute__((__vector_size__(sizeof(double)))) double a;
778+
double b = a - (long)(*0); // expected-error {{indirection requires pointer operand ('int' invalid)}} \
779+
// 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)}}
780+
781+
__attribute__((__vector_size__(sizeof(long)))) long c;
782+
long d = c - (long)(*0); // expected-error {{indirection requires pointer operand ('int' invalid)}} \
783+
// 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)}}
784+
785+
const long long e = *0; // expected-error {{indirection requires pointer operand ('int' invalid)}}
786+
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)}}
787+
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)}}
788+
}

0 commit comments

Comments
 (0)