1 /* Test integer wraparound in hcreate.
2 Copyright (C) 2016-2023 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 <https://www.gnu.org/licenses/>. */
25 #include <sys/resource.h>
28 test_size (size_t size
)
30 int res
= hcreate (size
);
35 printf ("error: hcreate (%zu): %m\n", size
);
39 for (int i
= 0; i
< 100; ++i
)
41 if (asprintf (keys
+ i
, "%d", i
) < 0)
43 printf ("error: asprintf: %m\n");
46 ENTRY e
= { keys
[i
], (char *) "value" };
47 if (hsearch (e
, ENTER
) == NULL
)
49 printf ("error: hsearch (\"%s\"): %m\n", keys
[i
]);
55 for (int i
= 0; i
< 100; ++i
)
62 /* Limit the size of the process, so that memory allocation will
63 fail without impacting the entire system. */
66 if (getrlimit (RLIMIT_AS
, &limit
) != 0)
68 printf ("getrlimit (RLIMIT_AS) failed: %m\n");
71 long target
= 100 * 1024 * 1024;
72 if (limit
.rlim_cur
== RLIM_INFINITY
|| limit
.rlim_cur
> target
)
74 limit
.rlim_cur
= target
;
75 if (setrlimit (RLIMIT_AS
, &limit
) != 0)
77 printf ("setrlimit (RLIMIT_AS) failed: %m\n");
86 test_size (INT_MAX
- 2);
87 test_size (INT_MAX
- 1);
89 test_size (((unsigned) INT_MAX
) + 1);
90 test_size (UINT_MAX
- 2);
91 test_size (UINT_MAX
- 1);
96 #define TEST_FUNCTION do_test ()
97 #include "../test-skeleton.c"