Blame SOURCES/glibc-rh788959.patch

b40826
diff -pruN glibc-2.12-2-gc4ccff1/nis/nss_compat/compat-initgroups.c glibc-2.12-2-gc4ccff1.patched/nis/nss_compat/compat-initgroups.c
b40826
--- glibc-2.12-2-gc4ccff1/nis/nss_compat/compat-initgroups.c	2010-05-04 16:57:23.000000000 +0530
b40826
+++ glibc-2.12-2-gc4ccff1.patched/nis/nss_compat/compat-initgroups.c	2012-02-21 11:11:19.877008465 +0530
b40826
@@ -297,6 +297,8 @@ getgrent_next_nss (ent_t *ent, char *buf
b40826
       if (nss_initgroups_dyn (user, group, &mystart, &mysize, &mygroups,
b40826
 			      limit, errnop) == NSS_STATUS_SUCCESS)
b40826
 	{
b40826
+	  status = NSS_STATUS_NOTFOUND;
b40826
+
b40826
 	  /* If there is no blacklist we can trust the underlying
b40826
 	     initgroups implementation.  */
b40826
 	  if (ent->blacklist.current <= 1)
b40826
@@ -309,6 +311,7 @@ getgrent_next_nss (ent_t *ent, char *buf
b40826
 		 overwrite the pointer with one to a bigger buffer.  */
b40826
 	      char *tmpbuf = buffer;
b40826
 	      size_t tmplen = buflen;
b40826
+	      bool use_malloc = false;
b40826
 
b40826
 	      for (int i = 0; i < mystart; i++)
b40826
 		{
b40826
@@ -316,21 +319,36 @@ getgrent_next_nss (ent_t *ent, char *buf
b40826
 						   tmpbuf, tmplen, errnop))
b40826
 			 == NSS_STATUS_TRYAGAIN
b40826
 			 && *errnop == ERANGE)
b40826
-		    if (tmpbuf == buffer)
b40826
-		      {
b40826
-			tmplen *= 2;
b40826
-			tmpbuf = __alloca (tmplen);
b40826
-		      }
b40826
-		    else
b40826
-		      tmpbuf = extend_alloca (tmpbuf, tmplen, 2 * tmplen);
b40826
+                    {
b40826
+                      if (__libc_use_alloca (tmplen * 2))
b40826
+                        {
b40826
+                          if (tmpbuf == buffer)
b40826
+                            {
b40826
+                              tmplen *= 2;
b40826
+                              tmpbuf = __alloca (tmplen);
b40826
+                            }
b40826
+                          else
b40826
+                            tmpbuf = extend_alloca (tmpbuf, tmplen, tmplen * 2);
b40826
+                        }
b40826
+                      else
b40826
+                        {
b40826
+                          tmplen *= 2;
b40826
+                          char *newbuf = realloc (use_malloc ? tmpbuf : NULL, tmplen);
b40826
+                    
b40826
+                          if (newbuf == NULL)
b40826
+                            {
b40826
+                              status = NSS_STATUS_TRYAGAIN;
b40826
+			      goto done;
b40826
+                            }
b40826
+                          use_malloc = true;
b40826
+                          tmpbuf = newbuf;
b40826
+                        }
b40826
+                    }
b40826
 
b40826
 		  if (__builtin_expect  (status != NSS_STATUS_NOTFOUND, 1))
b40826
 		    {
b40826
 		      if (__builtin_expect  (status != NSS_STATUS_SUCCESS, 0))
b40826
-			{
b40826
-			  free (mygroups);
b40826
-			  return status;
b40826
-			}
b40826
+		        goto done;
b40826
 
b40826
 		      if (!in_blacklist (grpbuf.gr_name,
b40826
 					 strlen (grpbuf.gr_name), ent)
b40826
@@ -348,11 +366,17 @@ getgrent_next_nss (ent_t *ent, char *buf
b40826
 			}
b40826
 		    }
b40826
 		}
b40826
+
b40826
+		status = NSS_STATUS_NOTFOUND;
b40826
+
b40826
+ done:
b40826
+		if (use_malloc)
b40826
+		  free (tmpbuf);
b40826
 	    }
b40826
 
b40826
 	  free (mygroups);
b40826
 
b40826
-	  return NSS_STATUS_NOTFOUND;
b40826
+	  return status;
b40826
 	}
b40826
 
b40826
       free (mygroups);
b40826
@@ -506,6 +530,7 @@ _nss_compat_initgroups_dyn (const char *
b40826
   char *tmpbuf;
b40826
   enum nss_status status;
b40826
   ent_t intern = { true, false, false, NULL, {NULL, 0, 0} };
b40826
+  bool use_malloc = false;
b40826
 
b40826
   status = internal_setgrent (&intern;;
b40826
   if (status != NSS_STATUS_SUCCESS)
b40826
@@ -519,13 +544,32 @@ _nss_compat_initgroups_dyn (const char *
b40826
 					    user, group, start, size,
b40826
 					    groupsp, limit, errnop))
b40826
 	     == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
b40826
-	tmpbuf = extend_alloca (tmpbuf, buflen, 2 * buflen);
b40826
+        if (__libc_use_alloca (buflen * 2))
b40826
+          tmpbuf = extend_alloca (tmpbuf, buflen, 2 * buflen);
b40826
+        else
b40826
+          {
b40826
+            buflen *= 2;
b40826
+            char *newbuf = realloc (use_malloc ? tmpbuf : NULL, buflen);
b40826
+            if (newbuf == NULL)
b40826
+              {
b40826
+                status = NSS_STATUS_TRYAGAIN;
b40826
+                goto done;
b40826
+              }
b40826
+            use_malloc = true;
b40826
+            tmpbuf = newbuf;
b40826
+          }
b40826
     }
b40826
   while (status == NSS_STATUS_SUCCESS);
b40826
 
b40826
+  status = NSS_STATUS_SUCCESS;
b40826
+
b40826
+ done:
b40826
+  if (use_malloc)
b40826
+    free (tmpbuf);
b40826
+
b40826
   internal_endgrent (&intern;;
b40826
 
b40826
-  return NSS_STATUS_SUCCESS;
b40826
+  return status;
b40826
 }
b40826
 
b40826