1 /* Copyright (C) 2003-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <setjmp.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 
24 static sigjmp_buf jmpbuf;
25 
26 static void
sig_handler(int signo)27 sig_handler (int signo)
28 {
29   siglongjmp (jmpbuf, 1);
30 }
31 
32 static int
do_test(void)33 do_test (void)
34 {
35   char *p = NULL;
36   /* gcc can overwrite the success written value by scheduling instructions
37      around sprintf.  It is allowed to do this since according to C99 the first
38      argument of sprintf is a character array and NULL is not a valid character
39      array.  Mark the return value as volatile so that it gets reloaded on
40      return.  */
41   volatile int ret = 0;
42 
43   if (signal (SIGSEGV, &sig_handler) == SIG_ERR)
44     {
45       perror ("installing SIGSEGV handler");
46       return 1;
47     }
48 
49   puts ("Attempting to sprintf to null ptr");
50   if (setjmp (jmpbuf))
51     {
52       puts ("Exiting main...");
53       return ret;
54     }
55 
56   sprintf (p, "This should segv\n");
57 
58   return 1;
59 }
60 
61 #define TEST_FUNCTION do_test ()
62 #include "../test-skeleton.c"
63