From: Bruno Haible Date: Tue, 8 Dec 2020 18:02:02 +0000 (+0100) Subject: argp: Don't rely on undefined behaviour of _tolower(). X-Git-Tag: v1.0~3401 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=2c36f42b2f4e53adc3bc3a73a46be47550999676;p=gnulib.git argp: Don't rely on undefined behaviour of _tolower(). Patch by Eric Blake . * lib/argp-help.c (hol_entry_cmp): Don't use _tolower on values that are not upper-case. Pass correct range to tolower. --- diff --git a/ChangeLog b/ChangeLog index 5992c891d9..eaea8df7ca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2020-12-08 Bruno Haible + + argp: Don't rely on undefined behaviour of _tolower(). + Patch by Eric Blake + . + * lib/argp-help.c (hol_entry_cmp): Don't use _tolower on values that are + not upper-case. Pass correct range to tolower. + 2020-12-07 Bruno Haible unicodeio: Fix wrong result on FreeBSD. diff --git a/lib/argp-help.c b/lib/argp-help.c index 52260f35e8..03fc6c79eb 100644 --- a/lib/argp-help.c +++ b/lib/argp-help.c @@ -789,13 +789,11 @@ hol_entry_cmp (const struct hol_entry *entry1, first, but as they're not displayed, it doesn't matter where they are. */ { - char first1 = short1 ? short1 : long1 ? *long1 : 0; - char first2 = short2 ? short2 : long2 ? *long2 : 0; -#ifdef _tolower - int lower_cmp = _tolower (first1) - _tolower (first2); -#else + unsigned char first1 = short1 ? short1 : long1 ? *long1 : 0; + unsigned char first2 = short2 ? short2 : long2 ? *long2 : 0; + /* Use tolower, not _tolower, since the latter has undefined + behaviour for characters that are not uppercase letters. */ int lower_cmp = tolower (first1) - tolower (first2); -#endif /* Compare ignoring case, except when the options are both the same letter, in which case lower-case always comes first. */ return lower_cmp ? lower_cmp : first2 - first1;