e7eb88
diff --git a/include/typemax.h b/include/typemax.h
e7eb88
--- a/include/typemax.h
e7eb88
+++ b/include/typemax.h
e7eb88
@@ -35,14 +35,23 @@
e7eb88
 #  define TYPE_SIGNED(t)	(! ((t) 0 < (t) -1))
e7eb88
 #endif
e7eb88
 
e7eb88
+#ifndef TYPE_SIGNED_MAGNITUDE
e7eb88
+#  define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
e7eb88
+#endif
e7eb88
+
e7eb88
+#ifndef TYPE_WIDTH
e7eb88
+#  define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
e7eb88
+#endif
e7eb88
+
e7eb88
 #ifndef TYPE_MINIMUM
e7eb88
-#  define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
e7eb88
-				? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
e7eb88
-				: (t) 0))
e7eb88
+#  define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
e7eb88
 #endif
e7eb88
 
e7eb88
 #ifndef TYPE_MAXIMUM
e7eb88
-#  define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
e7eb88
+#  define TYPE_MAXIMUM(t)  \
e7eb88
+  ((t) (! TYPE_SIGNED (t) \
e7eb88
+        ? (t) -1 \
e7eb88
+        : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
e7eb88
 #endif
e7eb88
 
e7eb88
 #ifdef HAVE_LONG_LONG
e7eb88
diff --git a/jobs.c b/jobs.c
e7eb88
--- a/jobs.c
e7eb88
+++ b/jobs.c
e7eb88
@@ -72,6 +72,8 @@
e7eb88
 #include "execute_cmd.h"
e7eb88
 #include "flags.h"
e7eb88
 
e7eb88
+#include "typemax.h"
e7eb88
+
e7eb88
 #include "builtins/builtext.h"
e7eb88
 #include "builtins/common.h"
e7eb88
 
e7eb88
@@ -92,7 +94,7 @@ extern int killpg __P((pid_t, int));
e7eb88
 #endif
e7eb88
 
e7eb88
 #if !MAX_CHILD_MAX
e7eb88
-#  define MAX_CHILD_MAX 8192
e7eb88
+#  define MAX_CHILD_MAX 32768
e7eb88
 #endif
e7eb88
 
e7eb88
 #if !defined (DEBUG)
e7eb88
@@ -751,7 +753,7 @@ stop_pipeline (async, deferred)
e7eb88
 static void
e7eb88
 bgp_resize ()
e7eb88
 {
e7eb88
-  ps_index_t nsize;
e7eb88
+  ps_index_t nsize, nsize_cur, nsize_max;
e7eb88
   ps_index_t psi;
e7eb88
 
e7eb88
   if (bgpids.nalloc == 0)
e7eb88
@@ -765,11 +767,20 @@ bgp_resize ()
e7eb88
   else
e7eb88
     nsize = bgpids.nalloc;
e7eb88
 
e7eb88
-  while (nsize < js.c_childmax)
e7eb88
-    nsize *= 2;
e7eb88
+  nsize_max = TYPE_MAXIMUM (ps_index_t);
e7eb88
+  nsize_cur = (ps_index_t)js.c_childmax;
e7eb88
+  if (nsize_cur < 0)				/* overflow */
e7eb88
+    nsize_cur = MAX_CHILD_MAX;
e7eb88
 
e7eb88
-  if (bgpids.nalloc < js.c_childmax)
e7eb88
-    {
e7eb88
+  while (nsize > 0 && nsize < nsize_cur)	/* > 0 should catch overflow */
e7eb88
+    nsize <<= 1;
e7eb88
+  if (nsize > nsize_max || nsize <= 0)		/* overflow? */
e7eb88
+    nsize = nsize_max;
e7eb88
+  if (nsize > MAX_CHILD_MAX)
e7eb88
+    nsize = nsize_max = MAX_CHILD_MAX;		/* hard cap */
e7eb88
+
e7eb88
+  if (bgpids.nalloc < nsize_cur && bgpids.nalloc < nsize_max)
e7eb88
+  {
e7eb88
       bgpids.storage = (struct pidstat *)xrealloc (bgpids.storage, nsize * sizeof (struct pidstat));
e7eb88
 
e7eb88
       for (psi = bgpids.nalloc; psi < nsize; psi++)
e7eb88
@@ -787,7 +798,7 @@ bgp_getindex ()
e7eb88
 {
e7eb88
   ps_index_t psi;
e7eb88
 
e7eb88
-  if (bgpids.nalloc < js.c_childmax || bgpids.head >= bgpids.nalloc)
e7eb88
+  if (bgpids.nalloc < (ps_index_t)js.c_childmax || bgpids.head >= bgpids.nalloc)
e7eb88
     bgp_resize ();
e7eb88
 
e7eb88
   pshash_delindex (bgpids.head);		/* XXX - clear before reusing */