[PATCH v3] Fix warning in isspace() and isdigit().

Aschref Ben-Thabet aschref.ben-thabet at embedded-brains.de
Mon Jul 20 15:17:57 UTC 2020


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 line array[c] 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.
---
 cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c b/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c
index b15103e53d..32c9c6e17e 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 (argv[mask_arg][0]))
+  if (isdigit((int)argv[mask_arg][0]))
     mask = strtoul (argv[mask_arg], 0, 0);
   else
   {
@@ -380,7 +380,7 @@ rtems_fdt_shell_cl (int argc, char *argv[])
   if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
     return 1;
 
-  if (isdigit (argv[mask_arg][0]))
+  if (isdigit((unsigned char)argv[mask_arg][0]))
     mask = strtoul (argv[mask_arg], 0, 0);
   else
   {
@@ -426,7 +426,7 @@ rtems_fdt_shell_up (int argc, char *argv[])
   if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
     return 1;
 
-  if (isdigit (argv[mask_arg][0]))
+  if (isdigit((unsigned char)argv[mask_arg][0]))
     mask = strtoul (argv[mask_arg], 0, 0);
   else
   {
@@ -473,7 +473,7 @@ rtems_fdt_shell_tst (int argc, char *argv[])
   if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
     return 1;
 
-  if (isdigit (argv[mask_arg][0]))
+  if (isdigit ((unsigned char) argv[mask_arg][0]))
     mask = strtoul (argv[mask_arg], 0, 0);
   else
   {
-- 
2.26.2



More information about the devel mailing list