#!/bin/sh
# Test regex on input buffers larger than 2GB

# Copyright (C) 2018-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=.}/testsuite/init.sh"; path_prepend_ ./sed
very_expensive_
print_ver_ sed

# Create a file larger than 2GB and containing a single line
# (resulting in a regex match against the entire file)
#
# This is a "very expensive" test, we can assume it is only run by
# developers or advanced users, and we can assume truncate(1) exists.
#
# On most modern file-systems, the file will be sparse and would not
# consume 2GB of physical storage.

truncate -s 1G  input || framework_failure_
printf aaaa >>  input || framework_failure_
truncate -s +1G input || framework_failure_
printf 'a\n' >> input || framework_failure_

echo bbbbb > exp-out || framework_failure_

# Before sed-4.5, this was silently a no-op: would not perform the substitution
# but would not indicate any error either (https://bugs.gnu.org/30520).
# With sed-4.9, it would result in bbbba, failing to transform the final "a",
# because sed/regex.c used "int" (causing wrap-to-negative malfunction).
sed s/a/b/g input 2>err1 | tr -d '\0' > out || fail=1
compare_ exp-out out || fail=1

Exit $fail
