Print this page
cpp: don't handroll an incorrect strtoul(3c), use the one in libc
The previous implementation would parse 0x7ff as 0x755 (etc).

Split Close
Expand all
Collapse all
          --- old/cpp/yylex.c
          +++ new/cpp/yylex.c
↓ open down ↓ 72 lines elided ↑ open up ↑
  73   73   */
  74   74  #define isid(a)  ((fastab+COFF)[(int)a]&IB)
  75   75  #define IB 1
  76   76  /*      #if '\377' < 0          it would be nice if this worked properly!!!!! */
  77   77  #if pdp11 | vax | '\377' < 0
  78   78  #define COFF 128
  79   79  #else
  80   80  #define COFF 0
  81   81  #endif
  82   82  
  83      -static  int     tobinary(char *, int);
       83 +static  long    tobinary(char *, int);
  84   84  
  85   85  int
  86   86  yylex(void)
  87   87  {
  88   88          static int ifdef=0;
  89   89          static char *op2[]={"||",  "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
  90   90          static int  val2[]={OROR, ANDAND,  RS,   LS,   GE,   LE,   NE,   EQ};
  91   91          static char *opc="b\bt\tn\nf\fr\r\\\\";
  92   92          extern char fastab[];
  93   93          extern char *outp,*inp,*newp; extern int flslvl;
↓ open down ↓ 34 lines elided ↑ open up ↑
 128  128          } else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
 129  129          else {
 130  130                  *newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
 131  131                  continue;
 132  132          }
 133  133  ret:
 134  134          *newp=savc; outp=inp=newp; return(val);
 135  135  }
 136  136  }
 137  137  
 138      -static int
 139      -tobinary(st, b)
 140      -        char    *st;
 141      -        int     b;
      138 +static long
      139 +tobinary(char *st, int b)
 142  140  {
 143      -        int n, c, t;
 144      -        char *s;
 145      -        n=0;
 146      -        s=st;
 147      -        while ((c = *s++) != '\0') {
 148      -        switch(c) {
 149      -                case '0': case '1': case '2': case '3': case '4': 
 150      -                case '5': case '6': case '7': case '8': case '9': 
 151      -                        t = c-'0'; break;
 152      -                case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 
 153      -                        t = c-'a'; if (b>10) break;
 154      -                case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 
 155      -                        t = c - 'A'; if (b>10) break;
 156      -                default:
 157      -                        t = -1;
 158      -                        if ( c=='l' || c=='L') if (*s=='\0') break;
 159      -                        pperror("Illegal number %s", st);
      141 +        char *tmp;
      142 +        int n;
      143 +        n = strtoul(st, &tmp, b);
      144 +        if (*tmp != '\0') {
      145 +                if ((strcasecmp(tmp, "L") != 0) &&
      146 +                    (strcasecmp(tmp, "LL") != 0) &&
      147 +                    (strcasecmp(tmp, "UL") != 0) &&
      148 +                    (strcasecmp(tmp, "ULL") != 0))
      149 +                        pperror("illegal number: %s", st);
 160  150          }
 161      -        if (t<0) break;
 162      -        n = n*b+t;
 163      -        }
 164      -return(n);
      151 +        return(n);
 165  152  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX