Skip to content

Commit 545c29b

Browse files
Add error handling for stderrno when errno is invalid (#25762)
This is to match llvm-libc's behavior when an invalid errno is specified. When errno is specified as -1, for instance, the current behavior prints "Success". With this change, any invalid errno emits out "Unknown error %d".
1 parent f69abab commit 545c29b

12 files changed

+54
-31
lines changed

system/lib/libc/musl/src/errno/__strerror.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* with these messages, and then to define a lookup table translating
44
* error codes to offsets of corresponding fields in the structure. */
55
#if defined(__EMSCRIPTEN__)
6-
// This is intended to match the errno in llvm-libc.
6+
/* Error handling is introduced to match the behavior in llvm-libc.
7+
* When invalid errno is specified, Unknown error: errno_code is emitted.
8+
*/
79
E(0, "Success")
810
#else
911
E(0, "No error information")

system/lib/libc/musl/src/errno/strerror.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@ char *__strerror_l(int e, locale_t loc)
3434
if (e==EDQUOT) e=0;
3535
else if (e==EDQUOT_ORIG) e=EDQUOT;
3636
#endif
37-
if (e >= sizeof errmsgidx / sizeof *errmsgidx) e = 0;
38-
s = (char *)&errmsgstr + errmsgidx[e];
3937
#ifdef __EMSCRIPTEN__
38+
if (e < 0 || e >= sizeof errmsgidx / sizeof *errmsgidx || (e != 0 && !errmsgidx[e])) {
39+
return "Unknown error";
40+
}
41+
s = (char *)&errmsgstr + errmsgidx[e];
4042
// strerror is a (debug) dependency of many emscripten syscalls which mean it
4143
// must be excluded from LTO, along with all of its dependencies.
4244
// In order to limit the transitive dependencies we disable localization of
4345
// rrno messages here.
4446
return (char *)s;
4547
#else
48+
if (e >= sizeof errmsgidx / sizeof *errmsgidx) e = 0;
49+
s = (char *)&errmsgstr + errmsgidx[e];
4650
return (char *)LCTRANS(s, LC_MESSAGES, loc);
4751
#endif
4852
}

test/codesize/test_codesize_cxx_except.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 23325,
33
"a.out.js.gz": 9128,
4-
"a.out.nodebug.wasm": 171265,
5-
"a.out.nodebug.wasm.gz": 57337,
6-
"total": 194590,
7-
"total_gz": 66465,
4+
"a.out.nodebug.wasm": 171311,
5+
"a.out.nodebug.wasm.gz": 57348,
6+
"total": 194636,
7+
"total_gz": 66476,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

test/codesize/test_codesize_cxx_except_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 19486,
33
"a.out.js.gz": 8077,
4-
"a.out.nodebug.wasm": 144620,
5-
"a.out.nodebug.wasm.gz": 54891,
6-
"total": 164106,
7-
"total_gz": 62968,
4+
"a.out.nodebug.wasm": 144667,
5+
"a.out.nodebug.wasm.gz": 54907,
6+
"total": 164153,
7+
"total_gz": 62984,
88
"sent": [
99
"_abort_js",
1010
"_tzset_js",

test/codesize/test_codesize_cxx_except_wasm_legacy.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 19555,
33
"a.out.js.gz": 8099,
4-
"a.out.nodebug.wasm": 142209,
5-
"a.out.nodebug.wasm.gz": 54350,
6-
"total": 161764,
7-
"total_gz": 62449,
4+
"a.out.nodebug.wasm": 142256,
5+
"a.out.nodebug.wasm.gz": 54366,
6+
"total": 161811,
7+
"total_gz": 62465,
88
"sent": [
99
"_abort_js",
1010
"_tzset_js",

test/codesize/test_codesize_cxx_mangle.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 23375,
33
"a.out.js.gz": 9148,
4-
"a.out.nodebug.wasm": 235299,
5-
"a.out.nodebug.wasm.gz": 78928,
6-
"total": 258674,
7-
"total_gz": 88076,
4+
"a.out.nodebug.wasm": 235343,
5+
"a.out.nodebug.wasm.gz": 78952,
6+
"total": 258718,
7+
"total_gz": 88100,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

test/codesize/test_codesize_hello_O0.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 24194,
33
"a.out.js.gz": 8694,
4-
"a.out.nodebug.wasm": 15117,
5-
"a.out.nodebug.wasm.gz": 7456,
6-
"total": 39311,
7-
"total_gz": 16150,
4+
"a.out.nodebug.wasm": 15168,
5+
"a.out.nodebug.wasm.gz": 7488,
6+
"total": 39362,
7+
"total_gz": 16182,
88
"sent": [
99
"fd_write"
1010
],

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"a.out.js": 245483,
3-
"a.out.nodebug.wasm": 574056,
4-
"total": 819539,
3+
"a.out.nodebug.wasm": 573907,
4+
"total": 819390,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"hello_world.js": 56928,
33
"hello_world.js.gz": 17700,
4-
"hello_world.wasm": 15117,
5-
"hello_world.wasm.gz": 7456,
4+
"hello_world.wasm": 15168,
5+
"hello_world.wasm.gz": 7488,
66
"no_asserts.js": 26632,
77
"no_asserts.js.gz": 8884,
88
"no_asserts.wasm": 12217,
99
"no_asserts.wasm.gz": 6015,
1010
"strict.js": 54943,
1111
"strict.js.gz": 17045,
12-
"strict.wasm": 15117,
13-
"strict.wasm.gz": 7451,
14-
"total": 180954,
15-
"total_gz": 64551
12+
"strict.wasm": 15168,
13+
"strict.wasm.gz": 7482,
14+
"total": 181056,
15+
"total_gz": 64614
1616
}

test/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13120,6 +13120,9 @@ def test_wasmfs_before_preload(self):
1312013120
def test_hello_world_above_2gb(self):
1312113121
self.do_run_in_out_file_test('hello_world.c', cflags=['-sGLOBAL_BASE=2GB', '-sINITIAL_MEMORY=3GB'])
1312213122

13123+
def test_unistd_strerror(self):
13124+
self.do_run_in_out_file_test('unistd/strerror.c')
13125+
1312313126
def test_hello_function(self):
1312413127
# hello_function.cpp is referenced/used in the docs. This test ensures that it
1312513128
# at least compiles.

0 commit comments

Comments
 (0)