From d9b8412f0843d7cfc71ec6877381a49c19c30a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 20 Sep 2023 17:23:28 +0200 Subject: [PATCH 1/3] Add semantic patch to explicitly cast chars to unsigned for ctype.h Add a semantic patch to catch all the places where we pass 'char' to the family of functions (isalpha() and friends, toupper(), tolower()). While it generally works because the way how these functions are constructed in the libc, it's safer to do the explicit cast. (cherry picked from commit 5ec65ab5d082616716c94ebff94636daf1f789ac) --- cocci/ctype.spatch | 105 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 cocci/ctype.spatch diff --git a/cocci/ctype.spatch b/cocci/ctype.spatch new file mode 100644 index 0000000000..2b392cb310 --- /dev/null +++ b/cocci/ctype.spatch @@ -0,0 +1,105 @@ +@@ +char T; +@@ + +- isalnum(T) ++ isalnum((unsigned char)T) + +@@ +char T; +@@ + +- isalpha(T) ++ isalpha((unsigned char)T) + +@@ +char T; +@@ + +- iscntrl(T) ++ iscntrl((unsigned char)T) + +@@ +char T; +@@ + +- isdigit(T) ++ isdigit((unsigned char)T) + +@@ +char T; +@@ + +- isgraph(T) ++ isgraph((unsigned char)T) + +@@ +char T; +@@ + +- islower(T) ++ islower((unsigned char)T) + +@@ +char T; +@@ + +- isprint(T) ++ isprint((unsigned char)T) + +@@ +char T; +@@ + +- ispunct(T) ++ ispunct((unsigned char)T) + +@@ +char T; +@@ + +- isspace(T) ++ isspace((unsigned char)T) + +@@ +char T; +@@ + +- isupper(T) ++ isupper((unsigned char)T) + +@@ +char T; +@@ + +- isxdigit(T) ++ isxdigit((unsigned char)T) + +@@ +char T; +@@ + +- isascii(T) ++ isascii((unsigned char)T) + +@@ +char T; +@@ + +- isblank(T) ++ isblank((unsigned char)T) + +@@ +char T; +@@ + +- tolower(T) ++ tolower((unsigned char)T) + +@@ +char T; +@@ + +- toupper(T) ++ toupper((unsigned char)T) + From 818f4dc3a7c08267768f56e69b64ae360d8cc8e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 20 Sep 2023 17:23:28 +0200 Subject: [PATCH 2/3] Explicitly cast chars to unsigned chars for functions Apply the semantic patch to catch all the places where we pass 'char' to the family of functions (isalpha() and friends, toupper(), tolower()). (cherry picked from commit 29caa6d1f0f32002245abfa838a5eb00dd7ed4e1) --- contrib/dlz/modules/common/dlz_dbi.c | 2 +- lib/dns/rdata.c | 2 +- lib/isc/httpd.c | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/contrib/dlz/modules/common/dlz_dbi.c b/contrib/dlz/modules/common/dlz_dbi.c index 88ff6328f4..d8e1909c44 100644 --- a/contrib/dlz/modules/common/dlz_dbi.c +++ b/contrib/dlz/modules/common/dlz_dbi.c @@ -474,7 +474,7 @@ get_parameter_value(const char *input, const char *key) { for (i = 0; i < 255; i++) { value[i] = keystart[keylen + i]; - if (isspace(value[i]) || value[i] == '\0') { + if (isspace((unsigned char)value[i]) || value[i] == '\0') { value[i] = '\0'; break; } diff --git a/lib/dns/rdata.c b/lib/dns/rdata.c index b7f9ed2b61..592b9746da 100644 --- a/lib/dns/rdata.c +++ b/lib/dns/rdata.c @@ -2059,7 +2059,7 @@ decvalue(char value) { * isascii() is valid for full range of int values, no need to * mask or cast. */ - if (!isascii(value)) { + if (!isascii((unsigned char)value)) { return (-1); } if ((s = strchr(decdigits, value)) == NULL) { diff --git a/lib/isc/httpd.c b/lib/isc/httpd.c index b15cc45448..a93f9e1393 100644 --- a/lib/isc/httpd.c +++ b/lib/isc/httpd.c @@ -340,8 +340,10 @@ value_match(const struct phr_header *header, const char *match) { limit = header->value_len - match_len + 1; for (size_t i = 0; i < limit; i++) { - if (isspace(header->value[i])) { - while (i < limit && isspace(header->value[i])) { + if (isspace((unsigned char)header->value[i])) { + while (i < limit && + isspace((unsigned char)header->value[i])) + { i++; } continue; From cb57e77c68aab9006b670f8246ee9a170518ffd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 20 Sep 2023 17:42:28 +0200 Subject: [PATCH 3/3] Add CHANGES note for [GL #4327] (cherry picked from commit 0e49a8422fa54020d2d7543ab02bcd2049b4afff) --- CHANGES | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES b/CHANGES index 6e792110d8..e85aae8e22 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +6254. [cleanup] Add semantic patch to do an explicit cast from char + to unsigned char in ctype.h class of functions. + [GL #4327] + 6252. [test] Python system tests have to be executed by invoking pytest directly. Executing them with the legacy test runner is no longer supported. [GL #4250]