Page MenuHomeFreeBSD

xargs: terminate if line replacement cannot be constructed
ClosedPublic

Authored by thj on Jun 23 2022, 10:00 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Apr 22, 7:36 AM
Unknown Object (File)
Sun, Apr 14, 10:27 PM
Unknown Object (File)
Sun, Mar 31, 1:36 PM
Unknown Object (File)
Mar 26 2024, 1:19 PM
Unknown Object (File)
Jan 27 2024, 6:46 AM
Unknown Object (File)
Jan 14 2024, 12:08 PM
Unknown Object (File)
Dec 20 2023, 2:54 AM
Unknown Object (File)
Sep 9 2023, 11:15 PM
Subscribers

Details

Summary

If the line with replacement cannot be constructed xargs should terminate as
documented in the man page:

-I replstr                                                               
        Execute utility for each input line, replacing one or more       
        occurrences of replstr in up to replacements (or 5 if no -R flag 
        is specified) arguments to utility with the entire line of input.
        The resulting arguments, after replacement is done, will not be  
        allowed to grow beyond replsize (or 255 if no -S flag is         
        specified) bytes; this is implemented by concatenating as much of
        the argument containing replstr as possible, to the constructed  
        arguments to utility, up to replsize bytes.  The size limit does 
        not apply to arguments to utility which do not contain replstr,  
        and furthermore, no replacement will be done on utility itself.  
        Implies -x.

Currently, xargs continues and lines that cannot accommodate the replacement get
as much of the replstr as will fit in the string. Instead xargs should error,
include an error when we do.

We encounter this error, but gnu/xargs doesn't because they have a much larger
limit for created outputs (~10000 lines).

Test Plan

The behaviour can be seen now with:

$ echo "hello\nworldss\nhello" | xargs -I @LINE@ echo "x$(printf ' %.0s' {1..246}) @LINE@"                                                                     
x                                                                                                                                                                                                                                                       hello
x                                                                                                                                                                                                                                                       @LINE@
x                                                                                                                                                                                                                                                       hello

and after:

$ echo "hello\nworldss\nhello" | /usr/obj/code/freebsd/worktrees/xargs-line/amd64.amd64/usr.bin/xargs/xargs.full -I @LINE@ echo "x$(printf ' %.0s' {1..246}) @LINE@"
x                                                                                                                                                                                                                                                       hello
xargs.full: comamnd line cannot be assembled, too long

I will arrange for an exp-run in case there is any build fall out.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

thj requested review of this revision.Jun 23 2022, 10:00 AM
oshogbo added inline comments.
usr.bin/xargs/strnsubst.c
24

The return code dosen't seem to be interpreted anywhere, do we really want to have them?
Returning just 0/-1, or just bool seems enough.

usr.bin/xargs/xargs.c
521–524

I would suggest negative values to indicate error, or just use bool.

  • make strnsubst return a bool indicating if truncation occured
usr.bin/xargs/strnsubst.c
31

So when is false returned?

This revision was not accepted when it landed; it landed in state Needs Review.Jul 5 2022, 3:18 PM
This revision was automatically updated to reflect the committed changes.