"Paul Herring" <[EMAIL PROTECTED]> wrote:
> Thomas Hruska <[EMAIL PROTECTED]> wrote:
> > ...strtol() depends on the source to be zero terminated.
> 
> Erm - no it doesn't

Yes, it does.

> - any unrecognised character will terminate the
> conversion, not just a NUL.

Nevertheless, strtol() _requires_ a pointer to a string as the
first argument.

7.20.1.4p2: "The strtol [et al] functions convert the initial
portion of the string pointed to by nptr..."

This is in contrast to other functions that don't require
a null terminated sequence although they mostly operate
on strings...

  #include <stdio.h>
  #include <stdlib.h>
  
  int main(void)
  {
    const char literal[6] = "123xyz";  /* NOT A STRING! */
    long l;
    printf("%.6s\n", literal);         /* valid */
    l = strtol(literal, 0, 10);        /* not valid!! */
    printf("%ld\n", l);
    return 0;
  }

The %s format specifier of printf does not require a string
if the precision is not larger than the sequence of characters
supplied.

A more notable example is strncpy() which was specifically
designed to work with fixed width fields where the last byte
needn't be a null byte.

But the strtol() function does not have that dispensation.
Although the above code will likely work on most platforms, it
is not strictly conforming as the call to strtol() does not
supply a valid _string_ as the first argument; the behaviour is
therefore undefined. If we change [6] to [7] (or just []) in
the declaration of 'literal', then the behaviour becomes well
defined.

-- 
Peter

Reply via email to