summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>2014-09-01 11:36:04 +0000
committerjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>2014-09-01 11:36:04 +0000
commit72695711bad6c10cefb554d603dc55e7666c553c (patch)
treec3619bd02368436546f1ce8ec609f781de991f48
parenta41ed05a442de10d4cd793791fecb37cc3203ad8 (diff)
downloadppe42-gcc-72695711bad6c10cefb554d603dc55e7666c553c.tar.gz
ppe42-gcc-72695711bad6c10cefb554d603dc55e7666c553c.zip
Backported from mainline
2014-08-04 Jakub Jelinek <jakub@redhat.com> * runtime/memory.c (xmallocarray): Avoid division for the common case. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-4_9-branch@214788 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--libgfortran/ChangeLog7
-rw-r--r--libgfortran/runtime/memory.c4
2 files changed, 10 insertions, 1 deletions
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index e0a003cb23e..a48a7d3014e 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,10 @@
+2014-09-01 Jakub Jelinek <jakub@redhat.com>
+
+ Backported from mainline
+ 2014-08-04 Jakub Jelinek <jakub@redhat.com>
+
+ * runtime/memory.c (xmallocarray): Avoid division for the common case.
+
2014-08-20 Steven G. Kargl <kargl@gcc.gnu.org>
PR libgfortran/62188
diff --git a/libgfortran/runtime/memory.c b/libgfortran/runtime/memory.c
index c1e735894a5..d3b77de4b87 100644
--- a/libgfortran/runtime/memory.c
+++ b/libgfortran/runtime/memory.c
@@ -56,7 +56,9 @@ xmallocarray (size_t nmemb, size_t size)
if (!nmemb || !size)
size = nmemb = 1;
- else if (nmemb > SIZE_MAX / size)
+#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)
{
errno = ENOMEM;
os_error ("Integer overflow in xmallocarray");
OpenPOWER on IntegriCloud