Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/find Inspired by PR pkg/53543



details:   https://anonhg.NetBSD.org/src/rev/e847a4042b2d
branches:  trunk
changeset: 835698:e847a4042b2d
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Sep 04 15:16:15 2018 +0000

description:
Inspired by PR pkg/53543

When calculating the length of the args that can be
appended in a "find .... -exec something {} +"
usage, remember to allow for the arg pointers, which
form part of what is allowed in ARG_MAX.

>From a fairly empty installation of HEAD on amd64
and with a "/tmp/args" command that simply prints
its arg count, and the length of the arg strings,
with this mod I see ..

netbsd# find / -exec /tmp/args {} +
Argc 5000 Arglen 107645
Argc 5000 Arglen 151324
Argc 5000 Arglen 187725
Argc 5000 Arglen 206591
Argc 5000 Arglen 172909
Argc 5000 Arglen 186264
Argc 5000 Arglen 167906
Argc 2881 Arglen 98260

The upper limit of 5000 args is in the code.

Using the biggest of those, 5000
args, plus 206591 bytes of strings
uses 246591 bytes total (this excludes
the command name, so add a few more).
That's fairly close to the ARG_MAX
of 262144.

On another system (with longer paths) I see:
(this is just a small part of the output, using a
different version of the dummy command, and a
slightly different invocation)

Args: 4546 Len 218030
Args: 4878 Len 217991
Args: 4813 Len 218028
Args: 4803 Len 218029

There, 4878*8 + 217991 == 257015 which is about
as close as we'd want to come to the arg limit.

XXX pullup -8

diffstat:

 usr.bin/find/function.c |  12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diffs (40 lines):

diff -r ca09929914d5 -r e847a4042b2d usr.bin/find/function.c
--- a/usr.bin/find/function.c   Tue Sep 04 15:08:30 2018 +0000
+++ b/usr.bin/find/function.c   Tue Sep 04 15:16:15 2018 +0000
@@ -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 @@
                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 @@
                                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



Home | Main Index | Thread Index | Old Index