This isn't ideal, but it matches xargs and none of us is likely to have
time to do the best possible thing any time soon.

Bug: http://b/65818597
Test: ./toybox find /usr/local/google/ndkports/ -exec echo {} +
---
 toys/posix/find.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
From 0b626dae4c98860ec4721cb5b94dbc5cdb3ae9f6 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Thu, 16 Nov 2017 13:59:37 -0800
Subject: [PATCH] Make find -exec + obey ARG_MAX just like xargs.

This isn't ideal, but it matches xargs and none of us is likely to have
time to do the best possible thing any time soon.

Bug: http://b/65818597
Test: ./toybox find /usr/local/google/ndkports/ -exec echo {} +
---
 toys/posix/find.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/toys/posix/find.c b/toys/posix/find.c
index 5c72131..762f89f 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -61,6 +61,7 @@ GLOBALS(
   struct double_list *argdata;
   int topdir, xdev, depth;
   time_t now;
+  long max_bytes;
 )
 
 struct execdir_data {
@@ -498,10 +499,10 @@ static int do_find(struct dirtree *new)
             // Done here vs argument parsing pass so it's after dlist_terminate
             aa->prev = (void *)1;
 
-            // Flush if we pass 16 megs of environment space.
+            // Flush if the child's environment space gets too large.
             // An insanely long path (>2 gigs) could wrap the counter and
             // defeat this test, which could potentially trigger OOM killer.
-            if ((aa->plus += sizeof(char *)+strlen(name)+1) > 1<<24) {
+            if ((aa->plus += sizeof(char *)+strlen(name)+1) > TT.max_bytes) {
               aa->plus = 1;
               toys.exitval |= flush_exec(new, aa);
             }
@@ -543,6 +544,7 @@ void find_main(void)
   char **ss = toys.optargs;
 
   TT.topdir = -1;
+  TT.max_bytes = sysconf(_SC_ARG_MAX) - environ_bytes();
 
   // Distinguish paths from filters
   for (len = 0; toys.optargs[len]; len++)
-- 
2.15.0.448.gf294e3d99a-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to