]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix memory allocation size overflows (PR69687, patch by Marcel Böhme)
authorMarcel Böhme <boehme.marcel@gmail.com>
Fri, 8 Apr 2016 12:10:21 +0000 (12:10 +0000)
committerBernd Schmidt <bernds@gcc.gnu.org>
Fri, 8 Apr 2016 12:10:21 +0000 (12:10 +0000)
PR c++/69687
* cplus-dem.c: Include <limits.h> if available.
(INT_MAX): Define if necessary.
(remember_type, remember_Ktype, register_Btype, string_need):
Abort if we detect cases where we the size of the allocation would
overflow.

From-SVN: r234829

libiberty/ChangeLog
libiberty/cplus-dem.c

index 8e82a5fd7767833f388be115d45b5aa60fbabb35..2a3435623a8fea345e11797df1ffb66d65b7ea5c 100644 (file)
@@ -1,5 +1,12 @@
 2016-04-08  Marcel Böhme  <boehme.marcel@gmail.com>
 
+       PR c++/69687
+       * cplus-dem.c: Include <limits.h> if available.
+       (INT_MAX): Define if necessary.
+       (remember_type, remember_Ktype, register_Btype, string_need):
+       Abort if we detect cases where we the size of the allocation would
+       overflow.
+
        PR c++/70498
        * cplus-dem.c (gnu_special): Handle case where consume_count returns
        -1.
index abba234f04c29289009fff1cdac2d188b7c5581e..7514e57913c55f30b339d4753161d735ef35e1fd 100644 (file)
@@ -56,6 +56,13 @@ void * malloc ();
 void * realloc ();
 #endif
 
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
+#ifndef INT_MAX
+# define INT_MAX       (int)(((unsigned int) ~0) >> 1)          /* 0x7FFFFFFF */ 
+#endif
+
 #include <demangle.h>
 #undef CURRENT_DEMANGLING_STYLE
 #define CURRENT_DEMANGLING_STYLE work->options
@@ -4261,6 +4268,8 @@ remember_type (struct work_stuff *work, const char *start, int len)
        }
       else
        {
+          if (work -> typevec_size > INT_MAX / 2)
+           xmalloc_failed (INT_MAX);
          work -> typevec_size *= 2;
          work -> typevec
            = XRESIZEVEC (char *, work->typevec, work->typevec_size);
@@ -4288,6 +4297,8 @@ remember_Ktype (struct work_stuff *work, const char *start, int len)
        }
       else
        {
+          if (work -> ksize > INT_MAX / 2)
+           xmalloc_failed (INT_MAX);
          work -> ksize *= 2;
          work -> ktypevec
            = XRESIZEVEC (char *, work->ktypevec, work->ksize);
@@ -4317,6 +4328,8 @@ register_Btype (struct work_stuff *work)
        }
       else
        {
+          if (work -> bsize > INT_MAX / 2)
+           xmalloc_failed (INT_MAX);
          work -> bsize *= 2;
          work -> btypevec
            = XRESIZEVEC (char *, work->btypevec, work->bsize);
@@ -4771,6 +4784,8 @@ string_need (string *s, int n)
   else if (s->e - s->p < n)
     {
       tem = s->p - s->b;
+      if (n > INT_MAX / 2 - tem)
+        xmalloc_failed (INT_MAX); 
       n += tem;
       n *= 2;
       s->b = XRESIZEVEC (char, s->b, n);