Skip to content

Commit 4218f89

Browse files
authored
Use higher-level string helpers where possible. NFC (#24486)
I guess one functional change is in implementation of `emscripten_fetch_get_response_headers_length`. The docs say that it just returns the length, and you must use `length + 1` for buffer size passed to `emscripten_fetch_get_response_headers`, and that's what tests do, but we were actually returning `length + 1` already. This seems like a bugfix, but happy to revert if deemed too risky.
1 parent c898bb4 commit 4218f89

File tree

10 files changed

+32
-46
lines changed

10 files changed

+32
-46
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ See docs/process.md for more on how version tagging works.
4646
- The field `responseUrl` is added to `emscripten_fetch_t`. This is notably
4747
usable for obtaining resolved URL, in line with JS `XMLHttpRequest.responseURL`
4848
field. (#24414)
49+
- `emscripten_fetch_get_response_headers_length` now excludes the trailing
50+
null character from the length calculation to match the documented behaviour.
51+
(#24486)
4952

5053
4.0.9 - 05/19/25
5154
----------------

src/Fetch.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,12 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
584584
}
585585

586586
function fetchGetResponseHeadersLength(id) {
587-
return lengthBytesUTF8(Fetch.xhrs.get(id).getAllResponseHeaders()) + 1;
587+
return lengthBytesUTF8(Fetch.xhrs.get(id).getAllResponseHeaders());
588588
}
589589

590590
function fetchGetResponseHeaders(id, dst, dstSizeBytes) {
591591
var responseHeaders = Fetch.xhrs.get(id).getAllResponseHeaders();
592-
var lengthBytes = lengthBytesUTF8(responseHeaders) + 1;
593-
stringToUTF8(responseHeaders, dst, dstSizeBytes);
594-
return Math.min(lengthBytes, dstSizeBytes);
592+
return stringToUTF8(responseHeaders, dst, dstSizeBytes) + 1;
595593
}
596594

597595
//Delete the xhr JS object, allowing it to be garbage collected.

src/lib/libfs.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ var LibraryFS = {
1010
'$FS_modeStringToFlags',
1111
'$FS_getMode',
1212
'$intArrayFromString',
13-
'$stringToUTF8Array',
14-
'$lengthBytesUTF8',
1513
#if LibraryManager.has('libidbfs.js')
1614
'$IDBFS',
1715
#endif
@@ -1347,28 +1345,24 @@ FS.staticInit();`;
13471345
if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') {
13481346
throw new Error(`Invalid encoding type "${opts.encoding}"`);
13491347
}
1350-
var ret;
13511348
var stream = FS.open(path, opts.flags);
13521349
var stat = FS.stat(path);
13531350
var length = stat.size;
13541351
var buf = new Uint8Array(length);
13551352
FS.read(stream, buf, 0, length, 0);
13561353
if (opts.encoding === 'utf8') {
1357-
ret = UTF8ArrayToString(buf);
1358-
} else if (opts.encoding === 'binary') {
1359-
ret = buf;
1354+
buf = UTF8ArrayToString(buf);
13601355
}
13611356
FS.close(stream);
1362-
return ret;
1357+
return buf;
13631358
},
13641359
writeFile(path, data, opts = {}) {
13651360
opts.flags = opts.flags || {{{ cDefs.O_TRUNC | cDefs.O_CREAT | cDefs.O_WRONLY }}};
13661361
var stream = FS.open(path, opts.flags, opts.mode);
13671362
if (typeof data == 'string') {
1368-
var buf = new Uint8Array(lengthBytesUTF8(data)+1);
1369-
var actualNumBytes = stringToUTF8Array(data, buf, 0, buf.length);
1370-
FS.write(stream, buf, 0, actualNumBytes, undefined, opts.canOwn);
1371-
} else if (ArrayBuffer.isView(data)) {
1363+
data = new Uint8Array(intArrayFromString(data, true));
1364+
}
1365+
if (ArrayBuffer.isView(data)) {
13721366
FS.write(stream, data, 0, data.byteLength, undefined, opts.canOwn);
13731367
} else {
13741368
throw new Error('Unsupported data type');

src/lib/libsdl.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var LibrarySDL = {
2020
'$PATH', '$Browser', 'SDL_GetTicks', 'SDL_LockSurface',
2121
'$MainLoop',
2222
// For makeCEvent().
23-
'$intArrayFromString',
23+
'$stringToUTF8',
2424
// Many SDL functions depend on malloc/free
2525
'malloc', 'free',
2626
'memcpy',
@@ -964,10 +964,7 @@ var LibrarySDL = {
964964
case 'keypress': {
965965
{{{ makeSetValue('ptr', C_STRUCTS.SDL_TextInputEvent.type, 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
966966
// Not filling in windowID for now
967-
var cStr = intArrayFromString(String.fromCharCode(event.charCode));
968-
for (var i = 0; i < cStr.length; ++i) {
969-
{{{ makeSetValue('ptr', C_STRUCTS.SDL_TextInputEvent.text + ' + i', 'cStr[i]', 'i8') }}};
970-
}
967+
stringToUTF8(String.fromCharCode(event.charCode), ptr + {{{ C_STRUCTS.SDL_TextInputEvent.text }}}, 4);
971968
break;
972969
}
973970
case 'mousedown': case 'mouseup': case 'mousemove': {
@@ -1782,7 +1779,7 @@ var LibrarySDL = {
17821779
SDL_GetKeyState: () => _SDL_GetKeyboardState(0),
17831780

17841781
SDL_GetKeyName__proxy: 'sync',
1785-
SDL_GetKeyName__deps: ['$stringToUTF8', 'realloc'],
1782+
SDL_GetKeyName__deps: ['$lengthBytesUTF8', '$stringToUTF8', 'realloc'],
17861783
SDL_GetKeyName: (key) => {
17871784
var name = '';
17881785
/* ASCII A-Z or 0-9 */
@@ -2904,8 +2901,8 @@ var LibrarySDL = {
29042901
audio.frequency = info.audio.frequency;
29052902
}
29062903
audio['onended'] = function() { // TODO: cache these
2907-
if (channelInfo.audio === this || channelInfo.audio.webAudioNode === this) {
2908-
channelInfo.audio.paused = true; channelInfo.audio = null;
2904+
if (channelInfo.audio === this || channelInfo.audio.webAudioNode === this) {
2905+
channelInfo.audio.paused = true; channelInfo.audio = null;
29092906
}
29102907
if (SDL.channelFinished) {{{ makeDynCall('vi', 'SDL.channelFinished') }}}(channel);
29112908
}

src/lib/libtime.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ addToLibrary({
257257
},
258258

259259
strptime__deps: ['$isLeapYear', '$arraySum', '$addDays', '$MONTH_DAYS_REGULAR', '$MONTH_DAYS_LEAP',
260-
'$intArrayFromString' ],
260+
'$lengthBytesUTF8'],
261261
strptime: (buf, format, tm) => {
262262
// char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tm);
263263
// http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html
@@ -474,7 +474,7 @@ addToLibrary({
474474
// GMT offset as either 'Z' or +-HH:MM or +-HH or +-HHMM
475475
if (value.toLowerCase() === 'z'){
476476
date.gmtoff = 0;
477-
} else {
477+
} else {
478478
var match = value.match(/^((?:\-|\+)\d\d):?(\d\d)?/);
479479
date.gmtoff = match[1] * 3600;
480480
if (match[2]) {
@@ -507,10 +507,10 @@ addToLibrary({
507507
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_yday, 'arraySum(isLeapYear(fullDate.getFullYear()) ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, fullDate.getMonth()-1)+fullDate.getDate()-1', 'i32') }}};
508508
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_isdst, '0', 'i32') }}};
509509
{{{ makeSetValue('tm', C_STRUCTS.tm.tm_gmtoff, 'date.gmtoff', LONG_TYPE) }}};
510-
510+
511511
// we need to convert the matched sequence into an integer array to take care of UTF-8 characters > 0x7F
512512
// TODO: not sure that intArrayFromString handles all unicode characters correctly
513-
return buf+intArrayFromString(matches[0]).length-1;
513+
return buf+lengthBytesUTF8(matches[0]);
514514
}
515515

516516
return 0;

src/lib/libwasi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ var WasiLibrary = {
442442
return {{{ cDefs.EBADF }}};
443443
}
444444
var preopen_path = preopens[fd];
445-
stringToUTF8Array(preopen_path, HEAP8, path, path_len)
445+
stringToUTF8(preopen_path, path, path_len)
446446
#if SYSCALL_DEBUG
447447
dbg(`fd_prestat_dir_name -> "${preopen_path}"`);
448448
#endif

src/lib/libwasmfs.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ addToLibrary({
141141

142142
// Default return type is binary.
143143
// The buffer contents exist 8 bytes after the returned pointer.
144-
var ret = new Uint8Array(HEAPU8.subarray(buf, buf + length));
145-
return opts.encoding === 'utf8' ? UTF8ArrayToString(ret) : ret;
144+
return opts.encoding === 'utf8' ? UTF8ToString(buf, length) : HEAPU8.slice(buf, buf + length);
146145
},
147146
#endif
148147

@@ -260,7 +259,7 @@ addToLibrary({
260259
ino: {{{ makeGetValue('statBuf', C_STRUCTS.stat.st_ino, "u53") }}}
261260
}
262261
},
263-
stat(path) {
262+
stat(path) {
264263
return withStackSave(() => {
265264
var statBuf = stackAlloc({{{ C_STRUCTS.stat.__size__ }}});
266265
FS.handleError(__wasmfs_stat(stringToUTF8OnStack(path), statBuf));
@@ -507,19 +506,17 @@ addToLibrary({
507506
$FS_writeFile: (path, data) => {
508507
var sp = stackSave();
509508
var pathBuffer = stringToUTF8OnStack(path);
510-
if (typeof data == 'string') {
511-
var buf = new Uint8Array(lengthBytesUTF8(data) + 1);
512-
var actualNumBytes = stringToUTF8Array(data, buf, 0, buf.length);
513-
data = buf.slice(0, actualNumBytes);
514-
}
515-
var dataBuffer = _malloc(data.length);
509+
var len = typeof data == 'string' ? lengthBytesUTF8(data) + 1 : data.length;
510+
var dataBuffer = _malloc(len);
516511
#if ASSERTIONS
517512
assert(dataBuffer);
518513
#endif
519-
for (var i = 0; i < data.length; i++) {
520-
{{{ makeSetValue('dataBuffer', 'i', 'data[i]', 'i8') }}};
514+
if (typeof data == 'string') {
515+
len = stringToUTF8(data, dataBuffer, len);
516+
} else {
517+
HEAPU8.set(data, dataBuffer);
521518
}
522-
var ret = __wasmfs_write_file(pathBuffer, dataBuffer, data.length);
519+
var ret = __wasmfs_write_file(pathBuffer, dataBuffer, len);
523520
_free(dataBuffer);
524521
stackRestore(sp);
525522
return ret;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11676
1+
11657
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27564
1+
27541

test/wasmfs/wasmfs_fetch.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ addToLibrary({
22
getUrlOrigin__sig: 'vpp',
33
getUrlOrigin: function (ptr, len) {
44
try {
5-
var orig = self.location.origin;
6-
var nb = lengthBytesUTF8(orig) + 1;
7-
if (nb <= len)
8-
stringToUTF8(orig, ptr, len);
5+
stringToUTF8(location.origin, ptr, len);
96
} catch (e) {
107
console.warn(e);
118
}

0 commit comments

Comments
 (0)