Libc is Broken, Part 1

It is time for a gripe.

Libc contains a lot of quite APIs. You probably know about the
gets dissaster, but there is a whole lot more.

  • String-to-value conversions. Here is how you
    convert from a string s containing a decimal integer
    into an int i:

      char *end;
    int i;
    long l;
    errno = 0;
    l = strtol (s, &end, 10);
    if (s != end && *end == 0 &&
    errno != ERANGE &&
    l >= INT_MIN && l
    And that is before considering locales (which typically do not
    affect integers, mind you).
  • Time zones. The localtime function
    is great, but having $TZ as as implicit and not explicit argument
    makes it really, really hard to use for anything but the default
    time zone.
  • ctype functions. At least two things are wrong:
    (1) the encoding used is an implicit argument; and (2) they are defined obscurely such that char c = 'a'; isdigit(c) may or may not
    be valid, depending on the platform. Most commonly it is not valid,
    i.e., it may core dump. The right way to do that is,
    for reference, isdigit((unsigned char)c) which is enough to make a sane man crazy. (Alternatively one
    could define c as unsigned char but in
    more practical code the type is often constrained by a parameter type.)

I have the feeling I might return to this subject later.

Comments are closed.