diff mbox

libgo patch committed: Fix append of slice with zero-sized elements

Message ID mcr61tfdyhj.fsf@iant-glaptop.roam.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor Oct. 2, 2013, 11:50 p.m. UTC
This patch to libgo fixes using append with a slice of elements of zero
size.  Previously this would get a division by zero error.  I also fixed
the code to use the correct type--intgo rather than int--and to handle
overflow in the initial allocation calculation.  Bootstrapped and ran Go
testsuite on x86_64-unknown-linux-gnu.  Committed to mainline and 4.8
branch.

Ian
diff mbox

Patch

diff -r 1207eb63dfa9 libgo/runtime/go-append.c
--- a/libgo/runtime/go-append.c	Wed Oct 02 12:18:05 2013 -0700
+++ b/libgo/runtime/go-append.c	Wed Oct 02 16:44:19 2013 -0700
@@ -24,24 +24,24 @@ 
 	     uintptr_t element_size)
 {
   uintptr_t ucount;
-  int count;
+  intgo count;
 
   if (bvalues == NULL || bcount == 0)
     return a;
 
   ucount = (uintptr_t) a.__count + bcount;
-  count = (int) ucount;
+  count = (intgo) ucount;
   if ((uintptr_t) count != ucount || count <= a.__count)
     runtime_panicstring ("append: slice overflow");
 
   if (count > a.__capacity)
     {
-      int m;
+      intgo m;
       void *n;
 
       m = a.__capacity;
-      if (m == 0)
-	m = (int) bcount;
+      if (m + m < count)
+	m = count;
       else
 	{
 	  do
@@ -54,7 +54,7 @@ 
 	  while (m < count);
 	}
 
-      if ((uintptr) m > MaxMem / element_size)
+      if (element_size > 0 && (uintptr) m > MaxMem / element_size)
 	runtime_panicstring ("growslice: cap out of range");
 
       n = __go_alloc (m * element_size);