]> granicus.if.org Git - curl/commitdiff
Avoid typecasting a signed char to an int when using is*() functions, as that
authorDaniel Stenberg <daniel@haxx.se>
Tue, 17 Oct 2006 21:32:56 +0000 (21:32 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 17 Oct 2006 21:32:56 +0000 (21:32 +0000)
could very well cause a negate number get passed in and thus cause reading
outside of the array usually used for this purpose.

We avoid this by using the uppercase macro versions introduced just now that
does some extra crazy typecasts to avoid byte codes > 127 to cause negative
int values.

17 files changed:
lib/base64.c
lib/escape.c
lib/ftp.c
lib/http.c
lib/http_chunks.c
lib/http_digest.c
lib/http_negotiate.c
lib/http_ntlm.c
lib/mprintf.c
lib/parsedate.c
lib/setup.h
lib/strtoofft.c
lib/transfer.c
lib/url.c
src/main.c
src/setup.h
src/urlglob.c

index c9e8a382c70b9c6eae4b91085efd335ddd04de74..2302eb0145a67ab5dd427ab05f44231434e8ba64 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -280,7 +280,7 @@ int main(int argc, char **argv, char **envp)
 
     for(j=0; j < 0x10; j++)
       if((j+i) < dataLen)
-        printf("%c", isgraph(data[i+j])?data[i+j]:'.');
+        printf("%c", ISGRAPH(data[i+j])?data[i+j]:'.');
       else
         break;
     puts("");
index c569902f6dd8c902c1d97210e6dbaf62156c497b..9552b0f31652eef663eecf704baaa9f5d3f63655 100644 (file)
@@ -116,10 +116,6 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
   return ns;
 }
 
-#define ishex(in) ((in >= 'a' && in <= 'f') || \
-                   (in >= 'A' && in <= 'F') || \
-                   (in >= '0' && in <= '9'))
-
 char *curl_easy_unescape(CURL *handle, const char *string, int length,
                          int *olen)
 {
@@ -138,7 +134,7 @@ char *curl_easy_unescape(CURL *handle, const char *string, int length,
 
   while(--alloc > 0) {
     in = *string;
-    if(('%' == in) && ishex(string[1]) && ishex(string[2])) {
+    if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
       /* this is two hexadecimal digits following a '%' */
       char hexstr[3];
       char *ptr;
index eb945ef0bb7b971c2b3931755443e622aa6e8695..e3d14a2ba212cb6b9e9f6623a32928d51cff2ad8 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -252,8 +252,8 @@ static void ftp_respinit(struct connectdata *conn)
 }
 
 /* macro to check for the last line in an FTP server response */
-#define lastline(line) (isdigit((int)line[0]) && isdigit((int)line[1]) && \
-                        isdigit((int)line[2]) && (' ' == line[3]))
+#define lastline(line) (ISDIGIT(line[0]) && ISDIGIT(line[1]) && \
+                        ISDIGIT(line[2]) && (' ' == line[3]))
 
 static CURLcode ftp_readresp(curl_socket_t sockfd,
                              struct connectdata *conn,
@@ -2177,7 +2177,7 @@ static CURLcode ftp_state_get_resp(struct connectdata *conn,
           if('(' == *bytes)
             break;
           /* skip only digits */
-          if(!isdigit((int)*bytes)) {
+          if(!ISDIGIT(*bytes)) {
             bytes=NULL;
             break;
           }
@@ -3208,7 +3208,7 @@ static CURLcode ftp_range(struct connectdata *conn)
 
   if(data->reqdata.use_range && data->reqdata.range) {
     from=curlx_strtoofft(data->reqdata.range, &ptr, 0);
-    while(ptr && *ptr && (isspace((int)*ptr) || (*ptr=='-')))
+    while(ptr && *ptr && (ISSPACE(*ptr) || (*ptr=='-')))
       ptr++;
     to=curlx_strtoofft(ptr, &ptr2, 0);
     if(ptr == ptr2) {
index 0b3111f473de31608cbdb56d014feee4aefed977..e31730e7d999a1fcd028a14743810f58b8b4c84a 100644 (file)
@@ -569,7 +569,7 @@ CURLcode Curl_http_input_auth(struct connectdata *conn,
   }
 
   /* pass all white spaces */
-  while(*start && isspace((int)*start))
+  while(*start && ISSPACE(*start))
     start++;
 
   /*
@@ -1051,7 +1051,7 @@ Curl_compareheader(char *headerline,    /* line to check */
   start = &headerline[hlen];
 
   /* pass all white spaces */
-  while(*start && isspace((int)*start))
+  while(*start && ISSPACE(*start))
     start++;
 
   /* find the end of the header line */
@@ -1558,7 +1558,7 @@ static CURLcode add_custom_headers(struct connectdata *conn,
       /* we require a colon for this to be a true header */
 
       ptr++; /* pass the colon */
-      while(*ptr && isspace((int)*ptr))
+      while(*ptr && ISSPACE(*ptr))
         ptr++;
 
       if(*ptr) {
@@ -1725,12 +1725,12 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
        redirected request is being out on thin ice. Except if the host name
        is the same as the first one! */
     char *start = ptr+strlen("Host:");
-    while(*start && isspace((int)*start ))
+    while(*start && ISSPACE(*start ))
       start++;
     ptr = start; /* start host-scanning here */
 
     /* scan through the string to find the end (space or colon) */
-    while(*ptr && !isspace((int)*ptr) && !(':'==*ptr))
+    while(*ptr && !ISSPACE(*ptr) && !(':'==*ptr))
       ptr++;
 
     if(ptr != start) {
index 8e9947f5d2975fd9a179e855b4c005621860ebcb..f398b100cc7f2be4cd66f6056a06a3e02bd146ad 100644 (file)
@@ -115,7 +115,7 @@ CHUNKcode Curl_httpchunk_read(struct connectdata *conn,
   while(length) {
     switch(ch->state) {
     case CHUNK_HEX:
-      if(isxdigit((int)*datap)) {
+      if(ISXDIGIT(*datap)) {
         if(ch->hexindex < MAXNUM_SIZE) {
           ch->hexbuffer[ch->hexindex] = *datap;
           datap++;
index e1aec3919faf888c1466b79125128e9dff245b28..8b605d5c566b9038f3927cc842829184cfa28202 100644 (file)
@@ -75,7 +75,7 @@ CURLdigest Curl_input_digest(struct connectdata *conn,
   }
 
   /* skip initial whitespaces */
-  while(*header && isspace((int)*header))
+  while(*header && ISSPACE(*header))
     header++;
 
   if(checkprefix("Digest", header)) {
@@ -93,7 +93,7 @@ CURLdigest Curl_input_digest(struct connectdata *conn,
       char content[128];
       size_t totlen=0;
 
-      while(*header && isspace((int)*header))
+      while(*header && ISSPACE(*header))
         header++;
 
       /* how big can these strings be? */
index 70062f85ae33ca78cc9bc14715834bf8b3dd1fa0..eb5bd92d1be87ea258a1030f6006c99c16e8363b 100644 (file)
@@ -124,7 +124,7 @@ int Curl_input_negotiate(struct connectdata *conn, char *header)
   bool gss;
   const char* protocol;
 
-  while(*header && isspace((int)*header))
+  while(*header && ISSPACE(*header))
     header++;
   if(checkprefix("GSS-Negotiate", header)) {
     protocol = "GSS-Negotiate";
@@ -160,7 +160,7 @@ int Curl_input_negotiate(struct connectdata *conn, char *header)
     return ret;
 
   header += strlen(neg_ctx->protocol);
-  while(*header && isspace((int)*header))
+  while(*header && ISSPACE(*header))
     header++;
 
   len = strlen(header);
index 7444a400c2ce34e161c6afe59c508ba46786077e..8205a8fea12fe0752a3c3de2708852b448bf5265 100644 (file)
@@ -218,13 +218,13 @@ CURLntlm Curl_input_ntlm(struct connectdata *conn,
   ntlm = proxy?&conn->proxyntlm:&conn->ntlm;
 
   /* skip initial whitespaces */
-  while(*header && isspace((int)*header))
+  while(*header && ISSPACE(*header))
     header++;
 
   if(checkprefix("NTLM", header)) {
     header += strlen("NTLM");
 
-    while(*header && isspace((int)*header))
+    while(*header && ISSPACE(*header))
       header++;
 
     if(*header) {
index 543a39f150674ca09daf951677fd184b9dff55be..6103953185c5c3237dc0210be2cfa16182bdfc81 100644 (file)
@@ -171,7 +171,7 @@ int curl_msprintf(char *buffer, const char *format, ...);
 static long dprintf_DollarString(char *input, char **end)
 {
   int number=0;
-  while(isdigit((int)*input)) {
+  while(ISDIGIT(*input)) {
     number *= 10;
     number += *input-'0';
     input++;
index 37b4ddfe362a9daf1af4a133c1ac1f7cf9216a84..0bb6d0c5aa6fac79a67b177f28844bfb8f2a1399 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -213,7 +213,7 @@ static int checktz(char *check)
 static void skip(const char **date)
 {
   /* skip everything that aren't letters or digits */
-  while(**date && !isalnum((int)**date))
+  while(**date && !ISALNUM(**date))
     (*date)++;
 }
 
@@ -256,7 +256,7 @@ static time_t Curl_parsedate(const char *date)
 
     skip(&date);
 
-    if(isalpha((int)*date)) {
+    if(ISALPHA(*date)) {
       /* a name coming up */
       char buf[32]="";
       size_t len;
@@ -286,7 +286,7 @@ static time_t Curl_parsedate(const char *date)
 
       date += len;
     }
-    else if(isdigit((int)*date)) {
+    else if(ISDIGIT(*date)) {
       /* a digit */
       int val;
       char *end;
index a02834ecdc8470c5839306e7050db06401b1a5cd..4f1a3c1cd091711b163b82de0c622133bb81ba67 100644 (file)
@@ -348,6 +348,16 @@ int fileno( FILE *stream);
 #define DEBUGF(x)
 #endif
 
+#ifndef ISSPACE
+/* typecasting craze to avoid negative number inputs to these macros */
+#define ISSPACE(x) (isspace((int)((unsigned char)x)))
+#define ISDIGIT(x) (isdigit((int)((unsigned char)x)))
+#define ISALNUM(x) (isalnum((int)((unsigned char)x)))
+#define ISXDIGIT(x) (isxdigit((int)((unsigned char)x)))
+#define ISGRAPH(x) (isgraph((int)((unsigned char)x)))
+#define ISALPHA(x) (isalpha((int)((unsigned char)x)))
+#endif
+
 /*
  * Include macros and defines that should only be processed once.
  */
index b11755b1bca7e8d00528f7a3c38821523ce427cd..3ab1bfdff29399860775f5d03f25bb179319ee03 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -55,7 +55,7 @@ curlx_strtoll(const char *nptr, char **endptr, int base)
 
   /* Skip leading whitespace. */
   end = (char *)nptr;
-  while (isspace((int)end[0])) {
+  while (ISSPACE(end[0])) {
     end++;
   }
 
index e2df5fc122d38190c50b3a0e14e75701aec71b81..25645c97d60b106f3d7a97d13c5dd87cc8a1e99e 100644 (file)
@@ -762,7 +762,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
 
               /* Find the first non-space letter */
               for(start=k->p+13;
-                  *start && isspace((int)*start);
+                  *start && ISSPACE(*start);
                   start++)
                 ;  /* empty loop */
 
@@ -772,7 +772,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
 
               if(end) {
                 /* skip all trailing space letters */
-                for(; isspace((int)*end) && (end > start); end--)
+                for(; ISSPACE(*end) && (end > start); end--)
                   ;  /* empty loop */
 
                 /* get length of the type */
@@ -877,7 +877,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
 
               /* Find the first non-space letter */
               for(start=k->p+17;
-                  *start && isspace((int)*start);
+                  *start && ISSPACE(*start);
                   start++)
                 ;  /* empty loop */
 
@@ -957,7 +957,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
 
                 /* Skip spaces and tabs. We do this to support multiple
                    white spaces after the "Location:" keyword. */
-                while(*start && isspace((int)*start ))
+                while(*start && ISSPACE(*start ))
                   start++;
 
                 /* Scan through the string from the end to find the last
@@ -966,7 +966,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
                    there. This logic strips off trailing whitespace, but keeps
                    any embedded whitespace. */
                 ptr = k->end_ptr-1;
-                while((ptr>=start) && isspace((int)*ptr))
+                while((ptr>=start) && ISSPACE(*ptr))
                   ptr--;
                 ptr++;
 
index 312862dcba75df9cb6f61e15086a7118f6271689..d236605156e4ee78755090205ae523b3527505d8 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -3252,7 +3252,7 @@ static CURLcode CreateConnection(struct SessionHandle *data,
     /* detect and extract RFC2732-style IPv6-addresses */
     if(*proxyptr == '[') {
       char *ptr = ++proxyptr; /* advance beyond the initial bracket */
-      while(*ptr && (isxdigit((int)*ptr) || (*ptr == ':')))
+      while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':')))
         ptr++;
       if(*ptr == ']') {
         /* yeps, it ended nicely with a bracket as well */
index af3f4355f52cca99d8b4f2633d4e1a0c1f19e8c2..24eec6b34ccf1b980ec51c47928864b7284d2fe3 100644 (file)
@@ -380,7 +380,7 @@ static void warnf(struct Configurable *config, const char *fmt, ...)
       if(len > (int)WARN_TEXTWIDTH) {
         int cut = WARN_TEXTWIDTH-1;
 
-        while(!isspace((int)ptr[cut]) && cut) {
+        while(!ISSPACE(ptr[cut]) && cut) {
           cut--;
         }
 
@@ -933,7 +933,7 @@ static int formparse(struct Configurable *config,
           while(ptr && (FORM_FILE_SEPARATOR!= *ptr)) {
 
             /* pass all white spaces */
-            while(isspace((int)*ptr))
+            while(ISSPACE(*ptr))
               ptr++;
 
             if(curlx_strnequal("type=", ptr, 5)) {
@@ -1155,7 +1155,7 @@ static void cleanarg(char *str)
 static int str2num(long *val, char *str)
 {
   int retcode = 0;
-  if(isdigit((int)*str))
+  if(ISDIGIT(*str))
     *val = atoi(str);
   else
     retcode = 1; /* badness */
@@ -1961,7 +1961,7 @@ static ParameterError getparameter(char *flag, /* f or -long-flag */
           if(ptr &&
              (ptr == &nextarg[1]) &&
              (nextarg[2] == '\\' || nextarg[2] == '/') &&
-             (isalpha((int)nextarg[0])) )
+             (ISALPHA(nextarg[0])) )
              /* colon in the second column, followed by a backslash, and the
                 first character is an alphabetic letter:
 
@@ -2463,7 +2463,7 @@ static int parseconfig(const char *filename,
     int lineno=0;
     bool alloced_param;
 
-#define isseparator(x) (((x)=='=') || ((x) == ':'))
+#define ISSEP(x) (((x)=='=') || ((x) == ':'))
 
     while (NULL != (aline = my_get_line(file))) {
       lineno++;
@@ -2471,7 +2471,7 @@ static int parseconfig(const char *filename,
       alloced_param=FALSE;
 
       /* lines with # in the fist column is a comment! */
-      while(*line && isspace((int)*line))
+      while(*line && ISSPACE(*line))
         line++;
 
       switch(*line) {
@@ -2487,7 +2487,7 @@ static int parseconfig(const char *filename,
 
       /* the option keywords starts here */
       option = line;
-      while(*line && !isspace((int)*line) && !isseparator(*line))
+      while(*line && !ISSPACE(*line) && !ISSEP(*line))
         line++;
       /* ... and has ended here */
 
@@ -2499,7 +2499,7 @@ static int parseconfig(const char *filename,
 #endif
 
       /* pass spaces and separator(s) */
-      while(*line && (isspace((int)*line) || isseparator(*line)))
+      while(*line && (ISSPACE(*line) || ISSEP(*line)))
         line++;
 
       /* the parameter starts here (unless quoted) */
@@ -2544,7 +2544,7 @@ static int parseconfig(const char *filename,
       }
       else {
         param=line; /* parameter starts here */
-        while(*line && !isspace((int)*line))
+        while(*line && !ISSPACE(*line))
           line++;
         *line=0; /* zero terminate */
       }
@@ -2852,7 +2852,8 @@ convert_from_network(char *buffer, size_t length)
 }
 
 static
-char convert_char(curl_infotype infotype, char this_char) {
+char convert_char(curl_infotype infotype, char this_char)
+{
 /* determine how this specific character should be displayed */
   switch(infotype) {
   case CURLINFO_DATA_IN:
@@ -2863,24 +2864,25 @@ char convert_char(curl_infotype infotype, char this_char) {
     if ((this_char >= 0x20) && (this_char < 0x7f)) {
       /* printable ASCII hex value: convert to host encoding */
       convert_from_network(&this_char, 1);
-    } else {
+    }
+    else {
       /* non-printable ASCII, use a replacement character */
-      return(UNPRINTABLE_CHAR);
+      return UNPRINTABLE_CHAR;
     }
     /* fall through to default */
   default:
     /* treat as host encoding */
-    if (isprint(this_char)
-    &&  (this_char != '\t')
-    &&  (this_char != '\r')
-    &&  (this_char != '\n')) {
+    if (ISPRINT(this_char)
+        &&  (this_char != '\t')
+        &&  (this_char != '\r')
+        &&  (this_char != '\n')) {
       /* printable characters excluding tabs and line end characters */
-      return(this_char);
+      return this_char;
     }
     break;
   }
   /* non-printable, use a replacement character  */
-  return(UNPRINTABLE_CHAR);
+  return UNPRINTABLE_CHAR;
 }
 #endif /* CURL_DOES_CONVERSIONS */
 
index 1acd19e85624650712ed3d6c49deab00f09f57cd..a8f636974ff12d59c1fa1a07ee5bb6651f26cff6 100644 (file)
@@ -7,7 +7,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -184,4 +184,16 @@ int fileno( FILE *stream);
 #define strdup(ptr) curlx_strdup(ptr)
 #endif
 
+#ifndef ISSPACE
+/* typecasting craze to avoid negative number inputs to these macros */
+/* copied from lib/setup.h */
+#define ISSPACE(x) (isspace((int)((unsigned char)x)))
+#define ISDIGIT(x) (isdigit((int)((unsigned char)x)))
+#define ISALNUM(x) (isalnum((int)((unsigned char)x)))
+#define ISXDIGIT(x) (isxdigit((int)((unsigned char)x)))
+#define ISGRAPH(x) (isgraph((int)((unsigned char)x)))
+#define ISALPHA(x) (isalpha((int)((unsigned char)x)))
+#define ISPRINT(x) (isprint((int)((unsigned char)x)))
+#endif
+
 #endif /* __SRC_CURL_SETUP_H */
index d5d3f4eaee51b2a776014af9ddaeee96b69619e8..ba4fb1eae4f6a6cfd47f51dbb445921cd37a0378 100644 (file)
@@ -177,7 +177,7 @@ static GlobCode glob_range(URLGlob *glob, char *pattern,
   /* patterns 0,1,2,... correspond to size=1,3,5,... */
   ++glob->size;
 
-  if (isalpha((int)*pattern)) {         /* character range detected */
+  if (ISALPHA(*pattern)) {         /* character range detected */
     char min_c;
     char max_c;
 
@@ -205,7 +205,7 @@ static GlobCode glob_range(URLGlob *glob, char *pattern,
     pat->content.CharRange.ptr_c = pat->content.CharRange.min_c = min_c;
     pat->content.CharRange.max_c = max_c;
   }
-  else if (isdigit((int)*pattern)) { /* numeric range detected */
+  else if (ISDIGIT(*pattern)) { /* numeric range detected */
     int min_n;
     int max_n;
 
@@ -229,9 +229,11 @@ static GlobCode glob_range(URLGlob *glob, char *pattern,
 
     if (*pattern == '0') {              /* leading zero specified */
       c = pattern;
-      while (isdigit((int)*c++))
+      while (ISDIGIT(*c)) {
+        c++;
         ++pat->content.NumRange.padlength; /* padding length is set for all
                                               instances of this pattern */
+      }
     }
 
   }
@@ -498,7 +500,7 @@ char *glob_match_url(char *filename, URLGlob *glob)
     return NULL; /* major failure */
 
   while (*filename) {
-    if (*filename == '#' && isdigit((int)filename[1])) {
+    if (*filename == '#' && ISDIGIT(filename[1])) {
       unsigned long i;
       char *ptr = filename;
       unsigned long num = strtoul(&filename[1], &filename, 10);