From a76230b70f5f81c63fbb43debf8d690885537de4 Mon Sep 17 00:00:00 2001
From: Lasse Collin <lasse.collin@tukaani.org>
Date: Wed, 23 Jan 2008 17:48:40 +0100
Subject: [PATCH] Don't rely on signed integer overflowing to negative value.

* lib/getugroups.c (getugroups): Include <limits.h>.
Instead, compare against INT_MAX, and increment only if the test passes.
---
 ChangeLog        | 6 ++++++
 lib/getugroups.c | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 155d5f7be8..a7ef8fb4be 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-02-13  Lasse Collin  <lasse.collin@tukaani.org>
+
+	Don't rely on signed integer overflowing to negative value.
+	* lib/getugroups.c (getugroups): Include <limits.h>.
+	Instead, compare against INT_MAX, and increment only if the test passes.
+
 2008-02-13  Jim Meyering  <meyering@redhat.com>
 	and Eric Blake  <ebb9@byu.net>
 
diff --git a/lib/getugroups.c b/lib/getugroups.c
index 6c557cdf34..4b8752f6b6 100644
--- a/lib/getugroups.c
+++ b/lib/getugroups.c
@@ -21,6 +21,7 @@
 
 #include "getugroups.h"
 
+#include <limits.h>
 #include <stdio.h> /* grp.h on alpha OSF1 V2.0 uses "FILE *". */
 #include <grp.h>
 
@@ -92,12 +93,12 @@ getugroups (int maxcount, GETGROUPS_T *grouplist, char const *username,
 		    goto done;
 		  grouplist[count] = grp->gr_gid;
 		}
-	      count++;
-	      if (count < 0)
+	      if (count == INT_MAX)
 		{
 		  errno = EOVERFLOW;
 		  goto done;
 		}
+	      count++;
 	    }
 	}
     }
-- 
2.30.2