Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion system/lib/libc/musl/src/errno/__strerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* with these messages, and then to define a lookup table translating
* error codes to offsets of corresponding fields in the structure. */
#if defined(__EMSCRIPTEN__)
// This is intended to match the errno in llvm-libc.
/* Error handling is introduced to match the behavior in llvm-libc.
* When invalid errno is specified, Unknown error: errno_code is emitted.
*/
E(0, "Success")
#else
E(0, "No error information")
Expand Down
8 changes: 6 additions & 2 deletions system/lib/libc/musl/src/errno/strerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ char *__strerror_l(int e, locale_t loc)
if (e==EDQUOT) e=0;
else if (e==EDQUOT_ORIG) e=EDQUOT;
#endif
if (e >= sizeof errmsgidx / sizeof *errmsgidx) e = 0;
s = (char *)&errmsgstr + errmsgidx[e];
#ifdef __EMSCRIPTEN__
if (e < 0 || e >= sizeof errmsgidx / sizeof *errmsgidx || (e != 0 && !errmsgidx[e])) {
return "Unknown error";
}
s = (char *)&errmsgstr + errmsgidx[e];
// strerror is a (debug) dependency of many emscripten syscalls which mean it
// must be excluded from LTO, along with all of its dependencies.
// In order to limit the transitive dependencies we disable localization of
// rrno messages here.
return (char *)s;
#else
if (e >= sizeof errmsgidx / sizeof *errmsgidx) e = 0;
s = (char *)&errmsgstr + errmsgidx[e];
return (char *)LCTRANS(s, LC_MESSAGES, loc);
#endif
}
Expand Down
3 changes: 3 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -13124,6 +13124,9 @@ def test_wasmfs_before_preload(self):
def test_hello_world_above_2gb(self):
self.do_run_in_out_file_test('hello_world.c', cflags=['-sGLOBAL_BASE=2GB', '-sINITIAL_MEMORY=3GB'])

def test_unistd_strerror(self):
self.do_run_in_out_file_test('unistd/strerror.c')

def test_hello_function(self):
# hello_function.cpp is referenced/used in the docs. This test ensures that it
# at least compiles.
Expand Down
13 changes: 13 additions & 0 deletions test/unistd/strerror.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2025 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <stdio.h>
#include <string.h>

int main() {
printf("errno: %s\n", strerror(-1));
}
1 change: 1 addition & 0 deletions test/unistd/strerror.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
errno: Unknown error
Loading