Remove 'grp' and merge into 'nss' and 'posix'
[platform/upstream/glibc.git] / stdlib / tst-realpath.c
1 /* Test to verify that realpath() doesn't cause false positives due
2    to GCC attribute malloc.
3
4    Test failure exposes the presence of the attribute in the following
5    declaration:
6
7    __attribute__ ((__malloc__ (free, 1))) char*
8    realpath (const char *, char *);
9
10    Copyright (C) 2021-2023 Free Software Foundation, Inc.
11    This file is part of the GNU C Library.
12
13    The GNU C Library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 2.1 of the License, or (at your option) any later version.
17
18    The GNU C Library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22
23    You should have received a copy of the GNU Lesser General Public
24    License along with the GNU C Library; if not, see
25    <https://www.gnu.org/licenses/>.  */
26
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31
32 #if defined __GNUC__ && __GNUC__ >= 11
33 /* Turn GCC -Wmismatched-dealloc warnings into errors to expose false
34    positives.  */
35 #  pragma GCC diagnostic push
36 #  pragma GCC diagnostic error "-Wmismatched-dealloc"
37
38 /* Associate dealloc as the only deallocator suitable for pointers
39    returned from alloc.
40    GCC automatically disables inlining of allocator and deallocator
41    functions marked with the argument form of attribute malloc but
42    it doesn't hurt to disable it explicitly.  */
43 __attribute  ((noipa)) void dealloc (void *);
44 __attribute ((malloc (dealloc, 1))) char* alloc (void);
45 #endif
46
47 void dealloc (void *p)
48 {
49   free (p);
50 }
51
52 char* alloc (void)
53 {
54 #ifdef PATH_MAX
55   return (char *)malloc (PATH_MAX);
56 #else
57   return (char *)malloc (4096);
58 #endif
59 }
60
61 static int
62 do_test (void)
63 {
64   char *resolved_path = alloc ();
65   char *ret = realpath ("/", resolved_path);
66   dealloc (ret);
67
68   resolved_path = alloc ();
69   ret = realpath ("/", resolved_path);
70   dealloc (resolved_path);
71
72   /* The following should emit a warning (but doesn't with GCC 11):
73      resolved_path = alloc ();
74      ret = realpath ("/", resolved_path);
75      free (ret);   // expect -Wmismatched-dealloc
76   */
77
78   return 0;
79 }
80
81 #if defined __GNUC__ && __GNUC__ >= 11
82 /* Restore -Wmismatched-dealloc setting.  */
83 #  pragma GCC diagnostic pop
84 #endif
85
86 #define TEST_FUNCTION do_test ()
87 #include "../test-skeleton.c"