mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03:00
seq: ensure termination at floating point limits
* src/seq.c (dump_strings): Rearrange limit checks to reduce rounding affects. Also add sanity check on on the iteration number limit. * tests/seq/seq-no-progress.sh: Add a new test. * tests/local.mk: Reference the new test. * NEWS: Mention the fix. Discovered by Anthropic's OSS Scanner, as bug ANT-2026-RI0OLUBX.
This commit is contained in:
1 parent
f30c663c73
commit
e85bf8468f
4 files changed
+66
-1
No files matched your search
@@ -129,6 +129,11 @@ GNU coreutils NEWS -*- outline -*-
|
||||
resolves to a path beginning with the symbolic link itself.
|
||||
[bug introduced in coreutils-9.0]
|
||||
|
||||
'seq' no longer outputs the same number indefinitely when the increment
|
||||
is too small to change the floating point value of FIRST.
|
||||
For example with 'seq -w 1e40 1e40' or 'seq 1 1e-40 1'.
|
||||
[This bug was present in "the beginning".]
|
||||
|
||||
'sort --compress-program=PROG' no longer treats an unset PATH environment
|
||||
variable as equivalent to a PATH environment variable defined to an empty
|
||||
string. On typical platforms, that would result in PROG being executed from
|
||||
|
||||
@@ -313,12 +313,22 @@ print_numbers (char const *fmt, struct layout layout,
|
||||
write_error ();
|
||||
if (out_of_range)
|
||||
break;
|
||||
if (i + 1 == i)
|
||||
error (EXIT_FAILURE, EOVERFLOW, _("sequence counter exhausted"));
|
||||
|
||||
/* Mathematically equivalent to 'x += step;', and typically
|
||||
less subject to rounding error. */
|
||||
x = first + i * step;
|
||||
|
||||
out_of_range = (step < 0 ? x < last : last < x);
|
||||
/* If STEP is too small to change X, then X might never pass LAST,
|
||||
so compare the offsets from FIRST, which are not so rounded. */
|
||||
if (x == x0
|
||||
&& ! (step < 0
|
||||
? last - first <= i * step : i * step <= last - first))
|
||||
break;
|
||||
|
||||
/* Also treat NaN, from "seq -- -inf inf 0", as out of range. */
|
||||
out_of_range = ! (step < 0 ? last <= x : x <= last);
|
||||
|
||||
if (out_of_range)
|
||||
{
|
||||
|
||||
@@ -282,6 +282,7 @@ all_tests = \
|
||||
tests/seq/seq-io-errors.sh \
|
||||
tests/seq/seq-locale.sh \
|
||||
tests/seq/seq-long-double.sh \
|
||||
tests/seq/seq-no-progress.sh \
|
||||
tests/seq/seq-precision.sh \
|
||||
tests/head/head.pl \
|
||||
tests/head/head-elide-tail.pl \
|
||||
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
# Ensure seq terminates when floating point arithmetic stops progressing.
|
||||
|
||||
# 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_ seq
|
||||
|
||||
# 2**130 is exactly representable in a 64 bit double
|
||||
big=1361129467683753853853498429727072845824
|
||||
test $(timeout 10 seq -f '%.0f' "$big" "$big" "$big") = "$big" ||
|
||||
skip_ 'seq not built with double support'
|
||||
|
||||
check_seq_ ()
|
||||
{
|
||||
printf '%s\n' "$1" >exp || framework_failure_
|
||||
shift
|
||||
|
||||
{ timeout 10 seq "$@" 2>err; echo $? >status; } | head -n2 >out
|
||||
echo 0 >exp-status || framework_failure_
|
||||
compare exp-status status || fail=1
|
||||
compare /dev/null err || fail=1
|
||||
compare exp out || fail=1
|
||||
}
|
||||
|
||||
check_seq_ "$big" -w "$big" "$big"
|
||||
check_seq_ "$big" "$big" 201 "$big"
|
||||
check_seq_ "-$big" -- "-$big" "-$big"
|
||||
check_seq_ "-$big" -- "-$big" -1 "-$big"
|
||||
|
||||
check_seq_ 1.0000000000000000000000000000000000000000 1 1e-40 1
|
||||
check_seq_ -inf -- -inf -inf
|
||||
# Adding opposite infinities produces NaN, which must terminate the loop.
|
||||
check_seq_ -inf -- -inf inf 0
|
||||
|
||||
Exit $fail
|
||||
Reference in new issue
Block a user