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