Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #59596

Re: strtoll discussion

From Tim Rentsch <txr@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: strtoll discussion
Date 2015-04-04 13:04 -0700
Organization A noiseless patient Spider
Message-ID <kfnsicf3bk4.fsf@x-alumni2.alumni.caltech.edu> (permalink)
References <mfm0uu$i18$1@dont-email.me>

Show all headers | View raw


jacobnavia <jacob@jacob.remcomp.fr> writes:

> I have written a document about the discusssion that we had in
> this group.  It would be nice to give to newcomers here, so that
> they can read non-trivial code that is well documented.  [snip]
>
> The URL is:  http://www.cs.virginia.edu/~lcc-win32/strtoll.pdf

I have a question on the text, and several comments on
the code.

In the text, you gave no mention of the method I described
for preventing overflow.  Is that because you decided not to
include it, or did you not see the posting?

For the code, here are several comments and corrections.

On page 7, the return types of strtol() and strtoul() are
reversed.  Also the names of the flags are a little bit
misleading, since [un]signed long may not be 32 bits.

On page 8, I recommend moving line 37, ie,

    if ( endptr ) *endptr = (char *) input;

after lines 49-53, ie, after

    if ( base < 0 || base == 1 || base > 36
       || input == NULL ) {
	errno = ERANGE ;
	return 0;
    }

which is more in keeping (AIUI) with POSIX and with other
implementations.  Related to that, the value ERANGE for errno on
line 51 is not really the best choice here, since the problem is
not a range error but a domain error.  Using EINVAL is a better
match to the actual circumstance, and also is more in keeping
(again AIUI) with POSIX and with other implementations.

On page 9, for lines 62-66, ie, this text

    if (*input == '0') {
	input++; /* Swallow the zero */
	ReadSomething = 1;
	if( (base == 0 || base == 16) &&
	   (input[1] == 'X') || (input[1] == 'x')) {

the condition in the second 'if' has several bugs.  First the
indexing of 'input' should be 'index[0]', not 'index[1]'.
Second the second line is mis-parenthesized -- the two
parentheses closest to the '||' should be removed.  Third there
needs to be another condition added, to be sure the digit (if
any) following the 'x' or 'X' is a hexadecimal digit.  Putting
these together the second 'if' test should be more like this:

	if( (base == 0 || base == 16) &&
	   (input[0] == 'X' || input[0] == 'x') &&
           is_hex_digit( input[1] )
        ) {

so that inputs starting '0x' but without a following hexadecimal
digit are recognized correctly, that is, as just '0' and nothing
else.  (The function is_hex_digit() does what its name implies.)

On page 10, line 83, there is this assignment

    nbmax = (char)((char)'0' + (char)(base - 1));

All of the casts here are unnecessary.  In fact nearly all of the
casts in the StrtoXXX() function are unnecessary;  the only
exceptions are the two (char*) casts in assigning to 'endptr'.

On line 87, ie, the assignment

    cbmaxLower = (char)('a' + (base - 11));

(which also has a superfluous cast), the code is operating on the
tacit assumption that lower case letters are contiguous (and
increasing).  The assumption is okay but I suggest it deserves
a comment or a test of some sort (such as assert()).  So at least
this:

    cbmaxLower = 'a' + (base - 11);
    // test:  cbmaxLower == "0123456789abcdefghijklmnopqrstuvwxyz"[ base-1 ]

which both states the test and also serves to convey just what
relationship is being calculated.  There is an analogous change
for line 90.

On lines 97-98, ie, these two assignments

n    maxval = LLONG_MAX ;
    errval = (unsigned long long) LLONG_MIN;

the cast is again superfluous, but more importantly the setting
of 'maxval' is wrong.  These lines may be correctly written as

    maxval = -(LLONG_MIN+1) + 1ULL;
    errval = LLONG_MIN;

A similar change is needed for line 107, ie, this line

    errval = maxval = LONG_MAX;

should be revised to, for example, these lines

    if ( sign == '-' ) {
        maxval = -(LONG_MIN+1) + 1ULL;
        errval = LONG_MIN;
    } else  errval = maxval = LONG_MAX;

On page 11, the final 'else' clause (lines 164-169):

    else if ( sign == '-' ) {
        if ( Flags & (SIGNED32 | UNSIGNED32) )
            value = - (long long) (value & 0xffffffff);
        else
            value = - (long long) value;
    }

has unnecessary casts, and also a dependency on an assumption
that 'long' is only 32 bits.  In fact all these complications are
not necessary.  These lines may be correctly written more simply
as

    else if ( sign == '-' ) {
        value = -value;
    }

which should produce correct results on your implementation (it
did in my tests).  Strictly speaking this result depends on an
implementation-defined behavior (in the two calling functions
strtol() and strtoll()), converting an unsigned long long to a
long or long long, respectively.  It is reasonable to expect this
conversion will do what we want, but it seems a good idea to put
in at least a comment to that effect.  If the above changes are
made the only places where this implementation-defined behavior
is relevant is in strtol() and strtoll().

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-03 14:28 +0200
  Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 14:46 +0100
  Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-03 11:08 -0700
  Re: strtoll discussion James Kuyper <jameskuyper@verizon.net> - 2015-04-03 15:23 -0400
    Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 20:55 +0100
      Re: strtoll discussion glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-03 20:41 +0000
        Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-03 13:52 -0700
          Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 22:25 +0100
            Re: strtoll discussion Bartc <bc@freeuk.com> - 2015-04-04 00:17 +0100
              Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-03 16:27 -0700
            Re: strtoll discussion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-04 02:07 +0100
              Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-04 03:44 -0700
                Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 12:13 +0100
    Re: strtoll discussion James Kuyper <jameskuyper@verizon.net> - 2015-04-03 16:11 -0400
    Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-03 14:12 -0700
    Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-03 23:56 +0200
  Re: strtoll discussion Ian Collins <ian-news@hotmail.com> - 2015-04-04 08:41 +1300
    Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-03 23:33 +0200
  Re: strtoll discussion James Lothian <james@firstnamelastname.freeserve.co.uk> - 2015-04-03 20:51 +0100
    Re: strtoll discussion asetofsymbols@gmail.com - 2015-04-03 13:09 -0700
      Re: strtoll discussion asetofsymbols@gmail.com - 2015-04-03 13:16 -0700
      Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 21:19 +0100
        Re: strtoll discussion asetofsymbols@gmail.com - 2015-04-03 13:28 -0700
          Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 22:29 +0100
          Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-03 23:33 +0200
      Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-03 23:34 +0200
        Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 22:42 +0100
          Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-03 15:08 -0700
          Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-04 00:13 +0200
            Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-03 23:44 +0100
              Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-04 00:48 +0200
                Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 00:03 +0100
                Re: strtoll discussion Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-03 19:31 -0400
                Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 00:37 +0100
                Re: strtoll discussion Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-04-04 10:29 -0400
                Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 15:54 +0100
                Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-04 08:07 -0700
                Re: strtoll discussion Bartc <bc@freeuk.com> - 2015-04-04 16:28 +0100
                Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-04 08:57 -0700
                Re: strtoll discussion Bartc <bc@freeuk.com> - 2015-04-04 17:03 +0100
                Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-04 09:44 -0700
                Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 18:05 +0100
                Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-04 23:32 -0700
                Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-05 11:00 +0100
                Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-05 13:18 -0700
                Re: strtoll discussion Richard Damon <Richard@Damon-Family.org> - 2015-04-05 17:37 -0400
                Re: strtoll discussion Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-05 15:00 -0700
                Re: strtoll discussion Robert Wessel <robertwessel2@yahoo.com> - 2015-04-05 22:17 -0500
                Re: strtoll discussion Richard Damon <Richard@Damon-Family.org> - 2015-04-04 18:54 -0400
                Re: strtoll discussion Bartc <bc@freeuk.com> - 2015-04-05 12:14 +0100
                Re: strtoll discussion glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-04 00:19 +0000
                Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-03 17:48 -0700
                Re: strtoll discussion James Kuyper <jameskuyper@verizon.net> - 2015-04-08 11:23 -0400
                Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-08 19:35 +0200
                Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-08 10:47 -0700
                Re: strtoll discussion James Kuyper <jameskuyper@verizon.net> - 2015-04-08 14:22 -0400
  lltostr/ulltostr jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-04 13:56 +0200
    Re: lltostr/ulltostr Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 13:36 +0100
    Re: lltostr/ulltostr Barry Schwarz <schwarzb@dqel.com> - 2015-04-04 18:54 -0700
      Re: lltostr/ulltostr Ian Collins <ian-news@hotmail.com> - 2015-04-05 14:29 +1200
    Re: lltostr/ulltostr Ralf Damaschke <rwspam@gmx.de> - 2015-04-05 16:58 +0000
      Re: lltostr/ulltostr jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-05 20:39 +0200
        Re: lltostr/ulltostr glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-04-05 20:19 +0000
      Re: lltostr/ulltostr Keith Thompson <kst-u@mib.org> - 2015-04-05 13:18 -0700
        Re: lltostr/ulltostr Ralf Damaschke <rwspam@gmx.de> - 2015-04-06 14:28 +0000
          Re: lltostr/ulltostr Keith Thompson <kst-u@mib.org> - 2015-04-06 08:40 -0700
  Re: strtoll discussion Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-04 13:04 -0700
    Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-04 22:52 +0200
      Re: strtoll discussion Richard Heathfield <rjh@cpax.org.uk> - 2015-04-04 22:14 +0100
      Re: strtoll discussion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-04-05 00:24 +0100
      Re: strtoll discussion Geoff <geoff@invalid.invalid> - 2015-04-04 16:51 -0700
      Re: strtoll discussion jacobnavia <jacob@jacob.remcomp.fr> - 2015-04-05 23:32 +0200
        Re: strtoll discussion Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-16 09:21 -0700
      Re: strtoll discussion Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-17 02:21 -0700
    Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-04 15:36 -0700
      Re: strtoll discussion Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-16 09:19 -0700
        Re: strtoll discussion Keith Thompson <kst-u@mib.org> - 2015-04-16 10:43 -0700
          Re: strtoll discussion Tim Rentsch <txr@alumni.caltech.edu> - 2015-04-17 07:59 -0700
  Re: strtoll discussion Philipp Klaus Krause <pkk@spth.de> - 2015-04-07 11:46 +0200

csiph-web