diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-10-10 15:59:56 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-10-10 16:06:28 -0700 |
commit | 6cafb122fa214baa96a67fbbd2dab6c77a961e0a (patch) | |
tree | 0c1a34948470dd3fdd21fed62e587a58c5a4f9b5 /gl | |
parent | 5b43b8e9ce215864001314f92097584bceb71b58 (diff) | |
download | coreutils-6cafb122fa214baa96a67fbbd2dab6c77a961e0a.tar.gz |
sort: fix unlikely bug when '\377' < 0
* gl/lib/strintcmp.c (strintcmp): Don’t assume that the input
cannot contain ((char) -1), as this equals '\377' when char is
signed (assuming 8-bit char).
* src/sort.c (decimal_point): Now char, to make it clear
that it’s always in char range now.
(NON_CHAR): New constant.
(traverse_raw_number): Return char not unsigned char;
this is simpler and could be faster. All callers changed.
(main): Do not convert decimal_point and thousands_sep to
unsigned char, as this can mishandle comparisons on
machines where char is signed and the input data contains
((char) -1). Use NON_CHAR, not -1, as an out-of-range value for
thousands_sep.
Diffstat (limited to 'gl')
-rw-r--r-- | gl/lib/strintcmp.c | 4 | ||||
-rw-r--r-- | gl/lib/strnumcmp-in.h | 4 |
2 files changed, 5 insertions, 3 deletions
diff --git a/gl/lib/strintcmp.c b/gl/lib/strintcmp.c index 0ecf6d02c..215158627 100644 --- a/gl/lib/strintcmp.c +++ b/gl/lib/strintcmp.c @@ -21,6 +21,8 @@ #include "strnumcmp-in.h" +#include <limits.h> + /* Compare strings A and B as integers without explicitly converting them to machine numbers, to avoid overflow problems and perhaps improve performance. */ @@ -28,5 +30,5 @@ int strintcmp (char const *a, char const *b) { - return numcompare (a, b, -1, -1); + return numcompare (a, b, CHAR_MAX + 1, CHAR_MAX + 1); } diff --git a/gl/lib/strnumcmp-in.h b/gl/lib/strnumcmp-in.h index 7f2d4bbe6..f1b03689c 100644 --- a/gl/lib/strnumcmp-in.h +++ b/gl/lib/strnumcmp-in.h @@ -106,8 +106,8 @@ fraccompare (char const *a, char const *b, char decimal_point) /* Compare strings A and B as numbers without explicitly converting them to machine numbers, to avoid overflow problems and perhaps improve performance. DECIMAL_POINT is the decimal point and - THOUSANDS_SEP the thousands separator. A DECIMAL_POINT of -1 - causes comparisons to act as if there is no decimal point + THOUSANDS_SEP the thousands separator. A DECIMAL_POINT outside + 'char' range causes comparisons to act as if there is no decimal point character, and likewise for THOUSANDS_SEP. */ static inline int _GL_ATTRIBUTE_PURE |