Live data from Hacker News

No way to parse integers in C (2022)

blog.habets.se

61–70 of 117 posts

Re: No way to parse integers in C (2022)

#61

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.

Re: No way to parse integers in C (2022)

#62

Earlier quoted context omitted.

Could you humor a coding noob--how do you deal with utterly insane inputs like that?

You first ask if you really need to.

Unless you're exposing it to the internet, ever, in the entire future history of the program. Then you kind of have to, in one form or another.

Re: No way to parse integers in C (2022)

#63
post #27

One of the first homework assignments when I learned C back in '83 was after a long lecture on how the string functions are fundamentally broken, and the class introduction to writing C was fixing all of them.

My memory growing up is that making your own C library was basically an inevitable rite of passage for any aspiring programmer.

Yeah, it's a shame we never got something like boost for C. Every company I ever worked for had its own common C library solving these problems.

Re: No way to parse integers in C (2022)

#64
post #61

Earlier quoted context omitted.

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.

Re: No way to parse integers in C (2022)

#65
post #41

As a C programmer, I find this kind of bad faith article very irritating. Yes, the standard library is bad. This is by far the worst part of the C legacy. But it is not that hard to write your own. String functions like this are not difficult at all, and you can use better naming and semantics, write faster code etc. C is not the C standard library, ffs.

The people downvoting you are probably not C programmers and love to hate C.

I guess trying to write in Rust makes them irritable.

Re: No way to parse integers in C (2022)

#66

I wasn't in this class myself, but one prof at my alma mater started his "Programming 201" class with the simplest assignment: write a C program that accepts two integers from the user and prints their sum. It actually was the only assignment for the rest of the semester, since he has a test suite that would humiliate the students gently at first, but would ultimately pipe a billion nines into stdin as the first argu…

Could you humor a coding noob--how do you deal with utterly insane inputs like that?

Arbitrary precision arithmetic (GMP, BigInteger, etc). Numbers can take arbitrary amounts of memory, instead of just a single machine word.

Re: No way to parse integers in C (2022)

#69
post #43
post #35

Earlier quoted context omitted.

Perfect is the enemy of good.

Once a program is available over the internet, hackers are the enemy of merely good programs that don't perfectly validate their input. "You have to get lucky every time. We only have to get lucky once".

Sure

Re: No way to parse integers in C (2022)

#70
post #63
post #27

Earlier quoted context omitted.

My memory growing up is that making your own C library was basically an inevitable rite of passage for any aspiring programmer.

Yeah, it's a shame we never got something like boost for C. Every company I ever worked for had its own common C library solving these problems.

It's a shame we never got a package manager for C (or C++).

EDIT: perhaps I should have been clearer; by not having one early on, we now have multiple competing package managers, with no clear winner. Responses prove that point.

Post reply on HN