lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2000, 2002 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>. |
| 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, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <limits.h> |
| 22 | #include <malloc.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | static int errors = 0; |
| 27 | |
| 28 | /* Number of samples per size. */ |
| 29 | #define N 50000 |
| 30 | |
| 31 | |
| 32 | static void |
| 33 | fixed_test (int size) |
| 34 | { |
| 35 | char *ptrs[N]; |
| 36 | int i; |
| 37 | |
| 38 | for (i = 0; i < N; ++i) |
| 39 | { |
| 40 | int j; |
| 41 | |
| 42 | ptrs[i] = (char *) calloc (1, size); |
| 43 | |
| 44 | if (ptrs[i] == NULL) |
| 45 | break; |
| 46 | |
| 47 | for (j = 0; j < size; ++j) |
| 48 | { |
| 49 | if (ptrs[i][j] != '\0') { |
| 50 | ++errors; |
| 51 | printf("byte not cleared (size %d, element %d, byte %d)", |
| 52 | size, i, j); |
| 53 | } |
| 54 | ptrs[i][j] = '\xff'; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | while (i-- > 0) |
| 59 | free (ptrs[i]); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void |
| 64 | random_test (void) |
| 65 | { |
| 66 | char *ptrs[N]; |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < N; ++i) |
| 70 | { |
| 71 | int j; |
| 72 | int n = 1 + random () % 10; |
| 73 | int elem = 1 + random () % 100; |
| 74 | int size = n * elem; |
| 75 | |
| 76 | ptrs[i] = (char *) calloc (n, elem); |
| 77 | |
| 78 | if (ptrs[i] == NULL) |
| 79 | break; |
| 80 | |
| 81 | for (j = 0; j < size; ++j) |
| 82 | { |
| 83 | if (ptrs[i][j] != '\0') { |
| 84 | ++errors; |
| 85 | printf("byte not cleared (size %d, element %d, byte %d)", |
| 86 | size, i, j); |
| 87 | } |
| 88 | ptrs[i][j] = '\xff'; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | while (i-- > 0) |
| 93 | free (ptrs[i]); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static void |
| 98 | null_test (void) |
| 99 | { |
| 100 | /* If the size is 0 the result is implementation defined. Just make |
| 101 | sure the program doesn't crash. */ |
| 102 | calloc (0, 0); |
| 103 | calloc (0, UINT_MAX); |
| 104 | calloc (UINT_MAX, 0); |
| 105 | calloc (0, ~((size_t) 0)); |
| 106 | calloc (~((size_t) 0), 0); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | int |
| 111 | main (void) |
| 112 | { |
| 113 | /* We are allocating blocks with `calloc' and check whether every |
| 114 | block is completely cleared. We first try this for some fixed |
| 115 | times and then with random size. */ |
| 116 | fixed_test (15); |
| 117 | fixed_test (5); |
| 118 | fixed_test (17); |
| 119 | fixed_test (6); |
| 120 | fixed_test (31); |
| 121 | fixed_test (96); |
| 122 | |
| 123 | random_test (); |
| 124 | |
| 125 | null_test (); |
| 126 | |
| 127 | return errors != 0; |
| 128 | } |