Skip to content
Tech News
← Back to articles

No way to parse integers in C (2022)

read original get C Programming Integer Parsing Book → more articles
Why This Matters

This article highlights the critical shortcomings of standard C functions for parsing integers, emphasizing their inability to reliably validate input and handle errors. This poses significant risks for developers relying on these functions for robust and secure applications, especially when processing untrusted data. The discussion underscores the need for better, more predictable parsing methods in the C and C++ standards to improve software safety and correctness.

Key Takeaways

This is my personal blog. The views expressed on these pages are mine alone and not those of my employer. This is not AI. All hallucinations are my own human ones.

There are a few ways to attempt to parse a string into a number in the C standard library. They are ALL broken.

Update at the bottom: Actually C++’s std::from_chars() looks useful.

Leaving aside the wide character versions, and staying with long (skipping int , long long or intmax_t , these variants all having the same problem) there are three ways I can think of:

atol() strtol() / strtoul() sscanf()

They are all broken.

What is the correct behavior, anyway?

I’ll start by claiming a common sense “I know it when I see it”. The number that I see in the string with my eyeballs must be the numerical value stored in the appropriate data type. “123” must be turned into the number 123 .

Another criteria is that the WHOLE number must be parsed. It is not OK to stop at the first sign of trouble, and return whatever maybe is right. “123timmy” is not a number, nor is the empty string.

Failing to provide the above must be an error. Or at least as the user of the parser I must have the option to know if it happened.

... continue reading