From af1e9051537d4e462a54367ca2f6544a2838e0f1 Mon Sep 17 00:00:00 2001 From: jb Date: Tue, 17 Jun 2014 03:50:34 +0000 Subject: Introduce xmallocarray, an overflow checking variant of xmalloc. 2014-06-17 Janne Blomqvist * libgfortran.h (xmallocarray): New prototype. * runtime/memory.c (xmallocarray): New function. (xcalloc): Check for nonzero separately instead of multiplying. * generated/*.c: Regenerated. * intrinsics/cshift0.c (cshift0): Call xmallocarray instead of xmalloc. * intrinsics/eoshift0.c (eoshift0): Likewise. * intrinsics/eoshift2.c (eoshift2): Likewise. * intrinsics/pack_generic.c (pack_internal): Likewise. (pack_s_internal): Likewise. * intrinsics/reshape_generic.c (reshape_internal): Likewise. * intrinsics/spread_generic.c (spread_internal): Likewise. (spread_internal_scalar): Likewise. * intrinsics/string_intrinsics_inc.c (string_trim): Likewise. (string_minmax): Likewise. * intrinsics/transpose_generic.c (transpose_internal): Likewise. * intrinsics/unpack_generic.c (unpack_internal): Likewise. * io/list_read.c (nml_touch_nodes): Don't cast xmalloc return value. * io/transfer.c (st_set_nml_var): Call xmallocarray instead of xmalloc. * io/unit.c (get_internal_unit): Likewise. (filename_from_unit): Don't cast xmalloc return value. * io/write.c (nml_write_obj): Likewise, formatting. * m4/bessel.m4 (bessel_jn_r'rtype_kind`): Call xmallocarray instead of xmalloc. (besse_yn_r'rtype_kind`): Likewise. * m4/cshift1.m4 (cshift1): Likewise. * m4/eoshift1.m4 (eoshift1): Likewise. * m4/eoshift3.m4 (eoshift3): Likewise. * m4/iforeach.m4: Likewise. * m4/ifunction.m4: Likewise. * m4/ifunction_logical.m4 (name`'rtype_qual`_'atype_code): Likewise. * m4/in_pack.m4 (internal_pack_'rtype_ccode`): Likewise. * m4/matmul.m4 (matmul_'rtype_code`): Likewise. * m4/matmull.m4 (matmul_'rtype_code`): Likewise. * m4/pack.m4 (pack_'rtype_code`): Likewise. * m4/reshape.m4 (reshape_'rtype_ccode`): Likewise. * m4/shape.m4 (shape_'rtype_kind`): Likewise. * m4/spread.m4 (spread_'rtype_code`): Likewise. (spread_scalar_'rtype_code`): Likewise. * m4/transpose.m4 (transpose_'rtype_code`): Likewise. * m4/unpack.m4 (unpack0_'rtype_code`): Likewise. (unpack1_'rtype_code`): Likewise. * runtime/convert_char.c (convert_char1_to_char4): Likewise. (convert_char4_to_char1): Simplify. * runtime/environ.c (init_unformatted): Call xmallocarray instead of xmalloc. * runtime/in_pack_generic.c (internal_pack): Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@211721 138bc75d-0d04-0410-961f-82ee72b054a4 --- libgfortran/m4/bessel.m4 | 4 ++-- libgfortran/m4/cshift1.m4 | 2 +- libgfortran/m4/eoshift1.m4 | 4 ++-- libgfortran/m4/eoshift3.m4 | 6 +++--- libgfortran/m4/iforeach.m4 | 6 +++--- libgfortran/m4/ifunction.m4 | 15 ++++++--------- libgfortran/m4/ifunction_logical.m4 | 5 ++--- libgfortran/m4/in_pack.m4 | 2 +- libgfortran/m4/matmul.m4 | 2 +- libgfortran/m4/matmull.m4 | 2 +- libgfortran/m4/pack.m4 | 4 ++-- libgfortran/m4/reshape.m4 | 6 +++--- libgfortran/m4/shape.m4 | 2 +- libgfortran/m4/spread.m4 | 6 +++--- libgfortran/m4/transpose.m4 | 3 ++- libgfortran/m4/unpack.m4 | 4 ++-- 16 files changed, 35 insertions(+), 38 deletions(-) (limited to 'libgfortran/m4') diff --git a/libgfortran/m4/bessel.m4 b/libgfortran/m4/bessel.m4 index 15127e55832..36ffd335688 100644 --- a/libgfortran/m4/bessel.m4 +++ b/libgfortran/m4/bessel.m4 @@ -56,7 +56,7 @@ bessel_jn_r'rtype_kind` ('rtype` * const restrict ret, int n1, int n2, 'rtype_na { size_t size = n2 < n1 ? 0 : n2-n1+1; GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1); - ret->base_addr = xmalloc (sizeof ('rtype_name`) * size); + ret->base_addr = xmallocarray (size, sizeof ('rtype_name`)); ret->offset = 0; } @@ -123,7 +123,7 @@ bessel_yn_r'rtype_kind` ('rtype` * const restrict ret, int n1, int n2, { size_t size = n2 < n1 ? 0 : n2-n1+1; GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1); - ret->base_addr = xmalloc (sizeof ('rtype_name`) * size); + ret->base_addr = xmallocarray (size, sizeof ('rtype_name`)); ret->offset = 0; } diff --git a/libgfortran/m4/cshift1.m4 b/libgfortran/m4/cshift1.m4 index 602bf9edb0d..e743e9e90d5 100644 --- a/libgfortran/m4/cshift1.m4 +++ b/libgfortran/m4/cshift1.m4 @@ -81,7 +81,7 @@ cshift1 (gfc_array_char * const restrict ret, { int i; - ret->base_addr = xmalloc (size * arraysize); + ret->base_addr = xmallocarray (arraysize, size); ret->offset = 0; ret->dtype = array->dtype; for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++) diff --git a/libgfortran/m4/eoshift1.m4 b/libgfortran/m4/eoshift1.m4 index c373afe4b6b..df785ecdc20 100644 --- a/libgfortran/m4/eoshift1.m4 +++ b/libgfortran/m4/eoshift1.m4 @@ -106,8 +106,8 @@ eoshift1 (gfc_array_char * const restrict ret, GFC_DIMENSION_SET(ret->dim[i], 0, ub, str); } - /* xmalloc allocates a single byte for zero size. */ - ret->base_addr = xmalloc (size * arraysize); + /* xmallocarray allocates a single byte for zero size. */ + ret->base_addr = xmallocarray (arraysize, size); } else if (unlikely (compile_options.bounds_check)) diff --git a/libgfortran/m4/eoshift3.m4 b/libgfortran/m4/eoshift3.m4 index f1997df36ec..1a257d98f3c 100644 --- a/libgfortran/m4/eoshift3.m4 +++ b/libgfortran/m4/eoshift3.m4 @@ -90,7 +90,7 @@ eoshift3 (gfc_array_char * const restrict ret, { int i; - ret->base_addr = xmalloc (size * arraysize); + ret->base_addr = xmallocarray (arraysize, size); ret->offset = 0; ret->dtype = array->dtype; for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++) @@ -108,8 +108,8 @@ eoshift3 (gfc_array_char * const restrict ret, GFC_DIMENSION_SET(ret->dim[i], 0, ub, str); } - /* xmalloc allocates a single byte for zero size. */ - ret->base_addr = xmalloc (size * arraysize); + /* xmallocarray allocates a single byte for zero size. */ + ret->base_addr = xmallocarray (arraysize, size); } else if (unlikely (compile_options.bounds_check)) diff --git a/libgfortran/m4/iforeach.m4 b/libgfortran/m4/iforeach.m4 index a875a2ac273..2b916af66dd 100644 --- a/libgfortran/m4/iforeach.m4 +++ b/libgfortran/m4/iforeach.m4 @@ -30,7 +30,7 @@ name`'rtype_qual`_'atype_code (rtype * const restrict retarray, GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1); retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1; retarray->offset = 0; - retarray->base_addr = xmalloc (sizeof (rtype_name) * rank); + retarray->base_addr = xmallocarray (rank, sizeof (rtype_name)); } else { @@ -133,7 +133,7 @@ void GFC_DIMENSION_SET(retarray->dim[0], 0, rank - 1, 1); retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1; retarray->offset = 0; - retarray->base_addr = xmalloc (sizeof (rtype_name) * rank); + retarray->base_addr = xmallocarray (rank, sizeof (rtype_name)); } else { @@ -264,7 +264,7 @@ void GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1); retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1; retarray->offset = 0; - retarray->base_addr = xmalloc (sizeof (rtype_name) * rank); + retarray->base_addr = xmallocarray (rank, sizeof (rtype_name)); } else if (unlikely (compile_options.bounds_check)) { diff --git a/libgfortran/m4/ifunction.m4 b/libgfortran/m4/ifunction.m4 index 1555aebbc4a..b4de9a87608 100644 --- a/libgfortran/m4/ifunction.m4 +++ b/libgfortran/m4/ifunction.m4 @@ -85,10 +85,9 @@ name`'rtype_qual`_'atype_code (rtype * const restrict retarray, retarray->offset = 0; retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; - alloc_size = sizeof (rtype_name) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) - * extent[rank-1]; + alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1]; - retarray->base_addr = xmalloc (alloc_size); + retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name)); if (alloc_size == 0) { /* Make sure we have a zero-sized array. */ @@ -260,8 +259,7 @@ void } - alloc_size = sizeof (rtype_name) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) - * extent[rank-1]; + alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1]; retarray->offset = 0; retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; @@ -273,7 +271,7 @@ void return; } else - retarray->base_addr = xmalloc (alloc_size); + retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name)); } else @@ -417,8 +415,7 @@ void retarray->offset = 0; retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; - alloc_size = sizeof (rtype_name) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) - * extent[rank-1]; + alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1]; if (alloc_size == 0) { @@ -427,7 +424,7 @@ void return; } else - retarray->base_addr = xmalloc (alloc_size); + retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name)); } else { diff --git a/libgfortran/m4/ifunction_logical.m4 b/libgfortran/m4/ifunction_logical.m4 index 13dd7a928f3..fd3afb074f8 100644 --- a/libgfortran/m4/ifunction_logical.m4 +++ b/libgfortran/m4/ifunction_logical.m4 @@ -89,8 +89,7 @@ name`'rtype_qual`_'atype_code (rtype * const restrict retarray, retarray->offset = 0; retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank; - alloc_size = sizeof (rtype_name) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1) - * extent[rank-1]; + alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1]; if (alloc_size == 0) { @@ -99,7 +98,7 @@ name`'rtype_qual`_'atype_code (rtype * const restrict retarray, return; } else - retarray->base_addr = xmalloc (alloc_size); + retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name)); } else { diff --git a/libgfortran/m4/in_pack.m4 b/libgfortran/m4/in_pack.m4 index 9d515d42b51..a807e71e5a6 100644 --- a/libgfortran/m4/in_pack.m4 +++ b/libgfortran/m4/in_pack.m4 @@ -79,7 +79,7 @@ internal_pack_'rtype_ccode` ('rtype` * source) return source->base_addr; /* Allocate storage for the destination. */ - destptr = ('rtype_name` *)xmalloc (ssize * sizeof ('rtype_name`)); + destptr = xmallocarray (ssize, sizeof ('rtype_name`)); dest = destptr; src = source->base_addr; stride0 = stride[0]; diff --git a/libgfortran/m4/matmul.m4 b/libgfortran/m4/matmul.m4 index 937daa829a4..e5f43e2d641 100644 --- a/libgfortran/m4/matmul.m4 +++ b/libgfortran/m4/matmul.m4 @@ -125,7 +125,7 @@ matmul_'rtype_code` ('rtype` * const restrict retarray, } retarray->base_addr - = xmalloc (sizeof ('rtype_name`) * size0 ((array_t *) retarray)); + = xmallocarray (size0 ((array_t *) retarray), sizeof ('rtype_name`)); retarray->offset = 0; } else if (unlikely (compile_options.bounds_check)) diff --git a/libgfortran/m4/matmull.m4 b/libgfortran/m4/matmull.m4 index 25bdc75b955..4be37d5a86c 100644 --- a/libgfortran/m4/matmull.m4 +++ b/libgfortran/m4/matmull.m4 @@ -89,7 +89,7 @@ matmul_'rtype_code` ('rtype` * const restrict retarray, } retarray->base_addr - = xmalloc (sizeof ('rtype_name`) * size0 ((array_t *) retarray)); + = xmallocarray (size0 ((array_t *) retarray), sizeof ('rtype_name`)); retarray->offset = 0; } else if (unlikely (compile_options.bounds_check)) diff --git a/libgfortran/m4/pack.m4 b/libgfortran/m4/pack.m4 index 8495de4efca..a5517d07b30 100644 --- a/libgfortran/m4/pack.m4 +++ b/libgfortran/m4/pack.m4 @@ -168,8 +168,8 @@ pack_'rtype_code` ('rtype` *ret, const 'rtype` *array, ret->offset = 0; - /* xmalloc allocates a single byte for zero size. */ - ret->base_addr = xmalloc (sizeof ('rtype_name`) * total); + /* xmallocarray allocates a single byte for zero size. */ + ret->base_addr = xmallocarray (total, sizeof ('rtype_name`)); if (total == 0) return; diff --git a/libgfortran/m4/reshape.m4 b/libgfortran/m4/reshape.m4 index 943f3e93653..6341b6e9a2c 100644 --- a/libgfortran/m4/reshape.m4 +++ b/libgfortran/m4/reshape.m4 @@ -115,11 +115,11 @@ reshape_'rtype_ccode` ('rtype` * const restrict ret, ret->offset = 0; if (unlikely (rs < 1)) - alloc_size = 1; + alloc_size = 0; else - alloc_size = rs * sizeof ('rtype_name`); + alloc_size = rs; - ret->base_addr = xmalloc (alloc_size); + ret->base_addr = xmallocarray (alloc_size, sizeof ('rtype_name`)); ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; } diff --git a/libgfortran/m4/shape.m4 b/libgfortran/m4/shape.m4 index 85dd233d1ca..9d02c6c4029 100644 --- a/libgfortran/m4/shape.m4 +++ b/libgfortran/m4/shape.m4 @@ -50,7 +50,7 @@ shape_'rtype_kind` ('rtype` * const restrict ret, { GFC_DIMENSION_SET(ret->dim[0], 0, rank - 1, 1); ret->offset = 0; - ret->base_addr = xmalloc (sizeof ('rtype_name`) * rank); + ret->base_addr = xmallocarray (rank, sizeof ('rtype_name`)); } stride = GFC_DESCRIPTOR_STRIDE(ret,0); diff --git a/libgfortran/m4/spread.m4 b/libgfortran/m4/spread.m4 index 82ae240e17d..62cfd81dea6 100644 --- a/libgfortran/m4/spread.m4 +++ b/libgfortran/m4/spread.m4 @@ -102,8 +102,8 @@ spread_'rtype_code` ('rtype` *ret, const 'rtype` *source, } ret->offset = 0; - /* xmalloc allocates a single byte for zero size. */ - ret->base_addr = xmalloc (rs * sizeof('rtype_name`)); + /* xmallocarray allocates a single byte for zero size. */ + ret->base_addr = xmallocarray (rs, sizeof('rtype_name`)); if (rs <= 0) return; } @@ -245,7 +245,7 @@ spread_scalar_'rtype_code` ('rtype` *ret, const 'rtype_name` *source, if (ret->base_addr == NULL) { - ret->base_addr = xmalloc (ncopies * sizeof ('rtype_name`)); + ret->base_addr = xmallocarray (ncopies, sizeof ('rtype_name`)); ret->offset = 0; GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1); } diff --git a/libgfortran/m4/transpose.m4 b/libgfortran/m4/transpose.m4 index da4f782b013..6d9f4764c30 100644 --- a/libgfortran/m4/transpose.m4 +++ b/libgfortran/m4/transpose.m4 @@ -61,7 +61,8 @@ transpose_'rtype_code` ('rtype` * const restrict ret, GFC_DIMENSION_SET(ret->dim[1], 0, GFC_DESCRIPTOR_EXTENT(source,0) - 1, GFC_DESCRIPTOR_EXTENT(source, 1)); - ret->base_addr = xmalloc (sizeof ('rtype_name`) * size0 ((array_t *) ret)); + ret->base_addr = xmallocarray (size0 ((array_t *) ret), + sizeof ('rtype_name`)); ret->offset = 0; } else if (unlikely (compile_options.bounds_check)) { diff --git a/libgfortran/m4/unpack.m4 b/libgfortran/m4/unpack.m4 index 4382ac425c4..e945446d804 100644 --- a/libgfortran/m4/unpack.m4 +++ b/libgfortran/m4/unpack.m4 @@ -100,7 +100,7 @@ unpack0_'rtype_code` ('rtype` *ret, const 'rtype` *vector, rs *= extent[n]; } ret->offset = 0; - ret->base_addr = xmalloc (rs * sizeof ('rtype_name`)); + ret->base_addr = xmallocarray (rs, sizeof ('rtype_name`)); } else { @@ -245,7 +245,7 @@ unpack1_'rtype_code` ('rtype` *ret, const 'rtype` *vector, rs *= extent[n]; } ret->offset = 0; - ret->base_addr = xmalloc (rs * sizeof ('rtype_name`)); + ret->base_addr = xmallocarray (rs, sizeof ('rtype_name`)); } else { -- cgit v1.2.1