diff options
Diffstat (limited to 'gcc-4.9/libgfortran/runtime')
-rw-r--r-- | gcc-4.9/libgfortran/runtime/convert_char.c | 4 | ||||
-rw-r--r-- | gcc-4.9/libgfortran/runtime/environ.c | 2 | ||||
-rw-r--r-- | gcc-4.9/libgfortran/runtime/in_pack_generic.c | 2 | ||||
-rw-r--r-- | gcc-4.9/libgfortran/runtime/memory.c | 29 |
4 files changed, 32 insertions, 5 deletions
diff --git a/gcc-4.9/libgfortran/runtime/convert_char.c b/gcc-4.9/libgfortran/runtime/convert_char.c index aa819912a..c3cd1c28c 100644 --- a/gcc-4.9/libgfortran/runtime/convert_char.c +++ b/gcc-4.9/libgfortran/runtime/convert_char.c @@ -44,7 +44,7 @@ convert_char1_to_char4 (gfc_char4_t **dst, gfc_charlen_type len, gfc_charlen_type i, l; l = len > 0 ? len : 0; - *dst = xmalloc ((l + 1) * sizeof (gfc_char4_t)); + *dst = xmallocarray ((l + 1), sizeof (gfc_char4_t)); for (i = 0; i < l; i++) (*dst)[i] = src[i]; @@ -60,7 +60,7 @@ convert_char4_to_char1 (unsigned char **dst, gfc_charlen_type len, gfc_charlen_type i, l; l = len > 0 ? len : 0; - *dst = xmalloc ((l + 1) * sizeof (unsigned char)); + *dst = xmalloc (l + 1); for (i = 0; i < l; i++) (*dst)[i] = src[i]; diff --git a/gcc-4.9/libgfortran/runtime/environ.c b/gcc-4.9/libgfortran/runtime/environ.c index 0c0e9308f..1095f443a 100644 --- a/gcc-4.9/libgfortran/runtime/environ.c +++ b/gcc-4.9/libgfortran/runtime/environ.c @@ -837,7 +837,7 @@ void init_unformatted (variable * v) } else { - elist = xmalloc (unit_count * sizeof (exception_t)); + elist = xmallocarray (unit_count, sizeof (exception_t)); do_count = 0; p = val; do_parse (); diff --git a/gcc-4.9/libgfortran/runtime/in_pack_generic.c b/gcc-4.9/libgfortran/runtime/in_pack_generic.c index 1b8c55843..aab155df6 100644 --- a/gcc-4.9/libgfortran/runtime/in_pack_generic.c +++ b/gcc-4.9/libgfortran/runtime/in_pack_generic.c @@ -180,7 +180,7 @@ internal_pack (gfc_array_char * source) return source->base_addr; /* Allocate storage for the destination. */ - destptr = xmalloc (ssize * size); + destptr = xmallocarray (ssize, size); dest = (char *)destptr; src = source->base_addr; stride0 = stride[0] * size; diff --git a/gcc-4.9/libgfortran/runtime/memory.c b/gcc-4.9/libgfortran/runtime/memory.c index efeea86f1..c1e735894 100644 --- a/gcc-4.9/libgfortran/runtime/memory.c +++ b/gcc-4.9/libgfortran/runtime/memory.c @@ -25,6 +25,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include "libgfortran.h" #include <stdlib.h> +#include <errno.h> + +#ifndef SIZE_MAX +#define SIZE_MAX ((size_t)-1) +#endif void * @@ -44,12 +49,34 @@ xmalloc (size_t n) } +void * +xmallocarray (size_t nmemb, size_t size) +{ + void *p; + + if (!nmemb || !size) + size = nmemb = 1; + else if (nmemb > SIZE_MAX / size) + { + errno = ENOMEM; + os_error ("Integer overflow in xmallocarray"); + } + + p = malloc (nmemb * size); + + if (!p) + os_error ("Memory allocation failed in xmallocarray"); + + return p; +} + + /* calloc wrapper that aborts on error. */ void * xcalloc (size_t nmemb, size_t size) { - if (nmemb * size == 0) + if (!nmemb || !size) nmemb = size = 1; void *p = calloc (nmemb, size); |