From nobody Wed Jun 14 13:26:32 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Qh5mS6v4Mz4dhDM; Wed, 14 Jun 2023 13:26:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Qh5mS3qTDz3tqW; Wed, 14 Jun 2023 13:26:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686749192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bjdvYMw19Oyr5NsmF30JVFt4r8Dn4Dx80fhQhWwAFSg=; b=BT6IbhUD3Sp8X/k+xyywKaifgQD9X2fa4T83AGcvfwCKijCu8mp00VQRhFGbL1NNWOhSTm +7GQ5idM8+w5wxMmc0xbOwYTVLGWKnEYJz+qYu6mX6FaWtShw78uS7SpSPBHp2BQ+9biuA LQ7Ii8Hn6VUxcVhTIE7Br3hhayM0u0VdauZJzii/2+dR+HM5rTO9Q8nyaO05wWlandIKTw mCMrDoCytmjtIIUc80Ewt1U0NpsDKrE8ShBrd6KwvIGXmXhjy7W0FRHO9Cb7flpUlt76Dl CWJGxW4Yu0wEf2h5TZd+0OFIeC7bPMkSc3nc65nIsqleMLA35VwePGCsi58eQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686749192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bjdvYMw19Oyr5NsmF30JVFt4r8Dn4Dx80fhQhWwAFSg=; b=Zapq82LYmQBba65PyuX6osmA1c/117xKO3/F7dVMGHdN3kkEcfhPP6gJr81SqcU1zRmQlt b6hCDIpa5Pe34ecxQOng+l8OdoOjqYqyB89UZeEMHAP85FuZwyJbYuJwEQdE7J8oQkzEuJ l0beZZKcPoyEsLHqCzNXwMogqB0v+KGjZpEk7iugCqJ3TTNrAbm1XFWzDwQ9IiJN/UzIvJ Ucfg/agbV7AkeMX92FoAqx44R6K1FFPJiI9KRBMdUOshNVXFxvE4VfQGu7qFB8eiysz6h9 SNDDSl3juJHBbUfsKdMtN3rfb6ibVehQ2vf2sxDsZq9haWYW3+TbMfQfRnCMsA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686749192; a=rsa-sha256; cv=none; b=M2269JhdP4qG1zfBbt07KqUlaaVBc0udADoPNTo9gFm7oUHtNhpA+Z83HzpNE4cBLxOC04 /FOXppDW95KdIOCTorBPQ7vakdOaoVzz7U7+c6Mcc1um3hFwdO14XIMJLBH5NBWj0melpo SNwq1KHThAYNLE8VFRZTN3BLp022HvwXJMYsl8m0QZZvNBUWJts46Y/GMC6Ct/yfKAMtq8 b6KWWuSnW3NRoKFff9cPRul5ITqApV9xg2OyY131RWwpkRA2whJgmfIpViKG55m5BsnKDR AA1wtxiK6LDupAxqRXDW2ZtnmQ2fGPvvxIQHY5DyT8GSwbCEQA7xeLb7xn0PAQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Qh5mS2cw7z15K0; Wed, 14 Jun 2023 13:26:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35EDQWIk099525; Wed, 14 Jun 2023 13:26:32 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35EDQWrO099524; Wed, 14 Jun 2023 13:26:32 GMT (envelope-from git) Date: Wed, 14 Jun 2023 13:26:32 GMT Message-Id: <202306141326.35EDQWrO099524@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: bc47005a648b - stable/13 - xargs: terminate if line replacement cannot be constructed List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: bc47005a648b1b326cfd852c039a9b01854c5796 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=bc47005a648b1b326cfd852c039a9b01854c5796 commit bc47005a648b1b326cfd852c039a9b01854c5796 Author: Tom Jones AuthorDate: 2022-07-05 15:03:51 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-06-14 12:48:55 +0000 xargs: terminate if line replacement cannot be constructed If the line with replacement cannot be constructed xargs should terminate as documented in the man page We encounter this error, but gnu/xargs doesn't because they have a much larger limit for created outputs (~10000 lines). Reviewed by: oshogbo Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D35574 (cherry picked from commit f058359ba5e08c555d7e6f192217f890b83cd46c) xargs: fix description of strnsubst return value Reported by: oshogbo Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D35574 (cherry picked from commit 1e692b938e37a9b43a43ace2739eb6b97379cac0) xargs: improve foundeof check for -E 4aeb63826e0f got it almost correct (we can't use strcmp() here as current argument isn't guaranteed to be NUL-terminated), but we also need to check that current argument length is equal to that of eofstr. PR: 270867 Reviewed by: bapt Differential Revision: https://reviews.freebsd.org/D39583 (cherry picked from commit 21ef48af5c0f4ed85a5f42855b5a8d58b5431103) xargs: Fix typo in error message. MFC after: 1 week Sponsored by: Klara, Inc. (cherry picked from commit 6d777389e19d3bebde515e88e8405de45d8af7bd) xargs: Consistently use strtonum() to parse arguments. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D40425 (cherry picked from commit fbc445addf9183d180bb8b488281617bb19d9242) --- usr.bin/xargs/strnsubst.c | 15 ++++++++++----- usr.bin/xargs/xargs.c | 44 +++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/usr.bin/xargs/strnsubst.c b/usr.bin/xargs/strnsubst.c index d826d33c0967..4a2ecd5aa9a8 100644 --- a/usr.bin/xargs/strnsubst.c +++ b/usr.bin/xargs/strnsubst.c @@ -12,11 +12,12 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include -void strnsubst(char **, const char *, const char *, size_t); +bool strnsubst(char **, const char *, const char *, size_t); /* * Replaces str with a string consisting of str with match replaced with @@ -26,16 +27,19 @@ void strnsubst(char **, const char *, const char *, size_t); * str as well as the new contents are handled in an appropriate manner. * If replstr is NULL, then that internally is changed to a nil-string, so * that we can still pretend to do somewhat meaningful substitution. - * No value is returned. + * + * Returns true if truncation was needed to do the replacement, false if + * truncation was not required. */ -void +bool strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) { char *s1, *s2, *this; + bool error = false; s1 = *str; if (s1 == NULL) - return; + return false; /* * If maxsize is 0 then set it to the length of s1, because we have * to duplicate s1. XXX we maybe should double-check whether the match @@ -67,6 +71,7 @@ strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) if ((strlen(s2) + strlen(s1) + strlen(replstr) - strlen(match) + 1) > maxsize) { strlcat(s2, s1, maxsize); + error = true; goto done; } strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1); @@ -76,7 +81,7 @@ strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) strcat(s2, s1); done: *str = s2; - return; + return error; } #ifdef TEST diff --git a/usr.bin/xargs/xargs.c b/usr.bin/xargs/xargs.c index 6520d43dae50..e832cbb35a31 100644 --- a/usr.bin/xargs/xargs.c +++ b/usr.bin/xargs/xargs.c @@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -73,7 +74,7 @@ static void prerun(int, char *[]); static int prompt(void); static void run(char **); static void usage(void); -void strnsubst(char **, const char *, const char *, size_t); +bool strnsubst(char **, const char *, const char *, size_t); static pid_t xwait(int block, int *status); static void xexit(const char *, const int); static void waitchildren(const char *, int); @@ -90,6 +91,7 @@ static char echo[] = _PATH_ECHO; static char **av, **bxp, **ep, **endxp, **xp; static char *argp, *bbp, *ebp, *inpline, *p, *replstr; static const char *eofstr; +static long eoflen; static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag; static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag; static int curprocs, maxprocs; @@ -122,12 +124,12 @@ main(int argc, char *argv[]) int ch, Jflag, nargs, nflag, nline; size_t linelen; struct rlimit rl; - char *endptr; const char *errstr; inpline = replstr = NULL; ep = environ; eofstr = ""; + eoflen = 0; Jflag = nflag = 0; (void)setlocale(LC_ALL, ""); @@ -158,6 +160,7 @@ main(int argc, char *argv[]) switch (ch) { case 'E': eofstr = optarg; + eoflen = strlen(eofstr); break; case 'I': Jflag = 0; @@ -171,23 +174,23 @@ main(int argc, char *argv[]) replstr = optarg; break; case 'L': - Lflag = strtonum(optarg, 0, INT_MAX, &errstr); + Lflag = (int)strtonum(optarg, 0, INT_MAX, &errstr); if (errstr) - errx(1, "-L %s: %s", optarg, errstr); + errx(1, "-%c %s: %s", ch, optarg, errstr); break; case 'n': nflag = 1; - nargs = strtonum(optarg, 1, INT_MAX, &errstr); + nargs = (int)strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) - errx(1, "-n %s: %s", optarg, errstr); + errx(1, "-%c %s: %s", ch, optarg, errstr); break; case 'o': oflag = 1; break; case 'P': - maxprocs = strtonum(optarg, 0, INT_MAX, &errstr); + maxprocs = (int)strtonum(optarg, 0, INT_MAX, &errstr); if (errstr) - errx(1, "-P %s: %s", optarg, errstr); + errx(1, "-%c %s: %s", ch, optarg, errstr); if (getrlimit(RLIMIT_NPROC, &rl) != 0) errx(1, "getrlimit failed"); if (maxprocs == 0 || maxprocs > rl.rlim_cur) @@ -197,22 +200,22 @@ main(int argc, char *argv[]) pflag = 1; break; case 'R': - Rflag = strtol(optarg, &endptr, 10); - if (*endptr != '\0') - errx(1, "replacements must be a number"); + Rflag = (int)strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(1, "-%c %s: %s", ch, optarg, errstr); break; case 'r': /* GNU compatibility */ break; case 'S': - Sflag = strtoul(optarg, &endptr, 10); - if (*endptr != '\0') - errx(1, "replsize must be a number"); + Sflag = (int)strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(1, "-%c %s: %s", ch, optarg, errstr); break; case 's': - nline = strtonum(optarg, 0, INT_MAX, &errstr); + nline = (int)strtonum(optarg, 0, INT_MAX, &errstr); if (errstr) - errx(1, "-s %s: %s", optarg, errstr); + errx(1, "-%c %s: %s", ch, optarg, errstr); break; case 't': tflag = 1; @@ -347,8 +350,8 @@ arg1: if (insingle || indouble) { xexit(*av, 1); } arg2: - foundeof = *eofstr != '\0' && - strncmp(argp, eofstr, p - argp) == 0; + foundeof = eoflen != 0 && p - argp == eoflen && + strncmp(argp, eofstr, eoflen) == 0; /* Do not make empty args unless they are quoted */ if ((argp != p || wasquoted) && !foundeof) { @@ -521,7 +524,10 @@ prerun(int argc, char *argv[]) while (--argc) { *tmp = *avj++; if (repls && strstr(*tmp, replstr) != NULL) { - strnsubst(tmp++, replstr, inpline, (size_t)Sflag); + if (strnsubst(tmp++, replstr, inpline, (size_t)Sflag)) { + warnx("command line cannot be assembled, too long"); + xexit(*argv, 1); + } if (repls > 0) repls--; } else {