[Toybox] [BUG] split and csplit destroy their input when its path coincides with an output path
Rob Landley
rob at landley.net
Fri Oct 9 17:06:28 UTC 2026
On 10/7/26 10:38, João Matos via Toybox wrote:
> Hello,
>
> We found two silent data-loss bugs in toybox's "split" and "csplit".
> Both are present in the current master branch, verified against commit
> 2a27e238cf59ca43c6985b509490a587ad882c12 (Codeberg; 6a0c0ef on GitHub
> differs only in the README), and in release 0.8.14.
>
> Summary
> -------
> Both commands open each output file with truncation while the input is
> still being read, and never check that an output is the input.
Yeah, I've fixed that sort of thing recently ala commit 91125f5a1606 but
in some cases like "echo <file >file" that's what it's _supposed_ to do.
> When the
> input is named like one of the generated outputs, the normal case when
> re-splitting a piece from a previous run, the input is truncated and
> the data is lost.
I also recently encountered vim writing back to the original file
instead of doing a temp/mv pair like I _thought_ it was doing (and thus
editing my hardlinked tree of files wound up modifying the original when
I thought it wouldn't) so... I dunno when it's NOT supposed to do this.
I knew/made "patch" break symlinks because my usual cp -s trees were
doing that way back when, but when I expected vim to do it (it's already
crapping .swp files all over the place) it didn't...
I don't suppose any of this is mentioned in posix or man pages or something?
> - split leaves the input holding one read buffer and exits 0.
Split adds a suffix, how do you get it to write to the same output
file... Ah, I see, I've been typing "split {,}file" by habit since the
90's, you're using the DEFAULT prefix.
I honestly forgot that was an option. The common failure mode back on
the sun workstations in 1993 wasn't "stomping the same file" it was
"there's leftover files from the last split and the new split created
fewer files so now the data seems to continue with leftover debris"
so... always name your split.)
(Sorry, git log --follow tests/split.test says I wrote the split test
cases in 2013, and _my_ usage is built from scar tissue...)
> - csplit goes further: the truncation makes its rules fail, and the
> error path removes the files it created, the input among them. The
> input file is deleted.
I've never used csplit, which is why it's still in pending. (Gotta come
up to speed on the command to clean it up, hasn't bubbled to the top of
the todo list yet...)
> Details
> -------
> split opens each new output (xaa, xab, ...) with
> xcreate(TT.outfile, O_RDWR|O_CREAT|O_TRUNC, ...) inside the read loop
> (toys/posix/split.c line 72). If the input is xaa, the first output
> opened is xaa: it is truncated while still open for reading, so after
> one buffer the input is empty and no other piece is written.
I'm still not sure that isn't pilot error.
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/split.html
doesn't mention anything about input/output file conflicts, unless I'm
just not seeing it?
I suppose I can add an unlink() of the target file before opening it.
Then you can't split across /dev/hd{a,b,c} but that's _probably_ not a
common use case?
If I check that the filename is the same it won't catch hardlinks or
symlinks. If I open the output file, stat it, check the major/minor/ino
against a saved stat of the input file, and then only IF they're the
same open a SECOND file with a tempnam(), write all the new data to it,
and them mv that file over the first one... that's adding a pile of
complexity for an arguably "pilot error" corner case. (It lets you
specify the prefix for a reason.)
Honestly it's a question of what failure modes you want, this is moving
problems around rather than fixing them...
> csplit (toys/pending/csplit.c) opens the input at line 139 and each
> output with xfopen(..., "w+") (line 150). If the input is named like
> the first output (xx00 by default), that open truncates it, the line
> rules can no longer be satisfied, and abrt() removes the output files
> (line 53) unless -k is given, which deletes the input.
>
> Reproduction
> ------------
> split (in defconfig):
>
> python3 -c "import sys; sys.stdout.buffer.write(b'A'*20000)" > xaa
head -c 20000 /dev/zero | tr \\0 A > xaa
> toybox split -b 8000 xaa
> echo "exit=$?" # 0
> wc -c xaa # 4096, not 20000
> ls xab xac # do not exist
With the debian host one:
$ split -b 8000 xaa
split: 'xaa' would overwrite input; aborting
Hmmm, what would...
$ toybox head -c 20000 /dev/zero | tr \\0 A > xab
$ split -b 8000 xab
split: 'xab' would overwrite input; aborting
$ ls -l x*
-rw-r--r-- 1 landley landley 8000 Oct 8 12:42 xaa
-rw-r--r-- 1 landley landley 20000 Oct 8 12:42 xab
So it presumably does the "fstat each one it just opened" thing and then
unceremoniously bails out partway through.
That's way uglier than I was thinking, but sure, I can add that. (Oh
hey, the first "standard does not cover" in split.c header comments is
now covered by the 2024 standard and yes they went with the same choice
we went with.)
> csplit is in pending/, so it needs CONFIG_CSPLIT=y:
I'm just gonna tackle split for now, but I added a note to my local
csplit.c.
> For split, GNU coreutils fixed the same defect in release 8.18 ("split
> now checks that the input is not the output");
Which was a judgement call, but fine. Do what they do. (Personally I
think the unlink() first is better. The output is then what you expect,
having deleted the input file is on you. Pity there's an O_TRUNC but not
O_EVICT or some such to atomically delete and recreate with new inode.)
I note that busybox still does it the old way in yesterday's git master.
(And that split isn't in debian's busybox build.)
$ cp ~/toybox/toybox/README xab
$ busybox split -n 3 xab
split: applet not found
$ ~/busybox/busybox split -n 3 xab
split: invalid option -- 'n'
BusyBox v1.39.0.git (2026-10-09 11:47:53 CDT) multi-call binary.
Usage: split [OPTIONS] [INPUT [PREFIX]]
-b N[k|m] Split by N (kilo|mega)bytes
-l N Split by N lines
-a N Use N letters as suffix
$ ~/busybox/busybox split -b 1000 xab
$ ls -l x*
-rw-r--r-- 1 landley landley 1000 Oct 9 11:54 xaa
-rw-r--r-- 1 landley landley 23 Oct 9 11:54 xab
$
> Suggested fix
> -------------
> Before opening each output, compare st_dev/st_ino (from stat/fstat) of
> the output path, if it exists, against the input's, and refuse if they
> match.
The reason I didn't do this last night is I was going "should there be a
same_file() function in lib/ doing the dev/ino comparison? Would its
arguments be two stat bufs, two filehandles, two filenames, a stat buf
and a filehandle..."
It's one of those cases where repeating the if (!stat() && test1 &&
test2) seems like common code that should be a function, but they're all
SLIGHTLY DIFFERENT, and factoring out JUST the comparison means
marshalling the arguments into the function eliminates possible
space/commplexity savings...
*shrug* Did it inline, but there's no four or five of them in the tree...
> A string comparison of the names is not sufficient (xaa vs
> ./xaa).
Yeah, I know. There's also the old "is fstat() doing an stat() or an
lstat()" (answer: it's s stat(), to do lstat on a filehandle you want
fstatat(fd, "", &st, AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
Honestly if ANY of the output files exist it's a bit weird. "I created
xaa and xab but those already existed and last time had an xac too" just
bit me again writing up the new test.
Funky special case checking for pilot error...
Commit 2dd558ee51e8, and yes I saw your follow up about attribution.
Rob
More information about the Toybox
mailing list