Fix year 2039 bug for localtime with 64-bit time_t (bug 22639).
[glibc.git] / stdlib / test-bz22786.c
blobe7837f98c19fc4bf8ac1da3a2fa5daa84d277051
1 /* Bug 22786: test for buffer overflow in realpath.
2 Copyright (C) 2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <http://www.gnu.org/licenses/>. */
19 /* This file must be run from within a directory called "stdlib". */
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <support/test-driver.h>
30 #include <libc-diag.h>
32 static int
33 do_test (void)
35 const char dir[] = "bz22786";
36 const char lnk[] = "bz22786/symlink";
38 rmdir (dir);
39 if (mkdir (dir, 0755) != 0 && errno != EEXIST)
41 printf ("mkdir %s: %m\n", dir);
42 return EXIT_FAILURE;
44 if (symlink (".", lnk) != 0 && errno != EEXIST)
46 printf ("symlink (%s, %s): %m\n", dir, lnk);
47 return EXIT_FAILURE;
50 const size_t path_len = (size_t) INT_MAX + 1;
52 DIAG_PUSH_NEEDS_COMMENT;
53 #if __GNUC_PREREQ (7, 0)
54 /* GCC 7 warns about too-large allocations; here we need such
55 allocation to succeed for the test to work. */
56 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
57 #endif
58 char *path = malloc (path_len);
59 DIAG_POP_NEEDS_COMMENT;
61 if (path == NULL)
63 printf ("malloc (%zu): %m\n", path_len);
64 return EXIT_UNSUPPORTED;
67 /* Construct very long path = "bz22786/symlink/aaaa....." */
68 char *p = mempcpy (path, lnk, sizeof (lnk) - 1);
69 *(p++) = '/';
70 memset (p, 'a', path_len - (path - p) - 2);
71 p[path_len - (path - p) - 1] = '\0';
73 /* This call crashes before the fix for bz22786 on 32-bit platforms. */
74 p = realpath (path, NULL);
76 if (p != NULL || errno != ENAMETOOLONG)
78 printf ("realpath: %s (%m)", p);
79 return EXIT_FAILURE;
82 /* Cleanup. */
83 unlink (lnk);
84 rmdir (dir);
86 return 0;
89 #define TEST_FUNCTION do_test
90 #include <support/test-driver.c>