This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 89d6abf3df setjmp: fix setjmp returns 0 when calling longjmp with 0 as 
the second argument
89d6abf3df is described below

commit 89d6abf3dfcf0bec86ed10ec9494995b6f3b41b4
Author: Gao Jiawei <[email protected]>
AuthorDate: Tue Jul 23 18:05:00 2024 +0800

    setjmp: fix setjmp returns 0 when calling longjmp with 0 as the second 
argument
    
    Signed-off-by: Gao Jiawei <[email protected]>
---
 libs/libc/machine/arm/gnu/arch_setjmp.S    | 16 +++++++++++++++-
 libs/libc/machine/sim/arch_setjmp_x86.S    |  6 ++++--
 libs/libc/machine/sim/arch_setjmp_x86_64.S |  5 ++++-
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/libs/libc/machine/arm/gnu/arch_setjmp.S 
b/libs/libc/machine/arm/gnu/arch_setjmp.S
index c4a26fcb93..1183f230d1 100644
--- a/libs/libc/machine/arm/gnu/arch_setjmp.S
+++ b/libs/libc/machine/arm/gnu/arch_setjmp.S
@@ -170,7 +170,21 @@ longjmp:
        vmsr            fpscr, r2                       /* Restore the FPSCR */
 #endif /* CONFIG_ARCH_FPU */
 
-       mov             r0, r1                          /* return val */
+       /* Check and substitute the given return value to 1 if it's 0 */
+
+       movs            r0, r1
+#ifdef CONFIG_ARCH_ARMV6M
+       /* ARMv6-M only supports branching with condition
+        * So we fall back to not use IT blocks in that case
+        */
+
+       bne             1f
+       movs            r0, #1
+1:
+#else
+       it              eq
+       moveq           r0, #1
+#endif
        bx              lr
 
        .size   longjmp, .-longjmp
diff --git a/libs/libc/machine/sim/arch_setjmp_x86.S 
b/libs/libc/machine/sim/arch_setjmp_x86.S
index 439e21331a..ea31516ffb 100644
--- a/libs/libc/machine/sim/arch_setjmp_x86.S
+++ b/libs/libc/machine/sim/arch_setjmp_x86.S
@@ -85,9 +85,11 @@ SYMBOL(setjmp):
 SYMBOL(longjmp):
        movl    4(%esp), %ecx      /* jmpbuf in %ecx.  */
        movl    8(%esp), %eax      /* Second argument is return value.  */
-
+       testl   %eax, %eax
+       jnz     1f
+       incl    %eax
        /* Save the return address now.  */
-
+1:
        movl    (JB_PC)(%ecx), %edx
 
        /* Restore registers.  */
diff --git a/libs/libc/machine/sim/arch_setjmp_x86_64.S 
b/libs/libc/machine/sim/arch_setjmp_x86_64.S
index 62c15ff355..896851e10f 100644
--- a/libs/libc/machine/sim/arch_setjmp_x86_64.S
+++ b/libs/libc/machine/sim/arch_setjmp_x86_64.S
@@ -130,9 +130,12 @@ SYMBOL(longjmp):
        /* Setup return value */
 
        movl    %esi,%eax
+       testl   %eax,%eax
+       jnz     1f
+       incl    %eax
 
        /* Restore registers */
-
+1:
        movq    JB_RBX(REGS),%rbx       /* Load 1: rbx */
        movq    JB_RSP(REGS),%rsp       /* Load 2: rsp */
        movq    JB_RBP(REGS),%rbp       /* Load 3: rdi */

Reply via email to