Skip to content

Commit 9e0121c

Browse files
authored
Fix undefined behavior in test_int53 (#22895)
The test uses numbers such as 0x8000000000000000ull but was converting to int64_t and overflowing, leading to UB and different behavior when using saturating conversions.
1 parent e6ed9d3 commit 9e0121c

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

test/core/test_int53.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ void writeI53ToI64_int64(int64_t *heapAddress, int64_t num) {
2121
#endif
2222
}
2323

24+
void writeI53ToI64_uint64(uint64_t *heapAddress, uint64_t num) {
25+
#ifdef GENERATE_ANSWERS
26+
*heapAddress = num;
27+
#else
28+
EM_ASM(writeI53ToI64($0, $1), heapAddress, (double)num);
29+
#endif
30+
}
31+
2432
void writeI53ToI64_double(int64_t *heapAddress, double num) {
2533
#ifdef GENERATE_ANSWERS
2634
if (num > 0 || num <= -9223372036854775808.0 /* underflow, garbage in-garbage out situation: just produce a value that matches current JS impl*/)
@@ -119,6 +127,14 @@ int64_t readI53FromU64_toInt64(uint64_t *heapAddress) {
119127
#endif
120128
}
121129

130+
uint64_t readI53FromU64_toUInt64(uint64_t *heapAddress) {
131+
#ifdef GENERATE_ANSWERS
132+
return (uint64_t)*heapAddress;
133+
#else
134+
return (uint64_t)EM_ASM_DOUBLE(return readI53FromU64($0), heapAddress);
135+
#endif
136+
}
137+
122138
double readI53FromU64(uint64_t *heapAddress) {
123139
#ifdef GENERATE_ANSWERS
124140
return (double)*heapAddress;
@@ -137,13 +153,13 @@ int64_t convertI32PairToI53(int32_t lo, int32_t hi) {
137153
#endif
138154
}
139155

140-
int64_t convertU32PairToI53(uint32_t lo, uint32_t hi) {
156+
uint64_t convertU32PairToI53(uint32_t lo, uint32_t hi) {
141157
#ifdef GENERATE_ANSWERS
142158
uint64_t val = (uint32_t)lo;
143159
val |= ((uint64_t)(uint32_t)hi) << 32;
144160
return val;
145161
#else
146-
return (int64_t)EM_ASM_DOUBLE(return convertU32PairToI53($0, $1), lo, hi);
162+
return (uint64_t)EM_ASM_DOUBLE(return convertU32PairToI53($0, $1), lo, hi);
147163
#endif
148164
}
149165

@@ -154,7 +170,7 @@ int64_t testconvertI32PairToI53(int64_t val) {
154170
return convertI32PairToI53(lo, hi);
155171
}
156172

157-
int64_t testconvertU32PairToI53(uint64_t val) {
173+
uint64_t testconvertU32PairToI53(uint64_t val) {
158174
uint32_t lo = (uint32_t)val;
159175
uint32_t hi = val >> 32;
160176
return convertU32PairToI53(lo, hi);
@@ -174,8 +190,8 @@ int64_t testReadWriteI64AsI53(int64_t num) {
174190

175191
uint64_t testReadWriteU64AsI53(uint64_t num) {
176192
uint64_t addr = 0;
177-
writeI53ToI64_int64((int64_t*)&addr, num);
178-
return readI53FromU64_toInt64(&addr);
193+
writeI53ToI64_uint64((uint64_t*)&addr, num);
194+
return readI53FromU64_toUInt64(&addr);
179195
}
180196

181197
int main() {

0 commit comments

Comments
 (0)