Skip to content

Commit 4cb578c

Browse files
authored
Fix MIN_INT values when checking integer ranges (#22664)
Fixes: #19251
1 parent b36624a commit 4cb578c

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/runtime_debug.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ var MAX_UINT32 = (2 ** 32) - 1;
142142
var MAX_UINT53 = (2 ** 53) - 1;
143143
var MAX_UINT64 = (2 ** 64) - 1;
144144

145-
var MIN_INT8 = - (2 ** ( 8 - 1)) + 1;
146-
var MIN_INT16 = - (2 ** (16 - 1)) + 1;
147-
var MIN_INT32 = - (2 ** (32 - 1)) + 1;
148-
var MIN_INT53 = - (2 ** (53 - 1)) + 1;
149-
var MIN_INT64 = - (2 ** (64 - 1)) + 1;
145+
var MIN_INT8 = - (2 ** ( 8 - 1));
146+
var MIN_INT16 = - (2 ** (16 - 1));
147+
var MIN_INT32 = - (2 ** (32 - 1));
148+
var MIN_INT53 = - (2 ** (53 - 1));
149+
var MIN_INT64 = - (2 ** (64 - 1));
150150

151151
function checkInt(value, bits, min, max) {
152152
assert(Number.isInteger(Number(value)), `attempt to write non-integer (${value}) into integer heap`);

test/core/test_getValue_setValue.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// found in the LICENSE file.
55

66
#include <emscripten.h>
7+
#include <stdint.h>
78

89
int main() {
910
char buffer_char[8] = { 'x' };
@@ -30,6 +31,28 @@ int main() {
3031
ptr = Module['getValue']($1, '*');
3132
out('ptr: 0x' + ptr.toString(16) + ' ' + typeof(ptr));
3233
#endif
33-
}, buffer_char, buffer_ptr);
34+
35+
/* In ASSERTIONS=2 mode we check the limits of input values */
36+
var min = $2;
37+
var max = $3;
38+
setValue($1, max, 'i8');
39+
setValue($1, min, 'i8');
40+
#ifdef ASSERTIONS_2
41+
function checkAsserts(f, expected) {
42+
try {
43+
f();
44+
} catch(e) {
45+
assert(e.message.includes(expected), "expected assertion to include:" + expected);
46+
return;
47+
}
48+
assert(false, "expected assertion");
49+
}
50+
checkAsserts(() => setValue($1, max + 1, 'i8'), "value (256) too large to write as 8-bit value");
51+
checkAsserts(() => setValue($1, min - 1, 'i8'), "value (-129) too small to write as 8-bit value");
52+
#else
53+
setValue($1, max + 1, 'i8');
54+
setValue($1, min - 1, 'i8');
55+
#endif
56+
}, buffer_char, buffer_ptr, INT8_MIN, UINT8_MAX);
3457
}
3558

test/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7068,11 +7068,17 @@ def test(args=None, asserts=False):
70687068
# see that direct usage (not on module) works. we don't export, but the use
70697069
# keeps it alive through JSDCE
70707070
test(args=['-DDIRECT'])
7071+
7072+
# Test with ASSERTIONS=2 where we check the limits of value passed to setValue.
7073+
self.set_setting('ASSERTIONS', 2)
7074+
test(args=['-DDIRECT', '-DASSERTIONS_2'])
7075+
70717076
# see that with assertions, we get a nice error message
70727077
self.set_setting('EXPORTED_RUNTIME_METHODS', [])
70737078
self.set_setting('ASSERTIONS')
70747079
test(asserts=True)
70757080
self.set_setting('ASSERTIONS', 0)
7081+
70767082
# see that when we export them, things work on the module
70777083
self.set_setting('EXPORTED_RUNTIME_METHODS', ['getValue', 'setValue'])
70787084
test()

0 commit comments

Comments
 (0)