Skip to content

Commit 53a650f

Browse files
committed
Don't allow invalid Unicode scalar values in char
1 parent 983ec57 commit 53a650f

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
* Make .wasm output deterministic when using `--reference-types`.
3232
[#3851](https://github.com/rustwasm/wasm-bindgen/pull/3851)
3333

34+
* Don't allow invalid Unicode scalar values in `char`.
35+
[#3866](https://github.com/rustwasm/wasm-bindgen/pull/3866)
36+
3437
--------------------------------------------------------------------------------
3538

3639
## [0.2.91](https://github.com/rustwasm/wasm-bindgen/compare/0.2.90...0.2.91)

crates/cli-support/src/js/binding.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ impl<'a, 'b> JsBuilder<'a, 'b> {
502502
self.prelude(&format!("_assertNonNull({});", arg));
503503
}
504504

505+
fn assert_char(&mut self, arg: &str) {
506+
self.cx.expose_assert_char();
507+
self.prelude(&format!("_assertChar({});", arg));
508+
}
509+
505510
fn assert_optional_bigint(&mut self, arg: &str) {
506511
if !self.cx.config.debug {
507512
return;
@@ -739,7 +744,11 @@ fn instruction(
739744

740745
Instruction::I32FromStringFirstChar => {
741746
let val = js.pop();
742-
js.push(format!("{}.codePointAt(0)", val));
747+
let i = js.tmp();
748+
js.prelude(&format!("const char{i} = {val}.codePointAt(0);"));
749+
let val = format!("char{i}");
750+
js.assert_char(&val);
751+
js.push(val);
743752
}
744753

745754
Instruction::I32FromExternrefOwned => {
@@ -816,11 +825,18 @@ fn instruction(
816825

817826
Instruction::I32FromOptionChar => {
818827
let val = js.pop();
828+
let i = js.tmp();
819829
js.cx.expose_is_like_none();
820-
js.push(format!(
821-
"isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0)",
830+
js.prelude(&format!(
831+
"const char{i} = isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0);",
822832
val
823833
));
834+
let val = format!("char{i}");
835+
js.cx.expose_assert_char();
836+
js.prelude(&format!(
837+
"if ({val} !== 0xFFFFFF) {{ _assertChar({val}); }}"
838+
));
839+
js.push(val);
824840
}
825841

826842
Instruction::I32FromOptionEnum { hole } => {

crates/cli-support/src/js/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,19 @@ impl<'a> Context<'a> {
21162116
);
21172117
}
21182118

2119+
fn expose_assert_char(&mut self) {
2120+
if !self.should_write_global("assert_char") {
2121+
return;
2122+
}
2123+
self.global(
2124+
"
2125+
function _assertChar(c) {
2126+
if (typeof(c) !== 'number' || c >= 0x110000 || (c >= 0xD800 && c < 0xE000)) throw new Error(`expected a number argument that is a valid Unicode scalar value, found ${c}`);
2127+
}
2128+
",
2129+
);
2130+
}
2131+
21192132
fn expose_make_mut_closure(&mut self) -> Result<(), Error> {
21202133
if !self.should_write_global("make_mut_closure") {
21212134
return Ok(());

src/convert/impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ impl FromWasmAbi for char {
188188

189189
#[inline]
190190
unsafe fn from_abi(js: u32) -> char {
191+
// SAFETY: Checked in bindings.
191192
char::from_u32_unchecked(js)
192193
}
193194
}

tests/wasm/char.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const wasm = require('wasm-bindgen-test.js');
22
const assert = require('assert');
33

44
exports.js_identity = a => a;
5+
exports.js_option_identity = a => a;
56

67
exports.js_works = () => {
78
assert.strictEqual(wasm.letter(), 'a');
@@ -14,4 +15,7 @@ exports.js_works = () => {
1415
assert.strictEqual(wasm.rust_js_identity('㊻'), '㊻');
1516
wasm.rust_letter('a');
1617
wasm.rust_face('😀');
18+
19+
assert.throws(() => wasm.rust_js_identity('\uD83D'), /expected a number argument that is a valid Unicode scalar value, found 55357/);
20+
assert.throws(() => wasm.rust_js_option_identity('\uD83D'), /expected a number argument that is a valid Unicode scalar value, found 55357/);
1721
};

tests/wasm/char.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use wasm_bindgen_test::*;
44
#[wasm_bindgen(module = "tests/wasm/char.js")]
55
extern "C" {
66
fn js_identity(c: char) -> char;
7+
fn js_option_identity(c: Option<char>) -> Option<char>;
78
fn js_works();
89
}
910

@@ -17,6 +18,11 @@ pub fn rust_js_identity(c: char) -> char {
1718
js_identity(c)
1819
}
1920

21+
#[wasm_bindgen]
22+
pub fn rust_js_option_identity(c: Option<char>) -> Option<char> {
23+
js_option_identity(c)
24+
}
25+
2026
#[wasm_bindgen]
2127
pub fn letter() -> char {
2228
'a'

0 commit comments

Comments
 (0)