Skip to content

Commit

Permalink
add test for longjump with 0 as return value
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Jiawei <gaojiawei@xiaomi.com>
  • Loading branch information
Gao Jiawei authored and xiaoxiang781216 committed Oct 15, 2024
1 parent 3f08d4f commit 30215c2
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions testing/ostest/setjmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,56 @@
* Public Functions
****************************************************************************/

void setjmp_test(void)
void jump_with_retval(jmp_buf buf, int ret)
{
volatile bool did_jump = false;
int value;
jmp_buf buf;

printf("setjmp_test: Initializing jmp_buf\n");

if ((value = setjmp(buf)) == 0)
{
printf("setjmp_test: Try jump\n");
longjmp(buf, 123);

if (did_jump)
{
ASSERT(!"setjmp retutns zero after calling longjmp");
}

did_jump = true;
printf("setjmp_test: About to jump, longjmp with ret val: %d\n", ret);
longjmp(buf, ret);

/* Unreachable */

ASSERT(0);
}
else
{
ASSERT(value == 123);
/* If we provide 0 as the return value to longjmp()
* we expect it substitute to 1 for us.
*/

if (ret == 0)
{
ret = 1;
}

ASSERT(value == ret);
printf("setjmp_test: Jump succeed\n");
}
}

void setjmp_test(void)
{
jmp_buf buf;

printf("setjmp_test: Initializing jmp_buf\n");

jump_with_retval(buf, 123);

/* Pls ref to:
* the longjmp should never make setjmp returns 0
* https://pubs.opengroup.org/onlinepubs/009604599/functions/longjmp.html
*/

jump_with_retval(buf, 0);
}

0 comments on commit 30215c2

Please sign in to comment.