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
F86039220: D35574.diff
Fri, Jun 14, 11:34 AM
Unknown Object (File)
Mon, Jun 3, 11:17 AM
Unknown Object (File)
Fri, May 31, 10:21 AM
Unknown Object (File)
Tue, May 21, 4:36 AM
Unknown Object (File)
Tue, May 21, 4:33 AM
Unknown Object (File)
Tue, May 21, 4:25 AM
Unknown Object (File)
Sun, May 19, 9:11 PM
Unknown Object (File)
Sat, May 18, 10:17 AM
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
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 46272
Build 43161: arc lint + arc unit

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.