[PATCH] Fix warning -Wchar-subscripts in isspace() and isdigit() macro

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Aug 3 13:25:20 UTC 2020


Hello Aschref,

On 03/08/2020 14:46, Aschref Ben-Thabet wrote:
> From: Aschref Ben Thabet <aschref.ben-thabet at embedded-brains.de>
>
> The warning may be because you're passing a char to the isspace() macro.
> The implementation of isspace() may be via array indexing.
> (The argument to the isspace()/isdegit() macro is defined to be an
> unsigned char the value of which can fit in an unsignedchar).
>
> at ctype.h in GNU libc, and while there's a complicated mess of macros
> and functions, somewhere in the definition of isspace() there's an array
> being indexed.
>
> See if isspace((unsigned char) *str) gets rid of the warning.
>
> The s[r + b] is very likely a bug, because the type char can be signed
> or unsigned—it's up to the compiler. If char is signed, then it's
> possible for c to be negative, in which case accessing a negative array
> index leads to Undefined Behavior.

I think this commit message contains several unrelated details. It 
should be sufficient to cite the C standard, e.g.

https://en.cppreference.com/w/c/string/byte/isdigit

"The behavior is undefined if the value of ch is not representable as 
unsigned char and is not equal to EOF."

> ---
>   cpukit/libdl/rtl-archive.c                 | 4 ++--
>   cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c | 2 +-
>   2 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/cpukit/libdl/rtl-archive.c b/cpukit/libdl/rtl-archive.c
> index eb7641b034..8f280157ff 100644
> --- a/cpukit/libdl/rtl-archive.c
> +++ b/cpukit/libdl/rtl-archive.c
> @@ -516,13 +516,13 @@ rtems_rtl_archives_load_config (rtems_rtl_archives* archives)
>         {
>           size_t ls = strlen (&s[r]);
>           size_t b = 0;
> -        while (b < ls && isspace (s[r + b]))
> +        while (b < ls && isspace((unsigned char)(s[r + b])))
>           {
>             s[r + b] = '\0';
>             ++b;
>           }
>           b = ls - 1;
> -        while (b > 0 && isspace (s[r + b]))
> +        while (b > 0 && isspace((unsigned char)(s[r + b])))
>           {
>             s[r + b] = '\0';
>             --b;
> diff --git a/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c b/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c
> index 32c9c6e17e..30002bc804 100644
> --- a/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c
> +++ b/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c
> @@ -338,7 +338,7 @@ rtems_fdt_shell_set (int argc, char *argv[])
>     if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
>       return 1;
>   
> -  if (isdigit((int)argv[mask_arg][0]))
> +  if (isdigit((unsigned char)argv[mask_arg][0]))
>       mask = strtoul (argv[mask_arg], 0, 0);
>     else
>     {

This patch is not against the master. Please generate a single commit 
fixing all the warnings:


../../../cpukit/libdl/rtl-archive.c:519:36: warning: array subscript has 
type 'char' [-Wchar-subscripts]
../../../cpukit/libdl/rtl-archive.c:525:35: warning: array subscript has 
type 'char' [-Wchar-subscripts]
../../../cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c:341:30: warning: 
array subscript has type 'char' [-Wchar-subscripts]
../../../cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c:383:30: warning: 
array subscript has type 'char' [-Wchar-subscripts]
../../../cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c:429:30: warning: 
array subscript has type 'char' [-Wchar-subscripts]
../../../cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c:476:30: warning: 
array subscript has type 'char' [-Wchar-subscripts]

Please make sure that your changes are in line with the coding style of 
the file.



More information about the devel mailing list