Skip to content

Commit 006c896

Browse files
authored
[SYCL] Fix checkValueRange (#18439)
The code previously cast to unsigned, assuming that checking the lower 32 bits would be sufficient for the overflow calculation. This is not true, because casting a `size_t` to `unsigned` could result in a very small number, e.g. `std::numeric_limits<unsigned>::max() + 1` casts to 0. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 8d8f695 commit 006c896

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

sycl/include/sycl/detail/id_queries_fit_in_int.hpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ checkValueRangeImpl(ValT V) {
5151
inline void checkMulOverflow(size_t a, size_t b) {
5252
#ifndef _MSC_VER
5353
int Product;
54-
// Since we must fit in SIGNED int, we can ignore the upper 32 bits.
55-
if (__builtin_mul_overflow(unsigned(a), unsigned(b), &Product)) {
54+
if (__builtin_mul_overflow(a, b, &Product)) {
5655
throw sycl::exception(make_error_code(errc::nd_range), Msg);
5756
}
5857
#else
@@ -66,9 +65,8 @@ inline void checkMulOverflow(size_t a, size_t b) {
6665
inline void checkMulOverflow(size_t a, size_t b, size_t c) {
6766
#ifndef _MSC_VER
6867
int Product;
69-
// Since we must fit in SIGNED int, we can ignore the upper 32 bits.
70-
if (__builtin_mul_overflow(unsigned(a), unsigned(b), &Product) ||
71-
__builtin_mul_overflow(Product, unsigned(c), &Product)) {
68+
if (__builtin_mul_overflow(a, b, &Product) ||
69+
__builtin_mul_overflow(Product, c, &Product)) {
7270
throw sycl::exception(make_error_code(errc::nd_range), Msg);
7371
}
7472
#else

0 commit comments

Comments
 (0)