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". */
28 #include <sys/types.h>
29 #include <support/test-driver.h>
30 #include <libc-diag.h>
35 const char dir
[] = "bz22786";
36 const char lnk
[] = "bz22786/symlink";
39 if (mkdir (dir
, 0755) != 0 && errno
!= EEXIST
)
41 printf ("mkdir %s: %m\n", dir
);
44 if (symlink (".", lnk
) != 0 && errno
!= EEXIST
)
46 printf ("symlink (%s, %s): %m\n", dir
, lnk
);
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=");
58 char *path
= malloc (path_len
);
59 DIAG_POP_NEEDS_COMMENT
;
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);
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
);
89 #define TEST_FUNCTION do_test
90 #include <support/test-driver.c>