Skip to content

Commit

Permalink
Fix allocation of output string in emscripten_run_script_string() to …
Browse files Browse the repository at this point in the history
…not truncate the string if it has characters outside 7-bit ASCII set. Fixes emscripten-core#4766.
  • Loading branch information
juj committed Dec 4, 2016
1 parent 18c1463 commit fdc57b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3831,9 +3831,10 @@ LibraryManager.library = {
emscripten_run_script_string: function(ptr) {
{{{ makeEval("var s = eval(Pointer_stringify(ptr)) + '';") }}}
var me = _emscripten_run_script_string;
if (!me.bufferSize || me.bufferSize < s.length+1) {
var len = lengthBytesUTF8(s);
if (!me.bufferSize || me.bufferSize < len+1) {
if (me.bufferSize) _free(me.buffer);
me.bufferSize = s.length+1;
me.bufferSize = len+1;
me.buffer = _malloc(me.bufferSize);
}
stringToUTF8(s, me.buffer, me.bufferSize);
Expand Down
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,21 @@ def process(filename):
self.do_run_in_out_file_test('tests', 'core', 'test_emscripten_api',
post_build=check)

def test_emscripten_run_script_string_utf8(self):
src = r'''
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten.h>
int main() {
const char *str = emscripten_run_script_string("'\\u2603 \\u2603 \\u2603 Hello!'");
printf("length of returned string: %d. Position of substring 'Hello': %d\n", strlen(str), strstr(str, "Hello")-str);
return 0;
}
'''
self.do_run(src, '''length of returned string: 18. Position of substring 'Hello': 12''')

def test_emscripten_get_now(self):
self.banned_js_engines = [V8_ENGINE] # timer limitations in v8 shell

Expand Down

0 comments on commit fdc57b6

Please sign in to comment.