diff options
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 49 |
1 files changed, 30 insertions, 19 deletions
@@ -116,15 +116,14 @@ void add_action (const char *new_text) int len = (int) strlen (new_text); while (len + action_index >= action_size - 10 /* slop */ ) { - int new_size = action_size * 2; - if (new_size <= 0) + if (action_size > INT_MAX / 2) /* Increase just a little, to try to avoid overflow * on 16-bit machines. */ action_size += action_size / 8; else - action_size = new_size; + action_size = action_size * 2; action_array = reallocate_character_array (action_array, @@ -141,20 +140,24 @@ void add_action (const char *new_text) void *allocate_array (int size, size_t element_size) { - void *mem; -#if HAVE_REALLOCARRAY - /* reallocarray has built-in overflow detection */ - mem = reallocarray(NULL, (size_t) size, element_size); + void *new_array; +#if HAVE_REALLOCARR + new_array = NULL; + if (reallocarr(&new_array, (size_t) size, element_size)) + flexfatal (_("memory allocation failed in allocate_array()")); #else +# if HAVE_REALLOCARRAY + new_array = reallocarray(NULL, (size_t) size, element_size); +# else + /* Do manual overflow detection */ size_t num_bytes = (size_t) size * element_size; - mem = (size && SIZE_MAX / (size_t) size < element_size) ? NULL : + new_array = (size && SIZE_MAX / (size_t) size < element_size) ? NULL : malloc(num_bytes); +# endif + if (!new_array) + flexfatal (_("memory allocation failed in allocate_array()")); #endif - if (!mem) - flexfatal (_ - ("memory allocation failed in allocate_array()")); - - return mem; + return new_array; } @@ -659,17 +662,22 @@ char *readable_form (int c) void *reallocate_array (void *array, int size, size_t element_size) { void *new_array; -#if HAVE_REALLOCARRAY - /* reallocarray has built-in overflow detection */ - new_array = reallocarray(array, (size_t) size, element_size); +#if HAVE_REALLOCARR + new_array = array; + if (reallocarr(&new_array, (size_t) size, element_size)) + flexfatal (_("attempt to increase array size failed")); #else +# if HAVE_REALLOCARRAY + new_array = reallocarray(array, (size_t) size, element_size); +# else + /* Do manual overflow detection */ size_t num_bytes = (size_t) size * element_size; new_array = (size && SIZE_MAX / (size_t) size < element_size) ? NULL : realloc(array, num_bytes); -#endif +# endif if (!new_array) flexfatal (_("attempt to increase array size failed")); - +#endif return new_array; } @@ -720,7 +728,10 @@ void skelout (void) */ #define cmd_match(s) (strncmp(buf,(s),strlen(s))==0) - if (buf[1] == '%') { + if (buf[1] == '#') { + /* %# indicates comment line to be ignored */ + } + else if (buf[1] == '%') { /* %% is a break point for skelout() */ return; } |