fix 'sort -g' don't meet expectations

This commit is contained in:
zou_lin77 2022-08-27 18:01:06 +08:00
parent 918a4543a4
commit b3ca7be8a3
4 changed files with 5490 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,57 @@
From 2f56f5a42033dc6db15d8963e54566f01fa0d61d Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sun, 1 May 2022 22:46:21 -0700
Subject: [PATCH] sort: fix sort -g infloop again
Problem reported by Giulio Genovese (Bug#55212).
* src/sort.c (nan_compare): To compare NaNs, simply printf+strcmp.
This avoids the problem of padding bits and unspecified behavior.
Args are now long double instead of char *; caller changed.
---
src/sort.c | 21 ++++++---------------
1 files changed, 6 insertions(+), 15 deletions(-)
diff --git a/src/sort.c b/src/sort.c
index 3b775d6bb..b2a465cf5 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2359,22 +2359,13 @@ numcompare (char const *a, char const *b)
}
#endif /* HAV_EMBRTOWC */
-/* Work around a problem whereby the long double value returned by glibc's
- strtold ("NaN", ...) contains uninitialized bits: clear all bytes of
- A and B before calling strtold. FIXME: remove this function if
- gnulib guarantees that strtold's result is always well defined. */
static int
-nan_compare (char const *sa, char const *sb)
+nan_compare (long double a, long double b)
{
- long double a;
- memset (&a, 0, sizeof a);
- a = strtold (sa, NULL);
-
- long double b;
- memset (&b, 0, sizeof b);
- b = strtold (sb, NULL);
-
- return memcmp (&a, &b, sizeof a);
+ char buf[2][sizeof "-nan()" + CHAR_BIT * sizeof a];
+ snprintf (buf[0], sizeof buf[0], "%Lf", a);
+ snprintf (buf[1], sizeof buf[1], "%Lf", b);
+ return strcmp (buf[0], buf[1]);
}
static int
@@ -2402,7 +2393,7 @@ general_numcompare (char const *sa, char const *sb)
: a == b ? 0
: b == b ? -1
: a == a ? 1
- : nan_compare (sa, sb));
+ : nan_compare (a, b));
}
/* Return an integer in 1..12 of the month name MONTH.
--
2.27.0

View File

@ -0,0 +1,43 @@
From ddafdae21c574b1dcd5c56e403c82010e7ed3565 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Mon, 2 May 2022 14:27:34 +0100
Subject: [PATCH] tests: sort-NaN-infloop: augment testing for recent fix
* tests/misc/sort-NaN-infloop.sh: Add test case from
https://unix.stackexchange.com/a/700967/37127
* src/sort.c: Avoid syntax-check failure.
---
src/sort.c | 2 +-
tests/misc/sort-NaN-infloop.sh | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/sort.c b/src/sort.c
index b2a465cf5..8af356c66 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2006,7 +2006,7 @@ numcompare (char const *a, char const *b)
static int
nan_compare (long double a, long double b)
{
- char buf[2][sizeof "-nan()" + CHAR_BIT * sizeof a];
+ char buf[2][sizeof "-nan""()" + CHAR_BIT * sizeof a];
snprintf (buf[0], sizeof buf[0], "%Lf", a);
snprintf (buf[1], sizeof buf[1], "%Lf", b);
return strcmp (buf[0], buf[1]);
diff --git a/tests/misc/sort-NaN-infloop.sh b/tests/misc/sort-NaN-infloop.sh
index 93cf9bd77..cc1c583cd 100755
--- a/tests/misc/sort-NaN-infloop.sh
+++ b/tests/misc/sort-NaN-infloop.sh
@@ -23,6 +23,9 @@ echo nan > F || framework_failure_
printf 'nan\nnan\n' > exp || framework_failure_
timeout 10 sort -g -m F F > out || fail=1
+# This was seen to infloop on some systems until coreutils v9.2 (bug 55212)
+yes nan | head -n128095 | timeout 60 sort -g > /dev/null || fail=1
+
compare exp out || fail=1
Exit $fail
--
2.27.0

View File

@ -1,6 +1,6 @@
Name: coreutils
Version: 9.0
Release: 5
Release: 6
License: GPLv3+
Summary: A set of basic GNU tools commonly used in shell scripts
Url: https://www.gnu.org/software/coreutils/
@ -27,6 +27,9 @@ Patch11: backport-df-fix-memory-leak.patch
Patch12: backport-ls-avoid-triggering-automounts.patch
Patch13: backport-stat-only-automount-with-cached-never.patch
Patch14: backport-config-color-alias-for-ls.patch
Patch15: backport-coreutils-i18n.patch
Patch16: backport-sort-fix-sort-g-infloop-again.patch
Patch17: backport-tests-sort-NaN-infloop-augment-testing-for-recent-fi.patch
Patch9000: openEuler-coreutils-df-direct.patch
@ -150,6 +153,9 @@ fi
%{_mandir}/man*/*
%changelog
* Sat Aug 27 2022 zoulin <zoulin13@h-partners.com> - 9.0-6
- fix 'sort -g' don't meet expectations
* Thu Jul 21 2022 xueyamao <xueyamao@kylinos.cn> - 9.0-5
- a new option df --direct