csplit: do not rely on undefined behavior in printf formats
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 11 Nov 2010 04:34:52 +0000 (20:34 -0800)
committerJim Meyering <meyering@redhat.com>
Thu, 11 Nov 2010 07:16:24 +0000 (08:16 +0100)
commit6568b173db1c98f39a53eadd9b09e0a0e5c11920
treeccbc21bc55d866e91db9e97453505d9c66263360
parent0339eb4598600024cedca14a7e882c1737dd2d82
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.
doc/coreutils.texi
src/csplit.c
tests/misc/csplit