delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2004/02/23/15:29:11

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Message-ID: <403A5F55.7E608910@yahoo.com>
Date: Mon, 23 Feb 2004 15:15:17 -0500
From: CBFalconer <cbfalconer AT yahoo DOT com>
Organization: Ched Research
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp-workers <djgpp-workers AT delorie DOT com>
Subject: strtoul bug (was Re: Fibonacci number)
References: <d7c3a0b2 DOT 0402220759 DOT 34d6435d AT posting DOT google DOT com> <4038E8CA DOT 6491815E AT virginia DOT edu> <4039DD96 DOT 3F36F3B7 AT yahoo DOT com> <200402231458 DOT i1NEwKwm020904 AT envy DOT delorie DOT com> <403A301C DOT E8F5FE65 AT yahoo DOT com> <200402231751 DOT i1NHp5lv022894 AT envy DOT delorie DOT com>
Reply-To: djgpp-workers AT delorie DOT com

DJ Delorie wrote:
> 
> > That isn't a range error IMO - the initial '-' is an illegal char,
> > so it shouldn't convert anything, and should return 0.  From N869:
> >
> >   [#4] The subject sequence is defined as the longest  initial
> >   subsequence  of  the  input  string, starting with the first
> >   non-white-space character, that is  of  the  expected  form.
> 
> ISO 9899/1999 explicitly allows '-' to be part of the expected form
> for strtoul.  Negative numbers are outside its range.
> 
> > In either case the 2.03 version of strtoul does NOT return ULONG_MAX
> > for -2, it simply performs the usual conversion modulo ULONG_MAX+1.
> 
> Ok, *that* is a bug.  Amusingly enough, glibc 2.3.2 has the same bug.
> 
> At this point, I think the right next step is to create a test suite
> for these functions and post it to djgpp-workers (no need to subject
> the regular list with such detail).  Once we agree on what the right
> results are, we can proceed with fixing them.

Moved from c.o.m.djgpp.  Here is a test suite:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv)
{
   unsigned int n;
   int          i;
   char         s[25];
   char        *rem;

   if (2 != argc) puts("Usage: djbug N");
   else {
      n = strtoul(argv[1], NULL, 10);
      if (n > 10) puts("Max. N is 10");
      else {
         printf("Neg args should yield %lu and error\n",
                            (unsigned long)-1);
         for (i = -n; i <= (int)n; i++) {
            sprintf(s, "%djunk", i);
            errno = 0;
            printf("strtoul(\"%s\") = %lu",
                      s, strtoul(s, &rem, 10));
            printf(" remnant \"%s\"\n", rem);
            if (errno) perror("Error");
         }
      }
   }
   return 0;
} /* main */

-- 
Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT worldnet DOT att DOT net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019