Skip to content

fix OOB on String.UTF8.encode using nullTerminated = true #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ export namespace String {
if (c1 < 128) {
store<u8>(bufOff, c1);
bufOff++;
// @ts-ignore: cast
if (nullTerminated & !c1) return bufOff - buf;
} else if (c1 < 2048) {
let b0 = c1 >> 6 | 192;
let b1 = c1 & 63 | 128;
Expand Down
12 changes: 11 additions & 1 deletion tests/compiler/std-wasi/crypto.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@
i32.const 1
i32.add
local.set $6
local.get $3
local.get $8
i32.eqz
i32.and
if
local.get $6
local.get $2
i32.sub
return
end
else
local.get $8
i32.const 2048
Expand Down Expand Up @@ -341,7 +351,7 @@
if
i32.const 32
i32.const 96
i32.const 739
i32.const 741
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down
14 changes: 12 additions & 2 deletions tests/compiler/std-wasi/process.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@
i32.const 1
i32.add
local.set $6
local.get $3
local.get $8
i32.eqz
i32.and
if
local.get $6
local.get $2
i32.sub
return
end
else
local.get $8
i32.const 2048
Expand Down Expand Up @@ -365,7 +375,7 @@
if
i32.const 112
i32.const 176
i32.const 739
i32.const 741
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down Expand Up @@ -7825,7 +7835,7 @@
if
i32.const 0
i32.const 176
i32.const 767
i32.const 769
i32.const 7
call $~lib/wasi/index/abort
unreachable
Expand Down
1,078 changes: 565 additions & 513 deletions tests/compiler/std/string-encoding.optimized.wat

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions tests/compiler/std/string-encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function testUTF8EncodeNullTerminated(): void {
assert(load<u8>(ptr, 8) == 0xAD);
assert(load<u8>(ptr, 9) == 0xA2);
assert(load<u8>(ptr, 10) == 0x00);
var str2 ="aaaaaaaaaaaaaaaaaaaaaaaaaaa\0";
var buf2 = String.UTF8.encode(str2, true);
assert(buf2.byteLength == String.UTF8.byteLength(str2, true));
}
testUTF8EncodeNullTerminated();

Expand Down
158 changes: 101 additions & 57 deletions tests/compiler/std/string-encoding.untouched.wat

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion tests/compiler/wasi/abort.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
i32.const 1
i32.add
local.set $6
local.get $3
local.get $8
i32.eqz
i32.and
if
local.get $6
local.get $2
i32.sub
return
end
else
local.get $8
i32.const 2048
Expand Down Expand Up @@ -210,7 +220,7 @@
if
i32.const 32
i32.const 96
i32.const 739
i32.const 741
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down
12 changes: 11 additions & 1 deletion tests/compiler/wasi/trace.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@
i32.const 1
i32.add
local.set $6
local.get $3
local.get $8
i32.eqz
i32.and
if
local.get $6
local.get $2
i32.sub
return
end
else
local.get $8
i32.const 2048
Expand Down Expand Up @@ -339,7 +349,7 @@
if
i32.const 80
i32.const 144
i32.const 739
i32.const 741
i32.const 49
call $~lib/wasi/index/abort
unreachable
Expand Down