Earlier quoted context omitted.
There was a nice article [0] about bloated edge cases libraries (discussion [1]). Sometimes, it's just not the responsibility of the library. Trying to handle every possible errors is a quick way to complexity. [0]: https://43081j.com/2025/09/bloat-of-edge-case-libraries [1]: https://news.ycombinator.com/item?id=45319399
This might be the right attitude for a max function written in JavaScript, where the calling code has some control over the inputs. It's the wrong attitude for a JSON parser written in C, unless you like to get owned.
Sj.h: A tiny little JSON parsing library in ~150 lines of C99
241–248 of 248 posts
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#242Earlier quoted context omitted.
Very few programming languages default to checked increments. Most Rust or Java programmers would make the same mistake. Writing a function to do a checked addition like in other languages isn't exactly difficult, either.
> Most Rust or Java programmers would make the same mistake. Detecting these mistakes in Rust is not too difficult. In debug builds, integer overflow triggers a panic[1]. Additionally, clippy (the official linter of Rust), has a rule[2] to detect this mistake. [1] https://doc.rust-lang.org/book/ch03-02-data-types.html#integ... [2] https://rust-lang.github.io/rust-clippy/master/index.html#ar...
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#243Earlier quoted context omitted.
Very few programming languages default to checked increments. Most Rust or Java programmers would make the same mistake. Writing a function to do a checked addition like in other languages isn't exactly difficult, either.
Many other languages automatically switch to a big integer number type, or have arbitrary size integers anyway.
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#244Earlier quoted context omitted.
An int will be 32 bits on any non-ancient platform, so this means, for each of those lines: - a JSON file with nested values exceeding 2 billion depth - a file with more than 2 billion lines - a line with more than 2 billion characters
The depth is 32 bit, not the index into the file. If you are nesting 2 Billion times in a row ( at minimum this means repeat { 2 billion times followed by a value before } another 2 billion times. You have messed up. You have 4GB of "padding"...at minimum. You file is going to be Petabytes in size for this to make any sense. You are using a terrible format for whatever you are doing. You are going to need a completel…
An after the fact check would be the wrong way to deal with UB, you'd need to check for < INT_MAX before the increment in order to avoid it.
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#245Earlier quoted context omitted.
An int will be 32 bits on any non-ancient platform, so this means, for each of those lines: - a JSON file with nested values exceeding 2 billion depth - a file with more than 2 billion lines - a line with more than 2 billion characters
What is your definition of non-ancient? There are still embedded systems being produced today that don't have 32-bit integers.
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#246Earlier quoted context omitted.
2GB in a single JSON file is definitely an outlier. A simple caveat when using this header could suffice: ensure inputs are less than 2GB.
I've seen much bigger, though technically that wasn't valid json, but rather structured logging with JSON on each line. On the other hand, I've seen exported JSON files that could grow to such sizes without doing anything weird, just nothing exceeding a couple hundred megabytes because I didn't use the software for long enough. Restricting the input to a reasonable size is an easy workaround for sure, but this limita…
And in the spirit of your profile text I'm quite glad for such landmines being out there to trip up those that do blindly ingest all code they can find.
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#247Earlier quoted context omitted.
What is your definition of non-ancient? There are still embedded systems being produced today that don't have 32-bit integers.
And those will need careful review of any code you want to run on them because no one cares about your weird architecture nor should they have to.
Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99
#248Earlier quoted context omitted.
It's astonishing how involved a fucking modern JSON library becomes. The once "very simple" C++ single-header JSON library by nlohmann is now * 13 years old * is still actively merging PRs (last one 5 hours ago) * has 122 __million__ unit tests Despite all this, it's self-admittedly still not the fastest possible way to parse JSON in C++. For that you might want to look into simdjson. Don't start your own JSON parser…
Yeah I use this and I think most of friends do too :)