]> git.ipfire.org Git - thirdparty/glibc.git/blame - misc/bug18240.c
Improve check against integer wraparound in hcreate_r [BZ #18240]
[thirdparty/glibc.git] / misc / bug18240.c
CommitLineData
bae7c7c7
FW
1/* Test integer wraparound in hcreate.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include <errno.h>
20#include <limits.h>
21#include <search.h>
22#include <stdbool.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26static void
27test_size (size_t size)
28{
29 int res = hcreate (size);
30 if (res == 0)
31 {
32 if (errno == ENOMEM)
33 return;
34 printf ("error: hcreate (%zu): %m\n", size);
35 exit (1);
36 }
37 char *keys[100];
38 for (int i = 0; i < 100; ++i)
39 {
40 if (asprintf (keys + i, "%d", i) < 0)
41 {
42 printf ("error: asprintf: %m\n");
43 exit (1);
44 }
45 ENTRY e = { keys[i], (char *) "value" };
46 if (hsearch (e, ENTER) == NULL)
47 {
48 printf ("error: hsearch (\"%s\"): %m\n", keys[i]);
49 exit (1);
50 }
51 }
52 hdestroy ();
53
54 for (int i = 0; i < 100; ++i)
55 free (keys[i]);
56}
57
58static int
59do_test (void)
60{
61 test_size (500);
62 test_size (-1);
63 test_size (-3);
64 test_size (INT_MAX - 2);
65 test_size (INT_MAX - 1);
66 test_size (INT_MAX);
67 test_size (((unsigned) INT_MAX) + 1);
68 test_size (UINT_MAX - 2);
69 test_size (UINT_MAX - 1);
70 test_size (UINT_MAX);
71 return 0;
72}
73
74#define TEST_FUNCTION do_test ()
75#include "../test-skeleton.c"