diff --git a/libc/src/setjmp/x86_64/longjmp.cpp b/libc/src/setjmp/x86_64/longjmp.cpp --- a/libc/src/setjmp/x86_64/longjmp.cpp +++ b/libc/src/setjmp/x86_64/longjmp.cpp @@ -29,6 +29,7 @@ // |val| in rax. Note that this has to happen before we restore the registers // from values in |buf|. Otherwise, once rsp and rbp are updated, we cannot // read |val|. + val = val == 0 ? 1 : val; LIBC_INLINE_ASM("mov %1, %0\n\t" : "=r"(rax) : "m"(val) :); LIBC_INLINE_ASM("mov %1, %0\n\t" : "=r"(rbx) : "m"(buf->rbx) :); LIBC_INLINE_ASM("mov %1, %0\n\t" : "=r"(rbp) : "m"(buf->rbp) :); diff --git a/libc/test/src/setjmp/setjmp_test.cpp b/libc/test/src/setjmp/setjmp_test.cpp --- a/libc/test/src/setjmp/setjmp_test.cpp +++ b/libc/test/src/setjmp/setjmp_test.cpp @@ -28,3 +28,13 @@ } ASSERT_EQ(n, MAX_LOOP + 1); } + +TEST(LlvmLibcSetJmpTest, SetAndJumpBackValOne) { + jmp_buf env; + + int val = __llvm_libc::setjmp(env); + if (val == 0) + __llvm_libc::longjmp(env, val); + + ASSERT_EQ(val, 1); +} \ No newline at end of file