diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index b8b4f488aef3..02540f23848b 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -1,4 +1,4 @@ -/* $NetBSD: function.c,v 1.76 2017/06/13 13:10:32 christos Exp $ */ +/* $NetBSD: function.c,v 1.77 2018/09/04 15:16:15 kre Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "from: @(#)function.c 8.10 (Berkeley) 5/4/95"; #else -__RCSID("$NetBSD: function.c,v 1.76 2017/06/13 13:10:32 christos Exp $"); +__RCSID("$NetBSD: function.c,v 1.77 2018/09/04 15:16:15 kre Exp $"); #endif #endif /* not lint */ @@ -759,7 +759,9 @@ c_exec(char ***argvp, int isok, char *opt) size_t c, bufsize; cnt = ap - *argvp - 1; /* units are words */ - new->ep_maxargs = 5000; + new->ep_maxargs = ARG_MAX / (sizeof (char *) + 16); + if (new->ep_maxargs > 5000) + new->ep_maxargs = 5000; new->e_argv = emalloc((cnt + new->ep_maxargs) * sizeof(*new->e_argv)); @@ -780,7 +782,9 @@ c_exec(char ***argvp, int isok, char *opt) errx(1, "Arguments too long"); new->e_argv[cnt] = *argv; } - bufsize = MAXARG - c; + if (c + new->ep_maxargs * sizeof (char *) >= MAXARG) + errx(1, "Arguments too long"); + bufsize = MAXARG - c - new->ep_maxargs * sizeof (char *); /* * Allocate, and then initialize current, base, and