[ovs-dev] [branch-1.1 17/34] Fix calls to ctype functions.

Ben Pfaff blp at nicira.com
Fri Apr 29 21:56:48 UTC 2011

The ctype functions often need casts to be fully C standards compliant.
Here's the full explanation that I used to post to comp.lang.c from time
to time when the issue came up:

    With the to*() and is*() functions, you should be careful to cast
    `char' arguments to `unsigned char' before calling them.  Type `char'
    may be signed or unsigned, depending on your compiler or its
    configuration.  If `char' is signed, then some characters have
    negative values; however, the arguments to is*() and to*() functions
    must be nonnegative (or EOF).  Casting to `unsigned char' fixes this
    problem by forcing the character to the corresponding positive value.

This fixes the following warnings from some version of GCC:

lib/ofp-parse.c:828: warning: array subscript has type 'char'
lib/ofp-print.c:617: warning: array subscript has type 'char'

Reported-by: YAMAMOTO Takashi <yamamoto at valinux.co.jp>
 AUTHORS         |    1 +
 lib/ofp-parse.c |    3 ++-
 lib/ofp-print.c |    2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index bc0f3f6..180428a 100644
@@ -80,6 +80,7 @@ Stephen Hemminger       shemminger at vyatta.com
 Takayuki HAMA           t-hama at cb.jp.nec.com
 Teemu Koponen           koponen at nicira.com
 Vishal Swarankar        vishal.swarnkar at gmail.com
+YAMAMOTO Takashi        yamamoto at valinux.co.jp
 Yongqiang Liu           liuyq7809 at gmail.com
 kk yap                  yapkke at stanford.edu
diff --git a/lib/ofp-parse.c b/lib/ofp-parse.c
index de7cd9c..a3492cb 100644
--- a/lib/ofp-parse.c
+++ b/lib/ofp-parse.c
@@ -820,7 +820,8 @@ parse_ofp_str(struct flow_mod *fm, uint8_t *table_idx,
                 } else {
                     parse_field_value(&fm->cr, f->index, value);
-            } else if (!strncmp(name, "reg", 3) && isdigit(name[3])) {
+            } else if (!strncmp(name, "reg", 3)
+                       && isdigit((unsigned char) name[3])) {
                 unsigned int reg_idx = atoi(name + 3);
                 if (reg_idx >= FLOW_N_REGS) {
                     ovs_fatal(0, "only %d registers supported", FLOW_N_REGS);
diff --git a/lib/ofp-print.c b/lib/ofp-print.c
index 1cd9462..32e8e7c 100644
--- a/lib/ofp-print.c
+++ b/lib/ofp-print.c
@@ -603,7 +603,7 @@ ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
     memcpy(name, port->name, sizeof name);
     for (j = 0; j < sizeof name - 1; j++) {
-        if (!isprint(name[j])) {
+        if (!isprint((unsigned char) name[j])) {

More information about the dev mailing list