Use __builtin_mul_overflow in xmallocarray
As GCC now provides builtins for doing integer overflow checking, lets use it when checking for overflow in xmallocarray. Regtested on x86_64-pc-linux-gnu. libgfortran/ChangeLog: 2019-06-14 Janne Blomqvist <jb@gcc.gnu.org> * runtime/memory.c (SIZE_MAX):Remove macro definition. (xmallocarray): Use __builtin_mul_overflow. From-SVN: r272295
This commit is contained in:
parent
66574c5374
commit
84d38abbc9
|
@ -1,3 +1,8 @@
|
||||||
|
2019-06-14 Janne Blomqvist <jb@gcc.gnu.org>
|
||||||
|
|
||||||
|
* runtime/memory.c (SIZE_MAX):Remove macro definition.
|
||||||
|
(xmallocarray): Use __builtin_mul_overflow.
|
||||||
|
|
||||||
2019-05-22 Jeff Law <law@redhat.com>
|
2019-05-22 Jeff Law <law@redhat.com>
|
||||||
|
|
||||||
PR fortran/89100
|
PR fortran/89100
|
||||||
|
|
|
@ -26,10 +26,6 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
||||||
#include "libgfortran.h"
|
#include "libgfortran.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#ifndef SIZE_MAX
|
|
||||||
#define SIZE_MAX ((size_t)-1)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
xmalloc (size_t n)
|
xmalloc (size_t n)
|
||||||
|
@ -52,18 +48,17 @@ void *
|
||||||
xmallocarray (size_t nmemb, size_t size)
|
xmallocarray (size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
void *p;
|
void *p;
|
||||||
|
size_t prod;
|
||||||
|
|
||||||
if (!nmemb || !size)
|
if (!nmemb || !size)
|
||||||
size = nmemb = 1;
|
prod = 1;
|
||||||
#define HALF_SIZE_T (((size_t) 1) << (__CHAR_BIT__ * sizeof (size_t) / 2))
|
else if (__builtin_mul_overflow (nmemb, size, &prod))
|
||||||
else if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
|
|
||||||
&& nmemb > SIZE_MAX / size)
|
|
||||||
{
|
{
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
os_error ("Integer overflow in xmallocarray");
|
os_error ("Integer overflow in xmallocarray");
|
||||||
}
|
}
|
||||||
|
|
||||||
p = malloc (nmemb * size);
|
p = malloc (prod);
|
||||||
|
|
||||||
if (!p)
|
if (!p)
|
||||||
os_error ("Memory allocation failed in xmallocarray");
|
os_error ("Memory allocation failed in xmallocarray");
|
||||||
|
|
Loading…
Reference in New Issue