1#!/bin/sh
2# Ensure that cut does not allocate mem for large ranges
3
4# Copyright (C) 2012-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ cut
21getlimits_
22
23vm=$(get_min_ulimit_v_ returns_ 0 cut -b1 /dev/null) \
24  || skip_ "this shell lacks ulimit support"
25
26# Ensure we can cut up to our sentinel value.
27# Don't use expr to subtract one,
28# since UINTMAX_MAX may exceed its maximum value.
29CUT_MAX=$(expr $UINTMAX_MAX - 1) || framework_failure_
30
31# From coreutils-8.10 through 8.20, this would make cut try to allocate
32# a 256MiB bit vector.
33(ulimit -v $vm && cut -b$CUT_MAX- /dev/null > err 2>&1) || fail=1
34
35# Up to and including coreutils-8.21, cut would allocate possibly needed
36# memory upfront.  Subsequently extra memory is no longer needed.
37(ulimit -v $vm && cut -b1-$CUT_MAX /dev/null >> err 2>&1) || fail=1
38
39# Explicitly disallow values above CUT_MAX
40(ulimit -v $vm && returns_ 1 cut -b$UINTMAX_MAX /dev/null 2>/dev/null) ||
41  fail=1
42(ulimit -v $vm && returns_ 1 cut -b$UINTMAX_OFLOW /dev/null 2>/dev/null) ||
43  fail=1
44
45compare /dev/null err || fail=1
46
47Exit $fail
48