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

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by klysm on Sunday September 21, 2025 @02:33PM (#45325374)
Strongly disagree here because JSON can come from untrusted sources and this has security implications. It's not the same kind of problem that the bloat article discusses where you just have bad contracts on interfaces.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Brian_K_White on Sunday September 21, 2025 @04:09PM (#45326204)
Public facing interfaces are their own special thing, regardless if json or anything else, and not all data is a public facing interface.

If you need it, then you need it. But if you don't need it, then you don't need it. There is a non-trivial value in the smallness and simplicity, and a non-trivial cost in trying to handle infinity problems when you don't have infinity use-case.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by klysm on Sunday September 21, 2025 @05:16PM (#45326726)
This is a serialization library. The entire point is to communicate with data that's coming from out of process. It should be safe by default especially if it's adding a quick check to avoid overflow and undefined behavior.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Brian_K_White on Sunday September 21, 2025 @06:28PM (#45327246)
Incorrect assumption.

If you are reading data from a file or stream that only you yourself wrote some other time, then it's true that data could possibly have been corrupted or something, but it's not true that it's automatically worth worrying about enough to justify making the code and thus it's bug surface larger.

How likely is the problem, how bad are the consequences if the problem happens, how many edge cases could possibly exist, how much code does it take to handle them all? None of these are questions you or anyone else can say about anyone else's project ahead of time.

If the full featured parser is too big, then the line drawing the scope of the lightweight parser has to go somewhere, and so of course there will be things on the other side of that line no matter where it is except all the way back at full-featured-parser.

"just this one little check" is not automatially reasonable, because that check isn't automatically more impoprtant than any other, and they are all "just one little checks"s. The one little check would perevent what? Maybe a problem that never happens or doesn't hurt when it does happen. A value might be misinerpreted? So what? Let it. Maybe it makes more sense to handle that in the application code the one place it might matter. If it will matter so much, then maybe the application needs the full fat library.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by leptons on Sunday September 21, 2025 @03:57PM (#45326108)
JSON does not necessarily come from untrusted sources if you control the entire system. Not everything needs to be absolutely 100% secure so long as you control the system. If you are opening the system to the public, then sure, you should strive for security, but that isn't always necessary in projects that are not processing public input.

Here's an example - I once coded a limited JSON parser in assembly language. I did not attempt to make it secure in any way. The purpose was to parse control messages sent over a serial port connection to an embedded CPU that controlled a small motor to rotate a camera and snap a photo. There was simply no way for any "untrusted" JSON to enter the system. It worked perfectly and nothing could ever be compromised by having a very simple JSON parser in the embedded device controlling the motor.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by boramalper on Sunday September 21, 2025 @04:09PM (#45326200)
Untrusted doesn’t always mean adversarial IMO, even a bitrot can invalidate your entire input and possibly also trigger undefined behaviour if you aren’t prepared to handle that.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by cwmoore on Sunday September 21, 2025 @04:19PM (#45326290)
UB = "undefined behavior", thanks
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by leptons on Sunday September 21, 2025 @05:15PM (#45326715)
I was using a checksum to protect against "bitrot" since this was over a very noisy serial transmission line (over a slip ring). So, no, there was no "undefined behavior" and it's quite easy to avoid.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by interstice on Sunday September 21, 2025 @04:09PM (#45326207)
Massively agree. Remember this thinking being everywhere with databases back in the day, not every text field is hooked up to a Wordpress comment section.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by userbinator on Sunday September 21, 2025 @04:09PM (#45326199)
You probably didn't control the other end, as otherwise you would've used something more sane than JSON?
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by leptons on Sunday September 21, 2025 @05:18PM (#45326742)
I controlled both ends. There is nothing "insane" about JSON. It's used far and wide for many purposes. The system sending the JSON was based on Nodejs, so it was pretty natural to use JSON. And I did it with JSON just because I wanted to. I'd have had to invent some other protocol to do it anyway, and I didn't feel like reinventing the wheel when it was quite simple to write a basic JSON parser in assembly language, which is what I am comfortable with on the embedded system (been coding assembly for 40 years).
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by userbinator on Sunday September 21, 2025 @06:10PM (#45327137)
For something that simple I'd choose a custom binary protocol or something like ASN.1 instead of JSON. It's easier to generate from a HLL and parse in a LLL (I've also been writing Asm for a few decades...)
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by leptons on Monday September 22, 2025 @01:06AM (#45329265)
I've done plenty of custom binary protocols before. I can't say they were any better or easier to deal with. I also can't say that the "parser" for a binary format was any easier than a simple, limited JSON parser.

For this specific project I chose JSON and it worked perfectly. Sending JSON from the embedded CPU was also really simple. Yes, there was a little overhead on a slow connection, but I wasn't getting anywhere near saturation. I think it was 9600 bps max on a noisy connection with checksums. If even 10% of the JSON "packets" got through it was still plenty for the system to run.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by johnisgood on Monday September 22, 2025 @04:40AM (#45330643)
I would love to use ASN.1 if other programming languages would match up to Erlang's ASN.1. :(
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Monday September 22, 2025 @11:25AM (#45334757)
Even if is not popular here .NET does support ASN.1, not sure if at the same level as Erlang.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by cozzyd on Monday September 22, 2025 @11:54AM (#45335227)
It depends on the use case. JSON has a lot of tooling making it convenient in a lot of cases
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by ghurtado on Sunday September 21, 2025 @05:27PM (#45326816)
> Not everything needs to be absolutely 100% secure so long as you control the system.

Isn't that a bit like saying "you don't have to worry about home security as long as you are the only person who has the ability to enter your house"?

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Sunday September 21, 2025 @09:47PM (#45328339)
Not at all.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Mogzol on Sunday September 21, 2025 @11:13PM (#45328757)
I mean yeah if you're truly the only person that has the ability to enter your house then why should you worry about home security? Nobody else has the ability to get in.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by ncruces on Monday September 22, 2025 @02:43AM (#45329846)
Sure. I don't password protect my (Android) TV like I password protect my (Android) phone, despite both of them allowing authorized access to the same Google accounts, because if someone entered my house I have bigger things to worry than them using my TV.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by johnisgood on Monday September 22, 2025 @04:38AM (#45330629)
I agree. I knew that the JSON is not going to change, so I wrote a 10 lines long parser for it. It is not a JSON parser by any means, but it parses properly what I need it to.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by cozzyd on Monday September 22, 2025 @10:08AM (#45333699)
sscanf as a parser definitely has its uses
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by johnisgood on Monday September 22, 2025 @10:15AM (#45333774)
I used strchr() in a function I named get_pair(). So, all in all, I used strchr() and strcmp() only!
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Sunday September 21, 2025 @09:44PM (#45328328)
You would use this for parsing data you know is safe.

Using a "tiny library" for parsing untrusted data is where the mistake is. Not in OP code.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by president_zippy on Sunday September 21, 2025 @10:27PM (#45328548)
It's too bad this header-only JSON library doesn't meet your requirements. How much did you pay for your license to use it? I'm sure the author will be happy to either ship security fixes or give you a refund. You should reach out to him and request support.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by account42 on Tuesday September 23, 2025 @10:04AM (#45347222)
There is only a problem with these checks here if you pass along arbitrarily large JSON strings as all of these counters are advanced at most once per input byte. If you don't limit the input to reasonable sizes you have a potentional denial of service problem even without the UB so you should be checking for reasonable sizes which depend on your application but are likely much lower than the 2^31-1 bytes the library can safely parse.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @02:17PM (#45325242)
The problem in the present case is that the caller is not made aware of the limitation, so can’t be expected to prevent passing unsupported input, and has no way to handle the overflow case after the fact.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by skydhash on Sunday September 21, 2025 @02:25PM (#45325296)
Do you not review libraries you add to your project? A quick scan of the issues page if it's on a forge? Or just reading through the code if it's small enough (or select functions)?

Code is the ultimate specification. I don't trust the docs if the behavior is different from what it's saying (or more often fails to mention). And anything that deals with recursive structures (or looping without a clear counter and checks) is my one of the first candidate for checks.

> has no way to handle the overflow case after the fact.

Fork/Vendor the code and add your assertions.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @02:27PM (#45325314)
Obviously I just did review it, and my conclusion was to not use that code.

In the spirit of the article you linked, I’d rather write my own version.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by jama211 on Sunday September 21, 2025 @03:28PM (#45325863)
If it has limitations they should be documented though right? especially if they’re security concerns.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by knowitnone2 on Sunday September 21, 2025 @09:57PM (#45328390)
If you review libraries, why do you need to quick scan the issues? You would have already identified all the issues right? Right?
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by jama211 on Monday September 22, 2025 @02:32PM (#45337573)
Hahaha well said
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by FooBarBizBazz on Sunday September 21, 2025 @03:27PM (#45325857)
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.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by account42 on Tuesday September 23, 2025 @10:15AM (#45347341)
It's entirely reasonable for a tiny JSON parser written in C to not deal with JSON files over 0x7fffffff bytes long.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by flykespice on Sunday September 21, 2025 @02:44PM (#45325473)
There is no easy way out when you're working with C: either you handle all possible UB cases with exhaustive checks, or you move on to another language.

(TIP: choose the latter)

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by jeroenhd on Sunday September 21, 2025 @03:56PM (#45326091)
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.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by paulddraper on Sunday September 21, 2025 @04:37PM (#45326402)
Yes but those languages have defined overflow.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by cozzyd on Sunday September 21, 2025 @05:28PM (#45326829)
-fwrapv
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by koito17 on Sunday September 21, 2025 @06:20PM (#45327197)
> 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... by account42 on Tuesday September 23, 2025 @10:17AM (#45347368)
You can also just use ubsan if you want the runtime checks for overflows in C/C++, no need to switch languages for this.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by zelphirkalt on Monday September 22, 2025 @05:43AM (#45331078)
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... by account42 on Tuesday September 23, 2025 @10:18AM (#45347385)
Which is a great way to turn an overflow into a denial of service as suddenly algorithms that were optimized for simple integer arithmetic slow to a crawl.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by uecker on Sunday September 21, 2025 @05:42PM (#45326939)
For signed overflow you can just turn on the sanitizer in trapping mode. Exhaustive checks is also not that terrible.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by paulddraper on Sunday September 21, 2025 @04:27PM (#45326335)
The amount of untrusted JSON I parse is very high.

UB is bad.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by RossBencina on Sunday September 21, 2025 @09:59PM (#45328399)
> Sometimes, it's just not the responsibility of the library.

Sometimes. In this case, where the library is a parser that is written in C. I think it is reasonable to expect the library to handle all possible inputs. Even corner cases like this which are unlikely to be encountered in common practice. This is not "bloat" it is correctness.

In C, this kind of bug is capable of being exploited. Sure, many users of this lib won't be using it in exposed cases, but sooner or later the lib will end up in some widely-used internet-facing codebase.

As others have said, the fix could be as simple as bailing once the input size exceeds 1GB. Or it could be fine-grained. Either-way the fix would not "bloat" the codebase.

And yes, I'm well aware of the single-file C library movement. I am a fan.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by hypeatei on Sunday September 21, 2025 @02:38PM (#45325417)
You're not aware of the simplistic, single header C library culture that some developers like to partake in. Tsoding (a streamer) is a prime example of someone who likes developing/using these types of libraries. They acknowledge that these things aren't focused on "security" or "features" and that's okay. Not everything is a super serious business project exposed to thousands of paying customers.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @02:44PM (#45325476)
Hobby projects that prove useful have a tendency of starting to be used in production code, and then turning into CVEs down the road.

If there is a conscious intent of disregarding safety as you say, the Readme should have a prominent warning about that.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by hypeatei on Sunday September 21, 2025 @02:47PM (#45325498)
> Hobby projects that prove useful have a tendency of starting to be used in production code

Even if that is true, how is that the authors problem? The license clearly states that they're not responsible for damages. If you were developing such a serious project then you need the appropriate vetting process and/or support contracts for your dependencies.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @02:50PM (#45325539)
I didn’t say it’s the author’s problem. It’s a problem with the code.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by ethanwillis on Sunday September 21, 2025 @05:01PM (#45326600)
Why play all these semantic games? You're saying it's the author's problem. You want them to even edit their readme to include warnings for would be production/business users who don't want to pay for it.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @07:30PM (#45327618)
GP is arguing about licences. Yes, formally there is no obligation, and I'm not saying the author has any such obligation.

In the present case, either the UB in the code is by mistake or lack of awareness, and then it's warranted to point out the error, or, as GGGP argued, the author deliberately decided to neglect safety or correctness, and then in my opinion you can't reject the criticism as unwarranted if the project's presentation isn't explicit about that.

I'm not making anything the author's problem here. Rather, I'm defending my criticism of the code, and am giving arguments as to why it would be good form to make it explicit if a project doesn't care about the code being safe and correct.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by stevepotter on Monday September 22, 2025 @07:07AM (#45331781)
I understand your point and if I were the author I would want either a disclaimer or a fix. File an issue or make a pr. Filing an issue is quicker and more fruitful than dealing with folks here
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by president_zippy on Sunday September 21, 2025 @10:13PM (#45328483)
There are two kinds of people in this world: people who do things, and people who bitch about other people doing things. layer8 is the latter because being the former takes too much effort.

Misery loves company, and losers love dragging other people down to their level so they feel a little less lonely.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by lixtra on Monday September 22, 2025 @12:57AM (#45329214)
Layer8 DID the thing though, skimmed through the code and thought about security issues.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by madeofpalk on Monday September 22, 2025 @09:59AM (#45333594)
It is useful to understand the limitations of such hobby programs to know what they are useful for.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by koolba on Sunday September 21, 2025 @03:30PM (#45325888)
> If there is a conscious intent of disregarding safety as you say, the Readme should have a prominent warning about that.

What do you consider this clause in the LICENSE:

>> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by CorrectHorseBat on Sunday September 21, 2025 @03:37PM (#45325964)
A standard clause you can find in every open source license? It doesn't say anything about how serious the project takes security
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Sunday September 21, 2025 @09:52PM (#45328373)
You write only Rust code don't you?
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by CorrectHorseBat on Monday September 22, 2025 @01:19AM (#45329334)
I wish ;) You're talking about how Rust code usually uses the MIT license and this is a part of the MIT license?

Every open source license has a very similar clause, include but not limited to BSD, GPL, CDDL, MPL and Apache.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by f1shy on Sunday September 21, 2025 @03:26PM (#45325837)
My personal take is: if the code is good enough, it should be trivial to switch to a better library at the point when needed.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by vrighter on Sunday September 21, 2025 @02:48PM (#45325513)
then that is their problem, not the code author's. If you use a hobby project in production, that's on you
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by w4rh4wk5 on Monday September 22, 2025 @04:47AM (#45330697)
When such a library is used in production code, that's on the person who chose to use it in production, not on the original author of the library.

You are responsible for the code you ship, doesn't matter whether it's written by you, an LLM, or whether it's a third-party dependency.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by zelphirkalt on Monday September 22, 2025 @05:33AM (#45331010)
While that is certainly true, we could also be nice and reduce the workload of someone reviewing their dependencies and write it down in the readme.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by zwnow on Sunday September 21, 2025 @02:41PM (#45325447)
So if its a hobby project designed for just a handful of people, its suddenly okay to endanger them due to being sloppy?
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by hypeatei on Sunday September 21, 2025 @02:44PM (#45325467)
This is an open source project that you're not obligated to use nor did you pay for it. Who is it endangering?

The license also makes it clear that the authors aren't liable for any damages.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by flykespice on Sunday September 21, 2025 @02:52PM (#45325556)
...and what open source software license in the world makes the author liable for damages?
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by k_roy on Sunday September 21, 2025 @03:30PM (#45325886)
Probably more of lack of explicit liability in the license.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by virtue3 on Sunday September 21, 2025 @04:45PM (#45326471)
every OSS license I've ever seen is "use at your own risk" essentially. That's how this whole system works.

You find a vulnerability? patch it, push change to repo maintainer.

https://xkcd.com/2347

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by flykespice on Monday September 22, 2025 @10:37AM (#45334091)
Pretty sure the all caps text on the bottom of most open source licenses out there makes it clear
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Sunday September 21, 2025 @09:58PM (#45328394)
None. That is how RedHat makes money.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by account42 on Tuesday September 23, 2025 @09:59AM (#45347166)
> The license also makes it clear that the authors aren't liable for any damages.

The license disclaims liability but that doesn't mean the author cannot ever be held liable. Ultimately, who is liable is up to a court to decide.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by nkrisc on Sunday September 21, 2025 @03:13PM (#45325735)
The code nor author don’t endanger anyone. Whoever uses it inappropriately endangers themselves or others.

Why are you using random, unvetted and unaudited code where safety is important?

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Sunday September 21, 2025 @09:55PM (#45328383)
Open Source is about sharing knowledge.

They are sharing their knowledge about how to create a tiny JSON parser. Where is the problem again?

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by zwnow on Monday September 22, 2025 @02:12AM (#45329648)
Refer to the original comment. Seems like you are incapable of connecting the comment chain.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Yeask on Monday September 22, 2025 @07:38AM (#45332026)
Have some manners please.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by tossaway0 on Sunday September 21, 2025 @11:19PM (#45328790)
Yes, pretty much. It has enough of a warning.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by taminka on Sunday September 21, 2025 @07:46PM (#45327699)
> They acknowledge that these things aren't focused on "security" or "features" and that's okay.

where? single header is just a way to package software, it has no relation to features, security or anything such...

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by TZubiri on Monday September 22, 2025 @12:39AM (#45329134)
Either you are :

- overestimating the gravity of a UB and its security implications

- underestimate the value of a 150 line json parser

- or overestimate the feasibility of having both a short and high quality parser.

It sometimes happens that fixing a bug is quicker than defending the low quality. Not everything is a tradeoff.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by nurettin on Monday September 22, 2025 @06:03AM (#45331232)
I have tsoding fatigue. Took a long time to get him out of the main page. I like the DIY attitude, but it gets old really fast.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by bmn__ on Monday September 22, 2025 @07:26AM (#45331923)
https://news.ycombinator.com/item?id=44345740

No one cares. Stop complaining or GTFO.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by nurettin on Monday September 22, 2025 @07:47AM (#45332079)
Au contraire, I think people do care. Now I will continue complaining and raising awareness with renewed fervor.
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.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by oguz-ismail on Sunday September 21, 2025 @04:14PM (#45326243)

    diff --git a/sj.h b/sj.h
    index 60bea9e..25f6438 100644
    --- a/sj.h
    +++ b/sj.h
    @@ -85,6 +85,7 @@ top:
             return res;
     
         case '{': case '[':
    +        if (r->depth > 999) { r->error = "can't go deeper"; goto top; }
             res.type = (*r->cur == '{') ? SJ_OBJECT : SJ_ARRAY;
             res.depth = ++r->depth;
             r->cur++;
There, fixed it
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by meindnoch on Sunday September 21, 2025 @06:45PM (#45327347)
I can only hope this was made by an LLM and not a real human.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by odie5533 on Sunday September 21, 2025 @02:33PM (#45325376)
Can't use this library in production that's for sure.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by EmilStenstrom on Sunday September 21, 2025 @02:06PM (#45325149)
Submit a PR!
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by uncircle on Monday September 22, 2025 @05:03AM (#45330822)
You don't get Hacker News karma if you quietly fix a bug instead of complaining about it, though.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by habibur on Sunday September 21, 2025 @06:47PM (#45327357)
Will trigger UB if level depth is > 2 billion or in the 2nd case number of lines > 2 billion.

Limit you JS input to 1 GB. I will have more problems in other portions of the stack if I start to receive a 2 GB JSON file over the web.

And if I still want to make it work for > 2GB, I would change all int in the source to 64 bits. Will still crash if input is > 2^64.

What I won't ever do in my code is check for int overflow.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by jcalvinowens on Sunday September 21, 2025 @07:18PM (#45327549)
> What I won't ever do in my code is check for int overflow

Amen. Just build with -fno-strict-overflow, my hot take is that should be the default on Linux anyway.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by zelphirkalt on Monday September 22, 2025 @05:48AM (#45331110)
Crashing without a proper error message, leaving the user wondering what happened, is a table stake in C projects, of course. How do you intend to determine the cause of your crashes and write a meaningful error message for the user, in case of too long input when you don't check overflow?
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by modeless on Sunday September 21, 2025 @07:04PM (#45327468)
I wouldn't expect a library like this to be secure. If you want it to be memory safe, compile it with Fil-C.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @07:09PM (#45327498)
This has nothing to do with memory safety.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by modeless on Sunday September 21, 2025 @07:16PM (#45327533)
This is an overstatement. Yes, UB does not necessarily cause a violation of memory safety, but triggering UB alone is not the goal of an attacker. UB is a means to an end and the end is usually a violation of memory safety leading to arbitrary code execution.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by layer8 on Sunday September 21, 2025 @08:30PM (#45327958)
The primary point was that the code doesn't ensure correct processing (or returning an appropriate error) for all JSON. Even if behavior is defined by the C implementation, the overflow can lead to parser mismatch vulnerabilites, if nothing else. There are likely other "defined" failure modes the overflow can enable here.

UB was a secondary observation, but it also can lead to logic errors in that vein, without involving memory safety.

I'm not sure I agree that UB usually leads to memory safety violations, but in any case, the fact that signed integer overflow is UB isn't what makes the code incorrect and unsafe in the first place.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by robmccoll on Sunday September 21, 2025 @07:36PM (#45327647)
Could just change the input len to an int instead of size_t. Not technically the correct type, but it would make it clear to the user that the input can't be greater than 2^31 in length.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by mscrnt on Sunday September 21, 2025 @11:07PM (#45328731)
cut a PR to improve it; that would be nice
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Gibbon1 on Monday September 22, 2025 @02:17AM (#45329687)
I've been tending to use ssize_t for indexes instead of int. Part of the reason was reading someones decent argument that

   for(int i=0; blah blah; i++)
Is actually broken and dangerous on 64 bit machines.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by oguz-ismail on Monday September 22, 2025 @03:22AM (#45330108)
How is ssize_t any better? It's not part of standard C and is only guaranteed to be capable of holding values between -1 and SSIZE_MAX (minimum 32767, no relation to SIZE_MAX).
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by pgen on Monday September 22, 2025 @03:47AM (#45330287)
The author has kindly provided you with simple, readable, and free code. If you find it incomplete or unsafe, you can always modify it and contribute your changes if you wish to improve it, in accordance with the licence; and thank him while you're at it.
Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by Someone on Monday September 22, 2025 @07:20AM (#45331881)
I wouldn’t rate those as very serious issues for this project. They’ll only be triggered if there are over MAX_INT lines or depth levels in the input. Yes, an attacker might be able to do that, but you’d have to put that input in a memory buffer to call this code. On many smaller systems, that will OOM.

Skimming the code, they also are loose in parsing incorrect json, it seems:

    static bool sj__is_number_cont(char c) {
        return (c >= '0' && c <= '9')
            ||  c == 'e' || c == 'E' || c == '.' || c == '-' || c == '+';
    }

    case '-': case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        res.type = SJ_NUMBER;
        while (r->cur != r->end && sj__is_number_cont(*r->cur)) { r->cur++; }
        break;
that seems to imply it treats “00.-E.e-8..7-E7E12” as a valid json number.

    case '}': case ']':
        res.type = SJ_END;
        if (--r->depth < 0) {
            r->error = (*r->cur == '}') ? "stray '}'" : "stray ']'";
            goto top;
        }
        r->cur++;
        break;
I think that means the code finds [1,2} a valid array and {"foo": 42] a valid struct (maybe, it even is happy with [1,2,"foo":42})

Those, to me, seem a more likely attack vector. The example code, for example, calls atoi on something parsed by the first piece of code.

⇒ I only would use this for parsing json config files.

Being tiny is one thing, but the json grammar isn’t that complex. They could easily do a better job at this without adding zillions of lines of code.

Re:Sj.h: A tiny little JSON parsing library in ~150 lines of... by account42 on Tuesday September 23, 2025 @09:53AM (#45347066)
Easy enough to fix:

  -sj_Reader sj_reader(char *data, size_t len) {
  +sj_Reader sj_reader(char *data, int len) {
Not everyone needs to waste cycles on supporting JSON files larger than 2^31-1.