zsh-workers
 help / color / mirror / code / Atom feed
From: Alan Wagner-Krankel <awk@awkwork.com>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: zsh-workers@zsh.org
Subject: Re: zargs error with some combinations of arguments
Date: Mon, 22 Aug 2022 23:39:32 -0500	[thread overview]
Message-ID: <CAGZNKjKyZU05jCFKqnz6iFZAcG-MGkV2a8U2Oa0WV4=TDDTe9A@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7ZAMPN2sKbD1_Fg7eB0ZrqBHLYt3+8KrtFh8TEu24B=_w@mail.gmail.com>

I think we're both proposing basically the same change. It looks like
the format of my context diff hid the use of the calculated 'a' value
later in the code. Here's the diff listing from git:

diff --git a/Functions/Misc/zargs b/Functions/Misc/zargs
index 782d681..c7e96b8 100644
--- a/Functions/Misc/zargs
+++ b/Functions/Misc/zargs
@@ -194,7 +194,7 @@ else
     return 1
 fi

-local -i end c=0
+local -i end a c=0
 if [[ $eof == -(e|-eof) ]]; then ((end=ARGC+1))
 elif (( $#eof )); then end=$argv[(i)${eof##-(e|-eof=)}]
 else end=$argv[(i)--]
@@ -265,6 +265,13 @@ then
     return 1
 fi

+(( a = s - ${#:-"$command "} ))
+if (( a <= 0 ))
+then
+    print -u2 'zargs: value for max-chars must be >= command length'
+    return 1
+fi
+
 l=${${${l##*-(l|L|-max-lines(=|))}[-1]}:-${${l[1]:+1}:-$ARGC}}
 if (( l <= 0 ))
 then
@@ -305,7 +312,7 @@ do
     repeat $P
     do
        ((ARGC)) || break
-       for (( end=l; end && ${(c)#argv[1,end]} > s; end/=2 )) { }
+       for (( end=l; end && ${(c)#argv[1,end]} > a; end/=2 )) { }
        (( end > n && ( end = n ) ))
        args=( "${(@)argv[1,end]}" )
        shift $((end > ARGC ? ARGC : end))


In this line:
  (( a = s - ${#:-"$command "} ))
's' is the max size of the total command line. The calculated value
'a' represents the maximum number of characters that can be used for
arguments to the command. It's stored in a variable to avoid
recalculating it each time in the primary nested loop.

The 'command' variable is an array, and we also need a space for the
separator between the command and the parameters. ${#:-"$command "}
was my (possibly too cute) way of calculating the total length of the
command plus the space. This should give the same result:
  (( a = s - ${(c)#command} - 1 ))


The error message after the 'a <= 0' test is only going to occur
with some edge cases and nonsensical parameters, e.g.:

  zargs -s 10 -- a b c -- somelongcmd

It's not really needed. If the command is too large, the 'cannot fit
single argument within size limit' error message will do the job.


Thanks for looking into this,
Awk


      reply	other threads:[~2022-08-23  4:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-19  2:06 Alan Wagner-Krankel
2022-08-22 21:31 ` Bart Schaefer
2022-08-23  4:39   ` Alan Wagner-Krankel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGZNKjKyZU05jCFKqnz6iFZAcG-MGkV2a8U2Oa0WV4=TDDTe9A@mail.gmail.com' \
    --to=awk@awkwork.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).