diff options
-rwxr-xr-x | src/embind/embind.js | 7 | ||||
-rwxr-xr-x | tests/embind/embind.test.js | 6 |
2 files changed, 12 insertions, 1 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js index f66b0495..f9b15fa3 100755 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -336,7 +336,12 @@ function __embind_register_std_string(rawType, name) { var ptr = _malloc(4 + length); HEAPU32[ptr >> 2] = length; for (var i = 0; i < length; ++i) { - HEAPU8[ptr + 4 + i] = value.charCodeAt(i); + var charCode = value.charCodeAt(i); + if (charCode > 255) { + _free(ptr); + throwBindingError('String has UTF-16 code units that do not fit in 8 bits'); + } + HEAPU8[ptr + 4 + i] = charCode; } destructors.push(_free, ptr); return ptr; diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 6f63d543..fec7145a 100755 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -397,6 +397,12 @@ module({ assert.equal(expected, cm.get_non_ascii_string()); }); + test("passing non-8-bit strings from JS to std::string throws", function() { + assert.throws(cm.BindingError, function() { + cm.emval_test_take_and_return_std_string("\u1234"); + }); + }); + test("non-ascii wstrings", function() { var expected = String.fromCharCode(10) + String.fromCharCode(1234) + |