tools/nolibc: Fix WIFSIGNALED(0)
PATCH 1/1 tools/nolibc: Fix WIFSIGNALED(0)
WIFSIGNALED(0) currently returns true, so a process that exited normally with status 0 looks like it was killed by a signal.
The macro uses a trick of checking whether status - 1 is below 0xff but is subtly broken because of using signed arithmetic. For 0 it computes -1 < 0xff which is true.
Do what musl does: mask the status to 16 bits and use unsigned arithmetic so 0 wraps to 0xffff and the comparison fails. The mask also prevents bits we don't care about from sneaking in.
---
I'm putting off sending anything else (dirfd and some other bits) until after the merge window has finished.. but this one seems like it should be fixed sooner.
I found this while debugging an annoying issue in my homebrew nolibc based shell.
tools/include/nolibc/types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/include/nolibc/types.h +1 -1
@@ -119,7 +119,7 @@ struct timeval {#define WEXITSTATUS (status) (((status) & 0xff00) >> 8)#define WIFEXITED (status) (((status) & 0x7f) == 0)#define WTERMSIG (status) ((status) & 0x7f) -#define WIFSIGNALED (status) ((status) - 1 < 0xff) +#define WIFSIGNALED (status) (((status) & 0xffff) - 1U < 0xffU)/* standard exit() codes */ #define EXIT_SUCCESS 0
Signature · 1 line
2.53.0
Thomas Weißschuh
maintainerHi Daniel,
Quoted 10 lines · Daniel Palmer
WIFSIGNALED(0) currently returns true, so a process that exited normally with status 0 looks like it was killed by a signal.
The macro uses a trick of checking whether status - 1 is below
0xffbut is subtly broken because of using signed arithmetic. For 0 it computes -1 <0xffwhich is true.Do what musl does: mask the status to 16 bits and use unsigned arithmetic so 0 wraps to
0xffffand the comparison fails. The mask also prevents bits we don't care about from sneaking in.
We already have commit 368fae47c6ca ("tools/nolibc: fix WIFSIGNALED() for status 0") for this issue in nolibc-next. This implementation is masking the status with 0xffff. OTOH the musl implementation of WIFCONTINUED() is also missing the masking, so I am not sure how necessary it is.
I'll look into it some more.
Quoted 7 lines
Fixes 8c934d4822c7 ("tools/nolibc: add helpers for wait() signal exits")Signed-off-by Daniel Palmer <[email protected]>---
I'm putting off sending anything else (dirfd and some other bits) until after the merge window has finished.. but this one seems like it should be fixed sooner.
Feel free to send the patches already. We won't apply them until after the merge window, but we can discuss them before. There are no other trees with changes to nolibc, so there won't be any conflicts.
Quoted 22 lines
I found this while debugging an annoying issue in my homebrew nolibc based shell.
tools/include/nolibc/types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/include/nolibc/types.h +1
-1@@ -119,7 +119,7 @@ struct timeval {#define WEXITSTATUS (status) (((status) & 0xff00) >> 8)#define WIFEXITED (status) (((status) & 0x7f) == 0)#define WTERMSIG (status) ((status) & 0x7f) -#define WIFSIGNALED (status) ((status) - 1 < 0xff) +#define WIFSIGNALED (status) (((status) & 0xffff) - 1U < 0xffU)/* standard exit() codes */ #define EXIT_SUCCESS 0Signature · 1 line
2.53.0