Updated to fedora-glibc-20050620T1530
[glibc.git] / debug / tst-chk1.c
bloba9a7761f9e7f9c30c44b4a2029d0f59ffb4540f6
1 /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* Hack: make sure GCC doesn't know __chk_fail () will not return. */
21 #define __noreturn__
23 #include <fcntl.h>
24 #include <paths.h>
25 #include <setjmp.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <unistd.h>
34 char *temp_filename;
35 static void do_prepare (void);
36 static int do_test (void);
37 #define PREPARE(argc, argv) do_prepare ()
38 #define TEST_FUNCTION do_test ()
39 #include "../test-skeleton.c"
41 static void
42 do_prepare (void)
44 int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
45 if (temp_fd == -1)
47 printf ("cannot create temporary file: %m\n");
48 exit (1);
51 const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
52 if (write (temp_fd, strs, strlen (strs)) != strlen (strs))
54 puts ("could not write test strings into file");
55 unlink (temp_filename);
56 exit (1);
60 volatile int chk_fail_ok;
61 volatile int ret;
62 jmp_buf chk_fail_buf;
64 static void
65 handler (int sig)
67 if (chk_fail_ok)
69 chk_fail_ok = 0;
70 longjmp (chk_fail_buf, 1);
72 else
73 _exit (127);
76 char buf[10];
77 volatile size_t l0;
78 volatile char *p;
79 const char *str1 = "JIHGFEDCBA";
80 const char *str2 = "F";
81 const char *str3 = "%s%n%s%n";
82 const char *str4 = "Hello, ";
83 const char *str5 = "World!\n";
84 char buf2[10] = "%s";
85 int num1 = 67;
86 int num2 = 987654;
88 #define FAIL() \
89 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
90 #define CHK_FAIL_START \
91 chk_fail_ok = 1; \
92 if (! setjmp (chk_fail_buf)) \
94 #define CHK_FAIL_END \
95 chk_fail_ok = 0; \
96 FAIL (); \
98 #if __USE_FORTIFY_LEVEL >= 2
99 #define CHK_FAIL2_START CHK_FAIL_START
100 #define CHK_FAIL2_END CHK_FAIL_END
101 #else
102 #define CHK_FAIL2_START
103 #define CHK_FAIL2_END
104 #endif
106 static int
107 do_test (void)
109 struct sigaction sa;
110 sa.sa_handler = handler;
111 sa.sa_flags = 0;
112 sigemptyset (&sa.sa_mask);
114 sigaction (SIGABRT, &sa, NULL);
116 /* Avoid all the buffer overflow messages on stderr. */
117 int fd = open (_PATH_DEVNULL, O_WRONLY);
118 if (fd == -1)
119 close (STDERR_FILENO);
120 else
122 dup2 (fd, STDERR_FILENO);
123 close (fd);
125 setenv ("LIBC_FATAL_STDERR_", "1", 1);
127 struct A { char buf1[9]; char buf2[1]; } a;
129 printf ("Test checking routines at fortify level %d\n",
130 #ifdef __USE_FORTIFY_LEVEL
131 (int) __USE_FORTIFY_LEVEL
132 #else
134 #endif
137 /* These ops can be done without runtime checking of object size. */
138 memcpy (buf, "abcdefghij", 10);
139 memmove (buf + 1, buf, 9);
140 if (memcmp (buf, "aabcdefghi", 10))
141 FAIL ();
143 if (mempcpy (buf + 5, "abcde", 5) != buf + 10 || memcmp (buf, "aabcdabcde", 10))
144 FAIL ();
146 memset (buf + 8, 'j', 2);
147 if (memcmp (buf, "aabcdabcjj", 10))
148 FAIL ();
150 strcpy (buf + 4, "EDCBA");
151 if (memcmp (buf, "aabcEDCBA", 10))
152 FAIL ();
154 if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
155 FAIL ();
157 strncpy (buf + 6, "X", 4);
158 if (memcmp (buf, "aabcEDX\0\0", 10))
159 FAIL ();
161 if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
162 FAIL ();
164 if (snprintf (buf + 7, 3, "%s", "987654") != 6
165 || memcmp (buf, "aabcEDX98", 10))
166 FAIL ();
168 /* These ops need runtime checking, but shouldn't __chk_fail. */
169 memcpy (buf, "abcdefghij", l0 + 10);
170 memmove (buf + 1, buf, l0 + 9);
171 if (memcmp (buf, "aabcdefghi", 10))
172 FAIL ();
174 if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10 || memcmp (buf, "aabcdabcde", 10))
175 FAIL ();
177 memset (buf + 8, 'j', l0 + 2);
178 if (memcmp (buf, "aabcdabcjj", 10))
179 FAIL ();
181 strcpy (buf + 4, str1 + 5);
182 if (memcmp (buf, "aabcEDCBA", 10))
183 FAIL ();
185 if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
186 FAIL ();
188 strncpy (buf + 6, "X", l0 + 4);
189 if (memcmp (buf, "aabcEDX\0\0", 10))
190 FAIL ();
192 if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEDX67", 10))
193 FAIL ();
195 if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEDX98", 10))
196 FAIL ();
198 buf[l0 + 8] = '\0';
199 strcat (buf, "A");
200 if (memcmp (buf, "aabcEDX9A", 10))
201 FAIL ();
203 buf[l0 + 7] = '\0';
204 strncat (buf, "ZYXWV", l0 + 2);
205 if (memcmp (buf, "aabcEDXZY", 10))
206 FAIL ();
208 memcpy (a.buf1, "abcdefghij", l0 + 10);
209 memmove (a.buf1 + 1, a.buf1, l0 + 9);
210 if (memcmp (a.buf1, "aabcdefghi", 10))
211 FAIL ();
213 if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
214 || memcmp (a.buf1, "aabcdabcde", 10))
215 FAIL ();
217 memset (a.buf1 + 8, 'j', l0 + 2);
218 if (memcmp (a.buf1, "aabcdabcjj", 10))
219 FAIL ();
221 #if __USE_FORTIFY_LEVEL < 2 || !__GNUC_PREREQ (4, 0)
222 /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
223 and sufficient GCC support, as the string operations overflow
224 from a.buf1 into a.buf2. */
225 strcpy (a.buf1 + 4, str1 + 5);
226 if (memcmp (a.buf1, "aabcEDCBA", 10))
227 FAIL ();
229 if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9 || memcmp (a.buf1, "aabcEDCBF", 10))
230 FAIL ();
232 strncpy (a.buf1 + 6, "X", l0 + 4);
233 if (memcmp (a.buf1, "aabcEDX\0\0", 10))
234 FAIL ();
236 if (sprintf (a.buf1 + 7, "%d", num1) != 2 || memcmp (a.buf1, "aabcEDX67", 10))
237 FAIL ();
239 if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
240 || memcmp (a.buf1, "aabcEDX98", 10))
241 FAIL ();
243 a.buf1[l0 + 8] = '\0';
244 strcat (a.buf1, "A");
245 if (memcmp (a.buf1, "aabcEDX9A", 10))
246 FAIL ();
248 a.buf1[l0 + 7] = '\0';
249 strncat (a.buf1, "ZYXWV", l0 + 2);
250 if (memcmp (a.buf1, "aabcEDXZY", 10))
251 FAIL ();
253 #endif
255 #if __USE_FORTIFY_LEVEL >= 1
256 /* Now check if all buffer overflows are caught at runtime. */
258 CHK_FAIL_START
259 memcpy (buf + 1, "abcdefghij", l0 + 10);
260 CHK_FAIL_END
262 CHK_FAIL_START
263 memmove (buf + 2, buf + 1, l0 + 9);
264 CHK_FAIL_END
266 CHK_FAIL_START
267 p = mempcpy (buf + 6, "abcde", l0 + 5);
268 CHK_FAIL_END
270 CHK_FAIL_START
271 memset (buf + 9, 'j', l0 + 2);
272 CHK_FAIL_END
274 CHK_FAIL_START
275 strcpy (buf + 5, str1 + 5);
276 CHK_FAIL_END
278 CHK_FAIL_START
279 p = stpcpy (buf + 9, str2);
280 CHK_FAIL_END
282 CHK_FAIL_START
283 strncpy (buf + 7, "X", l0 + 4);
284 CHK_FAIL_END
286 CHK_FAIL_START
287 sprintf (buf + 8, "%d", num1);
288 CHK_FAIL_END
290 CHK_FAIL_START
291 snprintf (buf + 8, l0 + 3, "%d", num2);
292 CHK_FAIL_END
294 memcpy (buf, str1 + 2, l0 + 9);
295 CHK_FAIL_START
296 strcat (buf, "AB");
297 CHK_FAIL_END
299 memcpy (buf, str1 + 3, l0 + 8);
300 CHK_FAIL_START
301 strncat (buf, "ZYXWV", l0 + 3);
302 CHK_FAIL_END
304 CHK_FAIL_START
305 memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
306 CHK_FAIL_END
308 CHK_FAIL_START
309 memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
310 CHK_FAIL_END
312 CHK_FAIL_START
313 p = mempcpy (a.buf1 + 6, "abcde", l0 + 5);
314 CHK_FAIL_END
316 CHK_FAIL_START
317 memset (a.buf1 + 9, 'j', l0 + 2);
318 CHK_FAIL_END
320 #if __USE_FORTIFY_LEVEL >= 2 && __GNUC_PREREQ (4, 0)
321 # define O 0
322 #else
323 # define O 1
324 #endif
326 CHK_FAIL_START
327 strcpy (a.buf1 + (O + 4), str1 + 5);
328 CHK_FAIL_END
330 CHK_FAIL_START
331 p = stpcpy (a.buf1 + (O + 8), str2);
332 CHK_FAIL_END
334 CHK_FAIL_START
335 strncpy (a.buf1 + (O + 6), "X", l0 + 4);
336 CHK_FAIL_END
338 CHK_FAIL_START
339 sprintf (a.buf1 + (O + 7), "%d", num1);
340 CHK_FAIL_END
342 CHK_FAIL_START
343 snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
344 CHK_FAIL_END
346 memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
347 CHK_FAIL_START
348 strcat (a.buf1, "AB");
349 CHK_FAIL_END
351 memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
352 CHK_FAIL_START
353 strncat (a.buf1, "ZYXWV", l0 + 3);
354 CHK_FAIL_END
355 #endif
357 /* Now checks for %n protection. */
359 /* Constant literals passed directly are always ok
360 (even with warnings about possible bugs from GCC). */
361 int n1, n2;
362 if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
363 || n1 != 1 || n2 != 2)
364 FAIL ();
366 /* In this case the format string is not known at compile time,
367 but resides in read-only memory, so is ok. */
368 if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
369 || n1 != 1 || n2 != 2)
370 FAIL ();
372 strcpy (buf2 + 2, "%n%s%n");
373 /* When the format string is writable and contains %n,
374 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
375 CHK_FAIL2_START
376 if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
377 FAIL ();
378 CHK_FAIL2_END
380 CHK_FAIL2_START
381 if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
382 FAIL ();
383 CHK_FAIL2_END
385 /* But if there is no %n, even writable format string
386 should work. */
387 buf2[6] = '\0';
388 if (sprintf (buf, buf2 + 4, str2) != 1)
389 FAIL ();
391 /* Constant literals passed directly are always ok
392 (even with warnings about possible bugs from GCC). */
393 if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
394 || n1 != 7 || n2 != 14)
395 FAIL ();
397 /* In this case the format string is not known at compile time,
398 but resides in read-only memory, so is ok. */
399 if (printf (str3, str4, &n1, str5, &n2) != 14
400 || n1 != 7 || n2 != 14)
401 FAIL ();
403 strcpy (buf2 + 2, "%n%s%n");
404 /* When the format string is writable and contains %n,
405 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
406 CHK_FAIL2_START
407 if (printf (buf2, str4, &n1, str5, &n1) != 14)
408 FAIL ();
409 CHK_FAIL2_END
411 /* But if there is no %n, even writable format string
412 should work. */
413 buf2[6] = '\0';
414 if (printf (buf2 + 4, str5) != 7)
415 FAIL ();
417 FILE *fp = stdout;
419 /* Constant literals passed directly are always ok
420 (even with warnings about possible bugs from GCC). */
421 if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
422 || n1 != 7 || n2 != 14)
423 FAIL ();
425 /* In this case the format string is not known at compile time,
426 but resides in read-only memory, so is ok. */
427 if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
428 || n1 != 7 || n2 != 14)
429 FAIL ();
431 strcpy (buf2 + 2, "%n%s%n");
432 /* When the format string is writable and contains %n,
433 with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
434 CHK_FAIL2_START
435 if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
436 FAIL ();
437 CHK_FAIL2_END
439 /* But if there is no %n, even writable format string
440 should work. */
441 buf2[6] = '\0';
442 if (fprintf (fp, buf2 + 4, str5) != 7)
443 FAIL ();
445 if (freopen (temp_filename, "r", stdin) == NULL)
447 puts ("could not open temporary file");
448 exit (1);
451 if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
452 FAIL ();
453 if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
454 FAIL ();
456 #if __USE_FORTIFY_LEVEL >= 1
457 CHK_FAIL_START
458 if (gets (buf) != buf)
459 FAIL ();
460 CHK_FAIL_END
461 #endif
463 rewind (stdin);
465 if (fgets (buf, sizeof (buf), stdin) != buf
466 || memcmp (buf, "abcdefgh\n", 10))
467 FAIL ();
468 if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
469 FAIL ();
471 rewind (stdin);
473 if (fgets (buf, l0 + sizeof (buf), stdin) != buf
474 || memcmp (buf, "abcdefgh\n", 10))
475 FAIL ();
477 #if __USE_FORTIFY_LEVEL >= 1
478 CHK_FAIL_START
479 if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
480 FAIL ();
481 CHK_FAIL_END
483 CHK_FAIL_START
484 if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
485 FAIL ();
486 CHK_FAIL_END
487 #endif
489 rewind (stdin);
491 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
492 || memcmp (buf, "abcdefgh\n", 10))
493 FAIL ();
494 if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
495 || memcmp (buf, "ABCDEFGHI", 10))
496 FAIL ();
498 rewind (stdin);
500 if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
501 || memcmp (buf, "abcdefgh\n", 10))
502 FAIL ();
504 #if __USE_FORTIFY_LEVEL >= 1
505 CHK_FAIL_START
506 if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
507 FAIL ();
508 CHK_FAIL_END
510 CHK_FAIL_START
511 if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
512 FAIL ();
513 CHK_FAIL_END
514 #endif
516 lseek (fileno (stdin), 0, SEEK_SET);
518 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
519 || memcmp (buf, "abcdefgh\n", 9))
520 FAIL ();
521 if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
522 || memcmp (buf, "ABCDEFGHI", 9))
523 FAIL ();
525 lseek (fileno (stdin), 0, SEEK_SET);
527 if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
528 || memcmp (buf, "abcdefgh\n", 9))
529 FAIL ();
531 #if __USE_FORTIFY_LEVEL >= 1
532 CHK_FAIL_START
533 if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
534 FAIL ();
535 CHK_FAIL_END
536 #endif
538 if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
539 != sizeof (buf) - 1
540 || memcmp (buf, "\nABCDEFGH", 9))
541 FAIL ();
542 if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
543 || memcmp (buf, "abcdefgh\n", 9))
544 FAIL ();
545 if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
546 != sizeof (buf) - 1
547 || memcmp (buf, "h\nABCDEFG", 9))
548 FAIL ();
550 #if __USE_FORTIFY_LEVEL >= 1
551 CHK_FAIL_START
552 if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
553 != sizeof (buf) + 1)
554 FAIL ();
555 CHK_FAIL_END
556 #endif
558 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
559 != sizeof (buf) - 1
560 || memcmp (buf, "\nABCDEFGH", 9))
561 FAIL ();
562 if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
563 || memcmp (buf, "abcdefgh\n", 9))
564 FAIL ();
565 if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
566 != sizeof (buf) - 1
567 || memcmp (buf, "h\nABCDEFG", 9))
568 FAIL ();
570 #if __USE_FORTIFY_LEVEL >= 1
571 CHK_FAIL_START
572 if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
573 != sizeof (buf) + 1)
574 FAIL ();
575 CHK_FAIL_END
576 #endif
578 if (freopen (temp_filename, "r", stdin) == NULL)
580 puts ("could not open temporary file");
581 exit (1);
584 if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
586 puts ("could not seek in test file");
587 exit (1);
590 #if __USE_FORTIFY_LEVEL >= 1
591 CHK_FAIL_START
592 if (gets (buf) != buf)
593 FAIL ();
594 CHK_FAIL_END
595 #endif
597 /* Check whether missing N$ formats are detected. */
598 CHK_FAIL2_START
599 printf ("%3$d\n", 1, 2, 3, 4);
600 CHK_FAIL2_END
602 CHK_FAIL2_START
603 fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
604 CHK_FAIL2_END
606 CHK_FAIL2_START
607 sprintf (buf, "%3$d\n", 1, 2, 3, 4);
608 CHK_FAIL2_END
610 CHK_FAIL2_START
611 snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
612 CHK_FAIL2_END
614 int sp[2];
615 if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
616 FAIL ();
617 else
619 const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
620 if (send (sp[0], sendstr, strlen (sendstr), 0) != strlen (sendstr))
621 FAIL ();
623 char recvbuf[12];
624 if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
625 != sizeof recvbuf
626 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
627 FAIL ();
629 if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
630 != sizeof recvbuf - 7
631 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
632 FAIL ();
634 #if __USE_FORTIFY_LEVEL >= 1
635 CHK_FAIL_START
636 if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
637 != sizeof recvbuf)
638 FAIL ();
639 CHK_FAIL_END
641 CHK_FAIL_START
642 if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
643 != sizeof recvbuf - 3)
644 FAIL ();
645 CHK_FAIL_END
646 #endif
648 socklen_t sl;
649 struct sockaddr_un sa_un;
651 sl = sizeof (sa_un);
652 if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK, &sa_un, &sl)
653 != sizeof recvbuf
654 || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
655 FAIL ();
657 sl = sizeof (sa_un);
658 if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
659 &sa_un, &sl) != sizeof recvbuf - 7
660 || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
661 FAIL ();
663 #if __USE_FORTIFY_LEVEL >= 1
664 CHK_FAIL_START
665 sl = sizeof (sa_un);
666 if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK, &sa_un, &sl)
667 != sizeof recvbuf)
668 FAIL ();
669 CHK_FAIL_END
671 CHK_FAIL_START
672 sl = sizeof (sa_un);
673 if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
674 &sa_un, &sl) != sizeof recvbuf - 3)
675 FAIL ();
676 CHK_FAIL_END
677 #endif
679 close (sp[0]);
680 close (sp[1]);
683 char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
684 char *enddir = strchr (fname, '\0');
685 if (mkdtemp (fname) == NULL)
687 printf ("mkdtemp failed: %m\n");
688 return 1;
690 *enddir = '/';
691 if (symlink ("bar", fname) != 0)
692 FAIL ();
694 char readlinkbuf[4];
695 if (readlink (fname, readlinkbuf, 4) != 3
696 || memcmp (readlinkbuf, "bar", 3) != 0)
697 FAIL ();
698 if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
699 || memcmp (readlinkbuf, "bbar", 4) != 0)
700 FAIL ();
702 #if __USE_FORTIFY_LEVEL >= 1
703 CHK_FAIL_START
704 if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
705 FAIL ();
706 CHK_FAIL_END
708 CHK_FAIL_START
709 if (readlink (fname, readlinkbuf + 3, 4) != 3)
710 FAIL ();
711 CHK_FAIL_END
712 #endif
714 char *cwd1 = getcwd (NULL, 0);
715 if (cwd1 == NULL)
716 FAIL ();
718 char *cwd2 = getcwd (NULL, 250);
719 if (cwd2 == NULL)
720 FAIL ();
722 if (cwd1 && cwd2)
724 if (strcmp (cwd1, cwd2) != 0)
725 FAIL ();
727 *enddir = '\0';
728 if (chdir (fname))
729 FAIL ();
731 char *cwd3 = getcwd (NULL, 0);
732 if (cwd3 == NULL)
733 FAIL ();
734 if (strcmp (fname, cwd3) != 0)
735 printf ("getcwd after chdir is '%s' != '%s',"
736 "get{c,}wd tests skipped\n", cwd3, fname);
737 else
739 char getcwdbuf[sizeof fname - 3];
741 char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
742 if (cwd4 != getcwdbuf
743 || strcmp (getcwdbuf, fname) != 0)
744 FAIL ();
746 cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
747 if (cwd4 != getcwdbuf + 1
748 || getcwdbuf[0] != fname[0]
749 || strcmp (getcwdbuf + 1, fname) != 0)
750 FAIL ();
752 #if __USE_FORTIFY_LEVEL >= 1
753 CHK_FAIL_START
754 if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
755 != getcwdbuf + 2)
756 FAIL ();
757 CHK_FAIL_END
759 CHK_FAIL_START
760 if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
761 != getcwdbuf + 2)
762 FAIL ();
763 CHK_FAIL_END
764 #endif
766 if (getwd (getcwdbuf) != getcwdbuf
767 || strcmp (getcwdbuf, fname) != 0)
768 FAIL ();
770 if (getwd (getcwdbuf + 1) != getcwdbuf + 1
771 || strcmp (getcwdbuf + 1, fname) != 0)
772 FAIL ();
774 #if 0 && __USE_FORTIFY_LEVEL >= 1
775 CHK_FAIL_START
776 if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
777 FAIL ();
778 CHK_FAIL_END
779 #endif
782 if (chdir (cwd1) != 0)
783 FAIL ();
784 free (cwd3);
787 free (cwd1);
788 free (cwd2);
789 *enddir = '/';
790 if (unlink (fname) != 0)
791 FAIL ();
793 *enddir = '\0';
794 if (rmdir (fname) != 0)
795 FAIL ();
797 return ret;