1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-21 03:12:48 +02:00

split: handle large numbers better

Prefer signed types to uintmax_t, as this allows for better
runtime checking with gcc -fsanitize=undefined.
Also, when an integer overflows just use the maximal value
when the code will do the right thing anyway.
* src/split.c (set_suffix_length, bytes_split, lines_split)
(line_bytes_split, lines_chunk_split, bytes_chunk_extract)
(lines_rr, parse_chunk, main):
Prefer a signed type (typically intmax_t) to uintmax_t.
(strtoint_die): New function.
(OVERFLOW_OK): New macro.  Use it elsewhere, where we now allow
LONGINT_OVERFLOW because the code then does the right thing on all
practical platforms (they have int wide enough so that it cannot
be practically exhausted).  We can do this now that we can safely
assume intmax_t has at least 64 bits.
(parse_n_units): New function.
(parse_chunk, main): Use it.
(main): Do not worry about integer overflow when the code
will do the right thing anyway with the extreme value.
Just use the extreme value.
* tests/split/fail.sh: Adjust to match new behavior.
This commit is contained in:
Paul Eggert
2023-03-04 11:42:16 -08:00
parent 1ebee5b1a3
commit 3434cdcec1
3 changed files with 81 additions and 81 deletions

View File

@@ -46,30 +46,17 @@ returns_ 1 split -0 in 2> /dev/null || fail=1
split --lines=$UINTMAX_MAX in || fail=1
split --bytes=$OFF_T_MAX in || fail=1
returns_ 1 split --line-bytes=$OFF_T_OFLOW 2> /dev/null in || fail=1
returns_ 1 split --line-bytes=$SIZE_OFLOW 2> /dev/null in || fail=1
split --line-bytes=$OFF_T_OFLOW in || fail=1
split --line-bytes=$SIZE_OFLOW in || fail=1
if truncate -s$SIZE_OFLOW large; then
# Ensure we can split chunks of a large file on 32 bit hosts
split --number=$SIZE_OFLOW/$SIZE_OFLOW large >/dev/null || fail=1
fi
split --number=r/$UINTMAX_MAX/$UINTMAX_MAX </dev/null >/dev/null || fail=1
split --number=r/$INTMAX_MAX/$UINTMAX_MAX </dev/null >/dev/null || fail=1
returns_ 1 split --number=r/$UINTMAX_OFLOW </dev/null 2>/dev/null || fail=1
# Make sure that a huge obsolete option evokes the right failure.
split -99999999999999999991 2> out
# On losing systems (x86 Solaris 5.9 c89), we get a message like this:
# split: line count option -9999999999... is too large
# while on most, we get this:
# split: line count option -99999999999999999991... is too large
# so map them both to -99*.
sed 's/99[19]*/99*/' out > out-t
mv -f out-t out
cat <<\EOF > exp
split: line count option -99*... is too large
EOF
compare exp out || fail=1
# Make sure that a huge obsolete option does the right thing.
split -99999999999999999991 in || fail=1
# Make sure split fails when it can't read input
# (the current directory in this case)