From 6568b173db1c98f39a53eadd9b09e0a0e5c11920 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 10 Nov 2010 20:34:52 -0800 Subject: csplit: do not rely on undefined behavior in printf formats * doc/coreutils.texi (csplit invocation): Say that %d and %i are aliases for %u. * src/csplit.c (FLAG_THOUSANDS, FLAG_ALTERNATIVE): New constants. (get_format_flags): Now take char const * and int * and return size_t. It now stores info about the flags instead of merely scanning them. Also, it handles '0' correctly. Drop support for the undocumented '+' and ' ' flags since the value is unsigned. Add support for the (undocumented) "'" flag. All uses changed. (get_format_width, get_format_prec): Remove. (check_format_conv_type): Renamed from get_format_conv_type, with a different signature. It now converts the format to one that is compatible with unsigned int, and checks flags. All uses changed. (max_out): Have snprintf compute the number of bytes needed rather than attempting to do it ourselves (which doesn't work portably with outlandish formats such as %4294967296d). (check_format_conv_type, main): Check for overflow in size calculations. Don't assume size_t fits in unsigned int. * tests/misc/csplit: Check for proper handling of flags, with %0#6.3x. Coreutils 8.6 mishandles this somewhat-weird example. --- tests/misc/csplit | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tests') diff --git a/tests/misc/csplit b/tests/misc/csplit index bef722485..f365da335 100755 --- a/tests/misc/csplit +++ b/tests/misc/csplit @@ -67,6 +67,21 @@ EOF compare err experr || fail=1 rm -f in out exp err experr +# `echo | csplit -b '%0#6.3x' - 1' incorrectly warned about the format +# up through coreutils 8.6. +echo > in +csplit -b '%0#6.3x' in 1 > out 2> err || fail=1 +cat < exp +0 +1 +EOF +compare out exp || fail=1 +touch experr +compare err experr || fail=1 +compare 'xx 000' experr || fail=1 +compare 'xx 0x001' in || fail=1 +rm -f in out exp err experr xx* + # make sure `csplit FILE 0' fails. echo > in csplit in 0 > out 2> err && fail=1 -- cgit v1.2.3-54-g00ecf