Skip to content

Commit 89d6abf

Browse files
Gao Jiaweixiaoxiang781216
authored andcommitted
setjmp: fix setjmp returns 0 when calling longjmp with 0 as the second argument
Signed-off-by: Gao Jiawei <gaojiawei@xiaomi.com>
1 parent 036a067 commit 89d6abf

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

libs/libc/machine/arm/gnu/arch_setjmp.S

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,21 @@ longjmp:
170170
vmsr fpscr, r2 /* Restore the FPSCR */
171171
#endif /* CONFIG_ARCH_FPU */
172172

173-
mov r0, r1 /* return val */
173+
/* Check and substitute the given return value to 1 if it's 0 */
174+
175+
movs r0, r1
176+
#ifdef CONFIG_ARCH_ARMV6M
177+
/* ARMv6-M only supports branching with condition
178+
* So we fall back to not use IT blocks in that case
179+
*/
180+
181+
bne 1f
182+
movs r0, #1
183+
1:
184+
#else
185+
it eq
186+
moveq r0, #1
187+
#endif
174188
bx lr
175189

176190
.size longjmp, .-longjmp

libs/libc/machine/sim/arch_setjmp_x86.S

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ SYMBOL(setjmp):
8585
SYMBOL(longjmp):
8686
movl 4(%esp), %ecx /* jmpbuf in %ecx. */
8787
movl 8(%esp), %eax /* Second argument is return value. */
88-
88+
testl %eax, %eax
89+
jnz 1f
90+
incl %eax
8991
/* Save the return address now. */
90-
92+
1:
9193
movl (JB_PC)(%ecx), %edx
9294

9395
/* Restore registers. */

libs/libc/machine/sim/arch_setjmp_x86_64.S

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ SYMBOL(longjmp):
130130
/* Setup return value */
131131

132132
movl %esi,%eax
133+
testl %eax,%eax
134+
jnz 1f
135+
incl %eax
133136

134137
/* Restore registers */
135-
138+
1:
136139
movq JB_RBX(REGS),%rbx /* Load 1: rbx */
137140
movq JB_RSP(REGS),%rsp /* Load 2: rsp */
138141
movq JB_RBP(REGS),%rbp /* Load 3: rdi */

0 commit comments

Comments
 (0)