News for hackers. Stuff that matters.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by ricardobeat on Sunday September 21, 2025 @02:16PM (#45325236)
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

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by fizzynut on Sunday September 21, 2025 @03:29PM (#45325871)
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 completely custom parser because nothing will fit in memory. I don't care how much RAM you have.

Simply accessing an element means traversing a nested object 2 billion times in probably any parser in the world is going to take somewhere between minutes and weeks per access.

All that is going to happen in this program is a crash.

I appreciate that people want to have some pointless if(depth > 0) check everywhere, but if your depth is anywhere north of million in any real world program, something messed up a long long time ago, never mind waiting until it hits 2 billion.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by account42 on Tuesday September 23, 2025 @10:23AM (#45347472)
> I appreciate that people want to have some pointless if(depth > 0) check everywhere

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... by klysm on Sunday September 21, 2025 @02:34PM (#45325382)
2 billion characters seems fairly plausible to hit in the real world
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by naasking on Sunday September 21, 2025 @03:14PM (#45325746)
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.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by jeroenhd on Sunday September 21, 2025 @04:07PM (#45326188)
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 limitation isn't indicated everywhere, so anyone deciding to consume this random project into their important code wouldn't know to defend against such situation.

In a web server scenario, 2GiB of { (which would trigger two overflows) in a compressed request would require a couple hundred kilobytes to two megabytes, depending on how old your server software is.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by account42 on Tuesday September 23, 2025 @10:28AM (#45347551)
To be fair, anyone who uses a 150 line library without bothering to read it deserves what they get.

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... by layer8 on Sunday September 21, 2025 @03:28PM (#45325861)
Less than INT_MAX, more accurately. But since the library contains a check when decreasing the counter, it might as well have a check when increasing the counter (and line/column numbers).
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by EasyMark on Sunday September 21, 2025 @03:25PM (#45325833)
Or fork and make a few modifications to handle it? I have to admit I haven't looked at the code to see if this particular code would allow for that.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by maleldil on Sunday September 21, 2025 @04:24PM (#45326317)
Not really. I deal with this everyday. If the library has a limit on the input size, it should mention this.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by naasking on Sunday September 21, 2025 @08:12PM (#45327838)
If you deal with this every day, you're an outlier.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by johnisgood on Monday September 22, 2025 @04:43AM (#45330665)
It is ~150 lines of code. Submit a PR, or when you git clone it add your checks, or stop complaining because the author does not owe you anything.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by ricardobeat on Sunday September 21, 2025 @04:22PM (#45326305)
In a single line. Still not impossible, but people handling that amount of data will likely not have “header only and <150 lines” as a strong criteria for choosing their JSON parsing library.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by xigoi on Monday September 22, 2025 @12:22AM (#45329077)
For such big data, you should definitely be using an efficient format, not JSON.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by klysm on Monday September 22, 2025 @11:48AM (#45335124)
I agree, but 2GB json files absolutely exist. It fits in ram easily
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by ranger_danger on Sunday September 21, 2025 @03:29PM (#45325868)
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... by account42 on Tuesday September 23, 2025 @10:24AM (#45347496)
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... by ranger_danger on Tuesday September 23, 2025 @10:46AM (#45347818)
I wouldn't call 8 or 16-bit microcontrollers (with no concept of a 32-bit int) that are in billions of devices "weird". But ok.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @02:18PM (#45325247)
All very possible on modern platforms.

Maybe more importantly, I won’t trust the rest of the code if the author doesn’t seem to have the finite range of integer types in mind.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by johnisgood on Sunday September 21, 2025 @02:39PM (#45325427)
Personally, all my C code is written with SEI C Coding Standard in mind.