Skip to content

Commit b469721

Browse files
committed
Add error handling for stderrno when errno is invalid
1 parent 3622274 commit b469721

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,21 @@ 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+
static char buf[32];
40+
snprintf(buf, sizeof buf, "Unknown error %d", e);
41+
return buf;
42+
}
43+
s = (char *)&errmsgstr + errmsgidx[e];
4044
// strerror is a (debug) dependency of many emscripten syscalls which mean it
4145
// must be excluded from LTO, along with all of its dependencies.
4246
// In order to limit the transitive dependencies we disable localization of
4347
// rrno messages here.
4448
return (char *)s;
4549
#else
50+
if (e >= sizeof errmsgidx / sizeof *errmsgidx) e = 0;
51+
s = (char *)&errmsgstr + errmsgidx[e];
4652
return (char *)LCTRANS(s, LC_MESSAGES, loc);
4753
#endif
4854
}

test/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6169,6 +6169,10 @@ def test_unistd_curdir(self):
61696169
def test_unistd_close(self):
61706170
self.do_run_in_out_file_test('unistd/close.c')
61716171

6172+
@also_with_noderawfs
6173+
def test_unistd_errno(self):
6174+
self.do_run_in_out_file_test('unistd/errno.c')
6175+
61726176
def test_unistd_fsync_stdout(self):
61736177
self.do_run_in_out_file_test('unistd/fsync_stdout.c')
61746178

test/unistd/errno.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2011 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <errno.h>
11+
#include <unistd.h>
12+
#include <emscripten.h>
13+
14+
int main() {
15+
int error = -1;
16+
printf("errno: %s\n", strerror(error));
17+
}

test/unistd/errno.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
errno: Unknown error -1

0 commit comments

Comments
 (0)