diff --git a/system/lib/compiler-rt/emscripten_setjmp.c b/system/lib/compiler-rt/emscripten_setjmp.c index ae68acb08380..fb8a287391ad 100644 --- a/system/lib/compiler-rt/emscripten_setjmp.c +++ b/system/lib/compiler-rt/emscripten_setjmp.c @@ -67,6 +67,12 @@ uint32_t testSetjmp(uintptr_t id, TableEntry* table, uint32_t size) { #include "emscripten_internal.h" void emscripten_longjmp(uintptr_t env, int val) { + // C standard: + // The longjmp function cannot cause the setjmp macro to return + // the value 0; if val is 0, the setjmp macro returns the value 1. + if (val == 0) { + val = 1; + } setThrew(env, val); _emscripten_throw_longjmp(); } diff --git a/test/core/test_longjmp_zero.c b/test/core/test_longjmp_zero.c index dea87ef64d52..b2553982b9e6 100644 --- a/test/core/test_longjmp_zero.c +++ b/test/core/test_longjmp_zero.c @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include #include #include @@ -13,6 +14,7 @@ int main() { jmp_buf b1; int val = setjmp(b1); if (val) { + assert(val == 1); printf("success\n"); return 0; }