| 1 |
/* t-malloc.c - Check some malloc functions |
| 2 |
* Copyright (C) 2020 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of Libgpg-error. |
| 5 |
* |
| 6 |
* Libgpg-error is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgpg-error is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
#define PGM "t-malloc" |
| 33 |
#include "t-common.h" |
| 34 |
|
| 35 |
|
| 36 |
static const char * |
| 37 |
my_strusage (int level) |
| 38 |
{ |
| 39 |
const char *p; |
| 40 |
|
| 41 |
switch (level) |
| 42 |
{ |
| 43 |
case 9: p = "LGPL-2.1-or-later"; break; |
| 44 |
case 11: p = PGM; break; |
| 45 |
default: p = NULL; |
| 46 |
} |
| 47 |
return p; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
static void |
| 52 |
check_reallocarray (void) |
| 53 |
{ |
| 54 |
struct foo_s { const char *a; int b; } *array; |
| 55 |
size_t n; |
| 56 |
|
| 57 |
array = gpgrt_calloc (10, sizeof *array); |
| 58 |
if (!array) |
| 59 |
die ("%s: malloc failed\n", __func__); |
| 60 |
|
| 61 |
for (n=0; n < 10; n++) |
| 62 |
if (array[n].a || array[n].b) |
| 63 |
fail ("%s: array not cleared at index %zu\n", __func__, n); |
| 64 |
|
| 65 |
for (n=0; n < 10; n++) |
| 66 |
{ |
| 67 |
array[n].a = "dummy string"; |
| 68 |
array[n].b = 100+n; |
| 69 |
} |
| 70 |
|
| 71 |
array = gpgrt_reallocarray (array, 10, 20, sizeof *array); |
| 72 |
if (!array) |
| 73 |
die ("%s: realloc failed\n", __func__); |
| 74 |
|
| 75 |
for (n=0; n < 10; n++) |
| 76 |
{ |
| 77 |
if (!array[n].a || strcmp (array[n].a, "dummy string")) |
| 78 |
fail ("%s: string in realloced array changed at index %zu\n", |
| 79 |
__func__, n); |
| 80 |
|
| 81 |
if (array[n].b != 100 + n) |
| 82 |
fail ("%s: number in realloced array changed at index %zu\n", |
| 83 |
__func__, n); |
| 84 |
} |
| 85 |
for (n=10; n < 20; n++) |
| 86 |
if (array[n].a || array[n].b) |
| 87 |
fail ("%s: realloced array not cleared at index %zu\n", __func__, n); |
| 88 |
|
| 89 |
/* We can't easily check whether the reallocated array does not |
| 90 |
* iniitialze in the case OLDN is equal or larger to N, so we skip |
| 91 |
* this. Let's do a simple shrink test instead. */ |
| 92 |
|
| 93 |
array = gpgrt_reallocarray (array, 20, 7, sizeof *array); |
| 94 |
if (!array) |
| 95 |
die ("%s: realloc (shrinking) failed\n", __func__); |
| 96 |
|
| 97 |
for (n=0; n < 7; n++) |
| 98 |
{ |
| 99 |
if (!array[n].a || strcmp (array[n].a, "dummy string")) |
| 100 |
fail ("%s: string in shrunk array changed at index %zu\n", |
| 101 |
__func__, n); |
| 102 |
|
| 103 |
if (array[n].b != 100 + n) |
| 104 |
fail ("%s: number in shrunk array changed at index %zu\n", |
| 105 |
__func__, n); |
| 106 |
} |
| 107 |
|
| 108 |
xfree (array); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
int |
| 113 |
main (int argc, char **argv) |
| 114 |
{ |
| 115 |
gpgrt_opt_t opts[] = { |
| 116 |
ARGPARSE_x ('v', "verbose", NONE, 0, "Print more diagnostics"), |
| 117 |
ARGPARSE_s_n('d', "debug", "Flyswatter"), |
| 118 |
ARGPARSE_end() |
| 119 |
}; |
| 120 |
gpgrt_argparse_t pargs = { &argc, &argv, 0 }; |
| 121 |
|
| 122 |
gpgrt_set_strusage (my_strusage); |
| 123 |
gpgrt_log_set_prefix (gpgrt_strusage (11), GPGRT_LOG_WITH_PREFIX); |
| 124 |
|
| 125 |
while (gpgrt_argparse (NULL, &pargs, opts)) |
| 126 |
{ |
| 127 |
switch (pargs.r_opt) |
| 128 |
{ |
| 129 |
case 'v': verbose++; break; |
| 130 |
case 'd': debug++; break; |
| 131 |
default : pargs.err = ARGPARSE_PRINT_ERROR; break; |
| 132 |
} |
| 133 |
} |
| 134 |
gpgrt_argparse (NULL, &pargs, NULL); |
| 135 |
|
| 136 |
show ("testing malloc functions\n"); |
| 137 |
|
| 138 |
check_reallocarray (); |
| 139 |
|
| 140 |
return !!errorcount; |
| 141 |
} |