re_search_internal: Avoid overflow in computing re_malloc buffer size
[glibc.git] / test-skeleton.c
blob0238db6b4dbe367ae3a42f9c14e05b08a1e4ffc6
1 /* Skeleton for test programs.
2 Copyright (C) 1998,2000-2004, 2005, 2009 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <getopt.h>
23 #include <malloc.h>
24 #include <search.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/resource.h>
31 #include <sys/wait.h>
32 #include <sys/param.h>
33 #include <time.h>
35 /* The test function is normally called `do_test' and it is called
36 with argc and argv as the arguments. We nevertheless provide the
37 possibility to overwrite this name. */
38 #ifndef TEST_FUNCTION
39 # define TEST_FUNCTION do_test (argc, argv)
40 #endif
42 #ifndef TEST_DATA_LIMIT
43 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
44 #endif
46 #define OPT_DIRECT 1000
47 #define OPT_TESTDIR 1001
49 static struct option options[] =
51 #ifdef CMDLINE_OPTIONS
52 CMDLINE_OPTIONS
53 #endif
54 { "direct", no_argument, NULL, OPT_DIRECT },
55 { "test-dir", required_argument, NULL, OPT_TESTDIR },
56 { NULL, 0, NULL, 0 }
59 /* PID of the test itself. */
60 static pid_t pid;
62 /* Directory to place temporary files in. */
63 static const char *test_dir;
65 /* List of temporary files. */
66 struct temp_name_list
68 struct qelem q;
69 const char *name;
70 } *temp_name_list;
72 /* Add temporary files in list. */
73 static void
74 __attribute__ ((unused))
75 add_temp_file (const char *name)
77 struct temp_name_list *newp
78 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
79 if (newp != NULL)
81 newp->name = name;
82 if (temp_name_list == NULL)
83 temp_name_list = (struct temp_name_list *) &newp->q;
84 else
85 insque (newp, temp_name_list);
89 /* Delete all temporary files. */
90 static void
91 delete_temp_files (void)
93 while (temp_name_list != NULL)
95 remove (temp_name_list->name);
96 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
100 /* Create a temporary file. */
101 static int
102 __attribute__ ((unused))
103 create_temp_file (const char *base, char **filename)
105 char *fname;
106 int fd;
108 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
109 + sizeof ("XXXXXX"));
110 if (fname == NULL)
112 puts ("out of memory");
113 return -1;
115 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
117 fd = mkstemp (fname);
118 if (fd == -1)
120 printf ("cannot open temporary file '%s': %m\n", fname);
121 free (fname);
122 return -1;
125 add_temp_file (fname);
126 if (filename != NULL)
127 *filename = fname;
129 return fd;
132 /* Timeout handler. We kill the child and exit with an error. */
133 static void
134 __attribute__ ((noreturn))
135 signal_handler (int sig __attribute__ ((unused)))
137 int killed;
138 int status;
140 /* Send signal. */
141 kill (pid, SIGKILL);
143 /* Wait for it to terminate. */
144 int i;
145 for (i = 0; i < 5; ++i)
147 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
148 if (killed != 0)
149 break;
151 /* Delay, give the system time to process the kill. If the
152 nanosleep() call return prematurely, all the better. We
153 won't restart it since this probably means the child process
154 finally died. */
155 struct timespec ts;
156 ts.tv_sec = 0;
157 ts.tv_nsec = 100000000;
158 nanosleep (&ts, NULL);
160 if (killed != 0 && killed != pid)
162 perror ("Failed to kill test process");
163 exit (1);
166 #ifdef CLEANUP_HANDLER
167 CLEANUP_HANDLER;
168 #endif
170 if (sig == SIGINT)
172 signal (sig, SIG_DFL);
173 raise (sig);
176 /* If we expected this signal: good! */
177 #ifdef EXPECTED_SIGNAL
178 if (EXPECTED_SIGNAL == SIGALRM)
179 exit (0);
180 #endif
182 if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
183 fputs ("Timed out: killed the child process\n", stderr);
184 else if (WIFSTOPPED (status))
185 fprintf (stderr, "Timed out: the child process was %s\n",
186 strsignal (WSTOPSIG (status)));
187 else if (WIFSIGNALED (status))
188 fprintf (stderr, "Timed out: the child process got signal %s\n",
189 strsignal (WTERMSIG (status)));
190 else
191 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
192 WEXITSTATUS (status));
194 /* Exit with an error. */
195 exit (1);
198 /* We provide the entry point here. */
200 main (int argc, char *argv[])
202 int direct = 0; /* Directly call the test function? */
203 int status;
204 int opt;
205 unsigned int timeoutfactor = 1;
206 pid_t termpid;
208 /* Make uses of freed and uninitialized memory known. */
209 mallopt (M_PERTURB, 42);
211 #ifdef STDOUT_UNBUFFERED
212 setbuf (stdout, NULL);
213 #endif
215 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
216 switch (opt)
218 case '?':
219 exit (1);
220 case OPT_DIRECT:
221 direct = 1;
222 break;
223 case OPT_TESTDIR:
224 test_dir = optarg;
225 break;
226 #ifdef CMDLINE_PROCESS
227 CMDLINE_PROCESS
228 #endif
231 /* If set, read the test TIMEOUTFACTOR value from the environment.
232 This value is used to scale the default test timeout values. */
233 char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
234 if (envstr_timeoutfactor != NULL)
236 char *envstr_conv = envstr_timeoutfactor;
237 unsigned long int env_fact;
239 env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
240 if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
241 timeoutfactor = MAX (env_fact, 1);
244 /* Set TMPDIR to specified test directory. */
245 if (test_dir != NULL)
247 setenv ("TMPDIR", test_dir, 1);
249 if (chdir (test_dir) < 0)
251 perror ("chdir");
252 exit (1);
255 else
257 test_dir = getenv ("TMPDIR");
258 if (test_dir == NULL || test_dir[0] == '\0')
259 test_dir = "/tmp";
262 /* Make sure we see all message, even those on stdout. */
263 setvbuf (stdout, NULL, _IONBF, 0);
265 /* make sure temporary files are deleted. */
266 atexit (delete_temp_files);
268 /* Correct for the possible parameters. */
269 argv[optind - 1] = argv[0];
270 argv += optind - 1;
271 argc -= optind - 1;
273 /* Call the initializing function, if one is available. */
274 #ifdef PREPARE
275 PREPARE (argc, argv);
276 #endif
278 /* If we are not expected to fork run the function immediately. */
279 if (direct)
280 return TEST_FUNCTION;
282 /* Set up the test environment:
283 - prevent core dumps
284 - set up the timer
285 - fork and execute the function. */
287 pid = fork ();
288 if (pid == 0)
290 /* This is the child. */
291 #ifdef RLIMIT_CORE
292 /* Try to avoid dumping core. */
293 struct rlimit core_limit;
294 core_limit.rlim_cur = 0;
295 core_limit.rlim_max = 0;
296 setrlimit (RLIMIT_CORE, &core_limit);
297 #endif
299 #ifdef RLIMIT_DATA
300 /* Try to avoid eating all memory if a test leaks. */
301 struct rlimit data_limit;
302 if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
304 if (TEST_DATA_LIMIT == RLIM_INFINITY)
305 data_limit.rlim_cur = data_limit.rlim_max;
306 else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
307 data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
308 data_limit.rlim_max);
309 if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
310 perror ("setrlimit: RLIMIT_DATA");
312 else
313 perror ("getrlimit: RLIMIT_DATA");
314 #endif
316 /* We put the test process in its own pgrp so that if it bogusly
317 generates any job control signals, they won't hit the whole build. */
318 setpgid (0, 0);
320 /* Execute the test function and exit with the return value. */
321 exit (TEST_FUNCTION);
323 else if (pid < 0)
325 perror ("Cannot fork test program");
326 exit (1);
329 /* Set timeout. */
330 #ifndef TIMEOUT
331 /* Default timeout is two seconds. */
332 # define TIMEOUT 2
333 #endif
334 signal (SIGALRM, signal_handler);
335 alarm (TIMEOUT * timeoutfactor);
337 /* Make sure we clean up if the wrapper gets interrupted. */
338 signal (SIGINT, signal_handler);
340 /* Wait for the regular termination. */
341 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
342 if (termpid == -1)
344 printf ("Waiting for test program failed: %m\n");
345 exit (1);
347 if (termpid != pid)
349 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
350 (long int) pid, (long int) termpid);
351 exit (1);
354 #ifndef EXPECTED_SIGNAL
355 /* We don't expect any signal. */
356 # define EXPECTED_SIGNAL 0
357 #endif
358 if (WTERMSIG (status) != EXPECTED_SIGNAL)
360 if (EXPECTED_SIGNAL != 0)
362 if (WTERMSIG (status) == 0)
363 fprintf (stderr,
364 "Expected signal '%s' from child, got none\n",
365 strsignal (EXPECTED_SIGNAL));
366 else
367 fprintf (stderr,
368 "Incorrect signal from child: got `%s', need `%s'\n",
369 strsignal (WTERMSIG (status)),
370 strsignal (EXPECTED_SIGNAL));
372 else
373 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
374 strsignal (WTERMSIG (status)));
375 exit (1);
378 /* Simply exit with the return value of the test. */
379 #ifndef EXPECTED_STATUS
380 return WEXITSTATUS (status);
381 #else
382 if (WEXITSTATUS (status) != EXPECTED_STATUS)
384 fprintf (stderr, "Expected status %d, got %d\n",
385 EXPECTED_STATUS, WEXITSTATUS (status));
386 exit (1);
389 return 0;
390 #endif