Discussion:
[Toybox] How is paste.c broken, let me count the ways...
Rob Landley
2017-03-20 16:13:13 UTC
Permalink
I've never really used "paste" (I keep thinking it lets the command line
interact with the x11 clipboard; instead it collates lines) but it's in
posix and one was contributed to toybox slightly before I started the
"pending" directory, and a few days ago a version got posted to
busybox's mailing list which had tests in it so I thought I'd try them
against toybox's version, and... It doesn't seem to work at _all_?

paste <(echo -e 'o1\no2\no3') <(echo -e 't1\nt2\nt3')

Should produce

o1\tt1
o2\tt2
o3\tt3

But instead has each \t and \n swapped. And the -s version should do:

o1\to2\to3
t1\tt2\tt3

But each line has an extra trailing tab.

So I start looking at the code, which is copying the -d stuff into
toybuf WITH NO BOUNDS CHECKING. There's no way I actually reviewed this,
he says while replacing ten lines of code with one line. No really:

- for (i = 0, c = 0; c != EOF;) {
- switch(c = getc(f)) {
- case '\n':
- putchar(toybuf[i++ % ndelim]);
- case EOF:
- break;
- default:
- putchar(c);
- }
- }
+
+ for (i=0; 0<=(c=getc(f));) putchar((c=='\n')?toybuf[i++%ndelim]:c);

(That last line has more spaces in the actual .c file, but the
wordwrapper in thunderbird's a bit aggressive. And totally broken when
it comes to quoted material...)

Sigh, this file was on my "list of commands to review" back when that
was a file instead of a directory. I thought I got all those but
apparently not, I should go back and dig that list up and see what else
I missed.

Rob

P.S. I have a todo item to do a full review of everything before 1.0,
but given how much $DAYJOB's distracted me from just general development
the past few months, dunno when that's likely to happen...

Loading...