← Back to context

Comment by contubernio

2 hours ago

One of the great virtues of C is that this sort of thing is not part of the language ...

Only literally. 7.24.1 in the C programming language spec has these poor parsers.

  • Is their misbehavior part of the spec as well? If not, we can always add the correct behavior to the spec and let anyone who implemented a broken version deal with fixing every program compiled using it.

    • Fair enough.

      For strtoul and friends, maybe? 7.24.1 is pretty dense, but the key parts are "the expected form of the subject sequence is a sequence of letters and digits representing an integer with the radix specified by base, optionally preceded by a plus or minus sign […] If the correct value is outside the range of representable values […] ULONG_MAX […] is returned".

      So the "expected form" allows a minus sign, but then it's clearly "outside the range of representable values" for strtoul to try parsing a negative value. So maybe it should return ULONG_MAX on those.

      So arguably a minus sign present could already be treated as an error, and still be standard compliant. Unless I'm misreading.