]>
Commit | Line | Data |
---|---|---|
678f9854 MT |
1 | From 23797fd0b0908dd2c5775e6135728c7384f4e952 Mon Sep 17 00:00:00 2001 |
2 | From: Thomas Klausner <wiz@NetBSD.org> | |
3 | Date: Fri, 19 May 2017 10:22:44 +0200 | |
4 | Subject: [PATCH] Use reallocarr() when available. | |
5 | ||
6 | Fixes https://github.com/westes/flex/issues/219 | |
7 | --- | |
8 | configure.ac | 1 + | |
9 | src/misc.c | 16 ++++++++++++++-- | |
10 | 2 files changed, 15 insertions(+), 2 deletions(-) | |
11 | ||
12 | diff --git a/configure.ac b/configure.ac | |
13 | index 55e774b0..d0f3b7da 100644 | |
14 | --- a/configure.ac | |
15 | +++ b/configure.ac | |
16 | @@ -166,6 +166,7 @@ strtol dnl | |
17 | AC_CHECK_FUNCS([dnl | |
18 | pow dnl Used only by "examples/manual/expr" | |
19 | setlocale dnl Needed only if NLS is enabled | |
20 | +reallocarr dnl NetBSD function. Use reallocarray if not available. | |
21 | reallocarray dnl OpenBSD function. We have replacement if not available. | |
22 | ]) | |
23 | ||
24 | diff --git a/src/misc.c b/src/misc.c | |
25 | index ef27833c..39483ea8 100644 | |
26 | --- a/src/misc.c | |
27 | +++ b/src/misc.c | |
28 | @@ -142,7 +142,14 @@ void add_action (const char *new_text) | |
29 | void *allocate_array (int size, size_t element_size) | |
30 | { | |
31 | void *mem; | |
32 | -#if HAVE_REALLOCARRAY | |
33 | +#if HAVE_REALLOCARR | |
34 | + mem = NULL; | |
35 | + if (reallocarr(&mem, (size_t) size, element_size)) | |
36 | + flexfatal (_ | |
37 | + ("memory allocation failed in allocate_array()")); | |
38 | + | |
39 | + return mem; | |
40 | +#elif HAVE_REALLOCARRAY | |
41 | /* reallocarray has built-in overflow detection */ | |
42 | mem = reallocarray(NULL, (size_t) size, element_size); | |
43 | #else | |
44 | @@ -659,7 +666,12 @@ char *readable_form (int c) | |
45 | void *reallocate_array (void *array, int size, size_t element_size) | |
46 | { | |
47 | void *new_array; | |
48 | -#if HAVE_REALLOCARRAY | |
49 | +#if HAVE_REALLOCARR | |
50 | + if (reallocarr(&array, (size_t) size, element_size)) | |
51 | + flexfatal (_("attempt to increase array size failed")); | |
52 | + | |
53 | + return array; | |
54 | +#elif HAVE_REALLOCARRAY | |
55 | /* reallocarray has built-in overflow detection */ | |
56 | new_array = reallocarray(array, (size_t) size, element_size); | |
57 | #else |