Files
coreutils/tests/pr/options.sh
T
Ismail Ramzi 3b77964c3c pr: avoid a heap overflow with a very wide line number
With -n and truncated lines, add_line_number() added NUMBER_WIDTH to
INPUT_POSITION without checking for overflow.  A tab at the start of a
line followed by a line number width near INT_MAX would wrap
INPUT_POSITION negative, and with -e the next tab would then be
expanded by more than the tab width, writing past the end of CLUMP_BUFF.
E.g., printf '\t\t\n' | pr -t -e -W2147483647 -n:2147483643
wrote 12 bytes into an 8 byte buffer.

* src/pr.c (add_line_number): Use ckd_add when advancing INPUT_POSITION.
(init_parameters): Likewise when computing NUMBER_WIDTH,
which overflowed with -n:2147483647.
* tests/pr/options.sh: Add test cases.
* NEWS: Mention the bug fix.

Link: https://github.com/coreutils/coreutils/pull/458
2026-11-15 12:32:36 +00:00

90 lines
3.2 KiB
Bash
Executable File

#!/bin/sh
# Test 'pr' numeric option handling
# Copyright (C) 2026 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ pr
getlimits_
# Ensure pr treats all invalid +page ranges as a file
for p in +0 +0foo; do
returns_ 1 pr "$p" 2>err </dev/null || fail=1
printf '%s\n' "pr: $p: $ENOENT" >exp || framework_failure_
compare exp err || fail=1
done
# number parsing issue
returns_ 1 pr --pages=-0 2>err </dev/null || fail=1
printf '%s\n' "pr: invalid --pages argument '-0'" >exp
compare exp err || fail=1
# number validation issue
returns_ 1 pr --pages=0 2>err </dev/null || fail=1
printf '%s\n' "pr: invalid page range '0'" >exp
compare exp err || fail=1
INV='invalid number'
returns_ 1 pr -l0 2>err </dev/null || fail=1
printf '%s\n' "pr: '-l PAGE_LENGTH' $INV of lines: '0': $ERANGE" \
>exp || framework_failure_
compare exp err || fail=1
for w in w W; do
returns_ 1 pr -${w}0 2>err </dev/null || fail=1
printf '%s\n' "pr: '-$w PAGE_WIDTH' $INV of characters: '0': $ERANGE" \
>exp || framework_failure_
compare exp err || fail=1
done
returns_ 1 pr -e=-1 2>err </dev/null || fail=1
printf '%s\n' "pr: '-e' extra characters or $INV in the argument: '=-1'" \
"Try 'pr --help' for more information." \
>exp || framework_failure_
compare exp err || fail=1
# Ensure the line number width plus its separator does not overflow
for n in -n$INT_MAX -n:$INT_MAX; do
returns_ 1 pr $n 2>err </dev/null || fail=1
printf '%s\n' "pr: integer overflow" > exp || framework_failure_
compare exp err || fail=1
done
# Ensure we exit gracefully upon internal overflow limits
# Tag as expensive as it uses little mem, but about 10s on a 2020 class machine.
if { test "$RUN_VERY_EXPENSIVE_TESTS" = yes ||
test "$RUN_EXPENSIVE_TESTS" = yes; } &&
test "$INT_MAX" = 2147483647; then # restrict to usual 32 bit limits
head -c1M /dev/zero | tr '\0' '\t' |
returns_ 1 pr -t -e$(($INT_MAX/(1024*1024) + 1)) 2>err >/dev/null || fail=1
printf '%s\n' "pr: integer overflow" > exp || framework_failure_
compare exp err || fail=1
fi
# Ensure a line number after a tab doesn't overflow the input position,
# which would expand the next tab past the end of its buffer.
# Tag as very expensive as it uses about 2GiB of memory and output.
if test "$RUN_VERY_EXPENSIVE_TESTS" = yes &&
test "$INT_MAX" = 2147483647; then # restrict to usual 32 bit limits
printf '\t\t\n' |
returns_ 1 pr -t -e -W$INT_MAX -n:$(($INT_MAX - 4)) 2>err >/dev/null || fail=1
printf '%s\n' "pr: integer overflow" > exp || framework_failure_
compare exp err || fail=1
fi
Exit $fail