Skip to content

Commit 09beb42

Browse files
authored
fix: Fix OOB on String.UTF8.encode when nullTerminated (#2173)
1 parent a962528 commit 09beb42

8 files changed

+716
-575
lines changed

std/assembly/string.ts

+2
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ export namespace String {
709709
if (c1 < 128) {
710710
store<u8>(bufOff, c1);
711711
bufOff++;
712+
// @ts-ignore: cast
713+
if (nullTerminated & !c1) return bufOff - buf;
712714
} else if (c1 < 2048) {
713715
let b0 = c1 >> 6 | 192;
714716
let b1 = c1 & 63 | 128;

tests/compiler/std-wasi/crypto.untouched.wat

+11-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@
208208
i32.const 1
209209
i32.add
210210
local.set $6
211+
local.get $3
212+
local.get $8
213+
i32.eqz
214+
i32.and
215+
if
216+
local.get $6
217+
local.get $2
218+
i32.sub
219+
return
220+
end
211221
else
212222
local.get $8
213223
i32.const 2048
@@ -341,7 +351,7 @@
341351
if
342352
i32.const 32
343353
i32.const 96
344-
i32.const 739
354+
i32.const 741
345355
i32.const 49
346356
call $~lib/wasi/index/abort
347357
unreachable

tests/compiler/std-wasi/process.untouched.wat

+12-2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@
232232
i32.const 1
233233
i32.add
234234
local.set $6
235+
local.get $3
236+
local.get $8
237+
i32.eqz
238+
i32.and
239+
if
240+
local.get $6
241+
local.get $2
242+
i32.sub
243+
return
244+
end
235245
else
236246
local.get $8
237247
i32.const 2048
@@ -365,7 +375,7 @@
365375
if
366376
i32.const 112
367377
i32.const 176
368-
i32.const 739
378+
i32.const 741
369379
i32.const 49
370380
call $~lib/wasi/index/abort
371381
unreachable
@@ -7825,7 +7835,7 @@
78257835
if
78267836
i32.const 0
78277837
i32.const 176
7828-
i32.const 767
7838+
i32.const 769
78297839
i32.const 7
78307840
call $~lib/wasi/index/abort
78317841
unreachable

tests/compiler/std/string-encoding.optimized.wat

+565-513
Large diffs are not rendered by default.

tests/compiler/std/string-encoding.ts

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ function testUTF8EncodeNullTerminated(): void {
8989
assert(load<u8>(ptr, 8) == 0xAD);
9090
assert(load<u8>(ptr, 9) == 0xA2);
9191
assert(load<u8>(ptr, 10) == 0x00);
92+
var str2 ="aaaaaaaaaaaaaaaaaaaaaaaaaaa\0";
93+
var buf2 = String.UTF8.encode(str2, true);
94+
assert(buf2.byteLength == String.UTF8.byteLength(str2, true));
9295
}
9396
testUTF8EncodeNullTerminated();
9497

tests/compiler/std/string-encoding.untouched.wat

+101-57
Large diffs are not rendered by default.

tests/compiler/wasi/abort.untouched.wat

+11-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@
7777
i32.const 1
7878
i32.add
7979
local.set $6
80+
local.get $3
81+
local.get $8
82+
i32.eqz
83+
i32.and
84+
if
85+
local.get $6
86+
local.get $2
87+
i32.sub
88+
return
89+
end
8090
else
8191
local.get $8
8292
i32.const 2048
@@ -210,7 +220,7 @@
210220
if
211221
i32.const 32
212222
i32.const 96
213-
i32.const 739
223+
i32.const 741
214224
i32.const 49
215225
call $~lib/wasi/index/abort
216226
unreachable

tests/compiler/wasi/trace.untouched.wat

+11-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@
206206
i32.const 1
207207
i32.add
208208
local.set $6
209+
local.get $3
210+
local.get $8
211+
i32.eqz
212+
i32.and
213+
if
214+
local.get $6
215+
local.get $2
216+
i32.sub
217+
return
218+
end
209219
else
210220
local.get $8
211221
i32.const 2048
@@ -339,7 +349,7 @@
339349
if
340350
i32.const 80
341351
i32.const 144
342-
i32.const 739
352+
i32.const 741
343353
i32.const 49
344354
call $~lib/wasi/index/abort
345355
unreachable

0 commit comments

Comments
 (0)