Live data from Hacker News

Sj.h: A tiny little JSON parsing library in ~150 lines of C99

github.com

151–160 of 248 posts

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#151

This is rather lenient. There's not anything wrong with that (although perhaps it should be noted for people that will use it without looking at the code), but it's the main reason this can be so small. Using their demo in the readme: {"x",10eee"y"22:5,{[:::,,}]"w"7"h"33 rect: { 10, 22, 7, 33 }

Parser, implies the input is assumed to be valid, validating is a whole other problem not covered by this library.

I don’t know what else you call a library that just extracts data.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#152

Earlier 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.

Not really. I deal with this everyday. If the library has a limit on the input size, it should mention this.

If you deal with this every day, you're an outlier.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#153
post #140

Earlier quoted context omitted.

This has nothing to do with memory safety.

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.

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 C99

#154
post #8

What I love about this author's work is that they're usually single-file libraries in ANSI C or Lua with focused scope, easy-to-use interface, and good documentation. And free software license. Aside from the posted project, some I like are: - log.c - A simple logging library implemented in C99 - microui - A tiny immediate-mode UI library - fe - A tiny, embeddable language implemented in ANSI C - microtar - A lightwe…

I vendor in log.c all the time for C projects! I had no idea the author was relatively prolific. Would really recommend checking out log.c, it's really easy to hack in what you need to.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#155
post #116

Earlier quoted context omitted.

What is the stance of Your Average Corp’s security department on public domain software? Do they accept software under such licensing (or lack thereof)?

Who cares? Seriously. Whether a commercial entity who wants to be able to benefit from your work accepts the license you choose for work you do is as much a concern as whether or not the prime minister of Liechtenstein accepts the color you paint the outside of your house in the USA. That is: none.

Kinda depends on whether you're publishing open source software so that people can use it. And if you're not publishing open source software so that people can use it, why exactly are you doing it? If you don't want people to use it, GPL is the way to go. If you do want people to use it, MIT or BSD is a much better way to go.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#156

Earlier quoted context omitted.

It's open source, not free software.

"Source Available" and "Open Source" (with an OSI-approved license) are the terms you're looking for. "Free as in speech, or free as in beer?" is your rallying cry.

Or Free as in Ebola, in the case of GPL-licensed software. Whatever happened to Free as in Air and Sunshine?

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#157
post #116

Earlier quoted context omitted.

Who cares? Seriously. Whether a commercial entity who wants to be able to benefit from your work accepts the license you choose for work you do is as much a concern as whether or not the prime minister of Liechtenstein accepts the color you paint the outside of your house in the USA. That is: none.

Kinda depends on whether you're publishing open source software so that people can use it. And if you're not publishing open source software so that people can use it, why exactly are you doing it? If you don't want people to use it, GPL is the way to go. If you do want people to use it, MIT or BSD is a much better way to go.

Linux, Git and the entire GNU system are counterexamples. Meanwhile FreeBSD dies by the day.

People != the legal departments of corporations.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#158
post #120
post #116

Earlier quoted context omitted.

Who cares? Seriously. Whether a commercial entity who wants to be able to benefit from your work accepts the license you choose for work you do is as much a concern as whether or not the prime minister of Liechtenstein accepts the color you paint the outside of your house in the USA. That is: none.

Bad analogy.. if they truly care what colour your house is then there's plenty of strings they could pull. I mean, a good number of large U.S. company's tax and corporate structures depend heavily on Liechtenstein's government’s rules..

Some people have standing for better or mostly worse - HOAs and local councils. The government of Liechtenstein does not.

Re: Sj.h: A tiny little JSON parsing library in ~150 lines of C99

#159
post #39

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

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.

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 C99

#160
Does C99 specify that this structure would be 0 initialized by default, or is this line missing a = { 0 }? - https://github.com/rxi/sj.h/blob/5cb5df45c8c37fd8c2322026a11... - it reads to me like r->depth might be randomly initialized and equal to depth by random chance on the first iteration of the sj__discard_until loop
Post reply on HN