libgfortran: Use __builtin_mul_overflow in xmallocarray

Message ID 20190614100848.31732-1-blomqvist.janne@gmail.com
State New
Headers show
Series
  • libgfortran: Use __builtin_mul_overflow in xmallocarray
Related show

Commit Message

Janne Blomqvist June 14, 2019, 10:08 a.m.
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, Ok for trunk?

libgfortran/ChangeLog:

2019-06-14  Janne Blomqvist  <jb@gcc.gnu.org>

	* runtime/memory.c (SIZE_MAX):Remove macro definition.
	(xmallocarray): Use __builtin_mul_overflow.
---
 libgfortran/runtime/memory.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

-- 
2.17.1

Comments

Steve Kargl June 14, 2019, 3:07 p.m. | #1
On Fri, Jun 14, 2019 at 01:08:48PM +0300, Janne Blomqvist wrote:
> 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, Ok for trunk?

> 


OK

-- 
Steve
Steve Kargl June 14, 2019, 11:23 p.m. | #2
On Fri, Jun 14, 2019 at 08:07:36AM -0700, Steve Kargl wrote:
> On Fri, Jun 14, 2019 at 01:08:48PM +0300, Janne Blomqvist wrote:

> > 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, Ok for trunk?

> > 

> 

> OK

> 


Just found

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65921

I assume that you close the PR.

-- 
Steve
Janne Blomqvist June 16, 2019, 7:36 p.m. | #3
On Sat, Jun 15, 2019 at 2:23 AM Steve Kargl
<sgk@troutmask.apl.washington.edu> wrote:
>

> On Fri, Jun 14, 2019 at 08:07:36AM -0700, Steve Kargl wrote:

> > On Fri, Jun 14, 2019 at 01:08:48PM +0300, Janne Blomqvist wrote:

> > > 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, Ok for trunk?

> > >

> >

> > OK

> >

>

> Just found

>

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65921

>

> I assume that you close the PR.


Oh, I had completely forgotten that I filed that one!

(I won't close it yet as the frontend part is still to be done)


-- 
Janne Blomqvist

Patch

diff --git a/libgfortran/runtime/memory.c b/libgfortran/runtime/memory.c
index 1a3d33b59dd..09a4ff8733f 100644
--- a/libgfortran/runtime/memory.c
+++ b/libgfortran/runtime/memory.c
@@ -26,10 +26,6 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include "libgfortran.h"
 #include <errno.h>
 
-#ifndef SIZE_MAX
-#define SIZE_MAX ((size_t)-1)
-#endif
-
 
 void *
 xmalloc (size_t n)
@@ -52,18 +48,17 @@  void *
 xmallocarray (size_t nmemb, size_t size)
 {
   void *p;
+  size_t prod;
 
   if (!nmemb || !size)
-    size = nmemb = 1;
-#define HALF_SIZE_T (((size_t) 1) << (__CHAR_BIT__ * sizeof (size_t) / 2))
-  else if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
-	   && nmemb > SIZE_MAX / size)
+    prod = 1;
+  else if (__builtin_mul_overflow (nmemb, size, &prod))
     {
       errno = ENOMEM;
       os_error ("Integer overflow in xmallocarray");
     }
 
-  p = malloc (nmemb * size);
+  p = malloc (prod);
 
   if (!p)
     os_error ("Memory allocation failed in xmallocarray");