29e444
commit 27572ef96a66b61f5a6d81196c05983ab3dc9994
29e444
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
29e444
Date:   Sun Jun 30 20:45:19 2013 +0530
29e444
29e444
    Check for integer overflow
29e444
12745e
diff --git glibc-2.17-c758a686/string/strcoll_l.c glibc-2.17-c758a686/string/strcoll_l.c
29e444
index 1be6874..cbe5962 100644
12745e
--- glibc-2.17-c758a686/string/strcoll_l.c
12745e
+++ glibc-2.17-c758a686/string/strcoll_l.c
29e444
@@ -524,6 +524,14 @@ STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
29e444
   memset (&seq1, 0, sizeof (seq1));
29e444
   seq2 = seq1;
29e444
 
29e444
+  size_t size_max = SIZE_MAX / (sizeof (int32_t) + 1);
29e444
+
29e444
+  /* If the strings are long enough to cause overflow in the size request, then
29e444
+     skip the allocation and proceed with the non-cached routines.  */
29e444
+  if (MIN (s1len, s2len) > size_max
29e444
+      || MAX (s1len, s2len) > size_max - MIN (s1len, s2len))
29e444
+    goto begin_collate;
29e444
+
29e444
   if (! __libc_use_alloca ((s1len + s2len) * (sizeof (int32_t) + 1)))
29e444
     {
29e444
       seq1.idxarr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));
29e444
@@ -546,8 +554,10 @@ STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
29e444
       seq2.rulearr = (unsigned char *) alloca (s2len);
29e444
     }
29e444
 
29e444
-  int rule = 0;
29e444
+  int rule;
29e444
 
29e444
+ begin_collate:
29e444
+  rule = 0;
29e444
   /* Cache values in the first pass and if needed, use them in subsequent
29e444
      passes.  */
29e444
   for (int pass = 0; pass < nrules; ++pass)