https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100049
Bug ID: 100049
Summary: loop counter double increment with longjmp inside
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: clyon at gcc dot gnu.org
Target Milestone: ---
Created attachment 50572
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50572&action=edit
Example code
As described in https://bugs.linaro.org/show_bug.cgi?id=5755
the following loop:
for(i = 0; i < CNT; i++) {
printf("Message %d\n", i);
if (setjmp((&ctx.jmp_buf[ctx.cnt])->env) == 0) {
++ctx.cnt;
do_jump();
--ctx.cnt;
}
}
has the following output:
Message 0
Message 2
It's sufficient to use -O2, and the offending code sequence is:
bl _setjmp
(*) ldr w1, [sp, 44]
add w1, w1, 1
str w1, [sp, 44]
cbnz w0, .L3
[....]
bl do_jump (which calls longjmp)
where w1 contains "i", the line marked (*) is where longjmp jumps to.
So "i" is incremented before calling longjmp, and a second time when longjmp
gives control back to the test() function.
The attached archive contains:
helper.h, helper.c
example.c
Makefile