Live data from Hacker News

Love C, hate C: Web framework memory problems

alew.is

181–190 of 218 posts

Re: Love C, hate C: Web framework memory problems

#181

Earlier quoted context omitted.

> LLMs are probably the best way to learn programming languages right now. Books still exist, be they in print or electronic form.

Examples are the best documentation, and we now have a machine to produce infinite examples tailored specifically to any situation

Pending on the quality of the examples, of course.

Re: Love C, hate C: Web framework memory problems

#182

While the classic "Parse, don’t validate"[0] paper uses Haskell instead of C as its illustrative programming language, the approach detailed is very much applicable in these scenarios. 0 - https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...

> While the classic "Parse, don’t validate"[0] paper uses Haskell instead of C as its illustrative programming language, the approach detailed is very much applicable in these scenarios. Good thing someone (i.e. me ) took the time to demonstrate PdV in C: https://www.lelanthran.com/chap13/content.html

I appreciate that link - now I see the parallels between “consolidate allocation in C to the extent that the rest of your code doesn’t have to worry”, and “consolidate validation in C” to the extent that…”.

Re: Love C, hate C: Web framework memory problems

#183

Earlier quoted context omitted.

Yes but how? After the overflow they still have to know the address of the next call site and the server would be in a UB state.

The code is on github. Figure out a way to get a shell through that code and you're hosed if someone recognizes it in active use.

I mean tha hacker won't know what software is running on the server, unless the server announces itself which can be traced to the repo, but then, why ?? Who cares about this guy's vps? This whole thread makes no sense to me and I'm the only one questioning.

Re: Love C, hate C: Web framework memory problems

#184
post #94

As an aside, it's amusing that it took 25 years for C coders to embrace the C99 named struct designator feature: HttpParser parser = { .isValid = true, .requestBuffer = strdup(request), .requestLength = strlen(request), .position = 0, }; All the kids are doing it now!

I’m still regularly getting on projects and moving C89 variable declarations from the start of functions to where they’re initialized, but I guess it’s not the kids doing it.

I only declare variables at the begin of a block, not because I would need C89 compatibility, but because I find it clearer to establish the working set of variables upfront. This doesn't restrict me in anyway, because I just start a new block, when I feel the need. I also try to keep the scope of a variable as small as possible.

Re: Love C, hate C: Web framework memory problems

#185

Earlier quoted context omitted.

> The difference is that with signed integers, your sanity check is simple and always the same and requires no thought for edge cases: `if(index max)` Wait, what? How is that easier than `if (index > max)`?

Because if max is a calculated value, it could silently wrap around and leave index to cause a buffer overflow. Or if index is counting down, a calculated index could silently wrap around and cause the same issue. And if both are calculated and wrap around, you'll have fun debugging spooky action at a distance! If both are signed, that won't happen. You probably do have a bug if max or index is calculated to a negati…

I have no clue what cases you have in mind, can you give some examples? Surely when you have index as unsigned the maximum would be represented unsigned as well?

Re: Love C, hate C: Web framework memory problems

#186
post #81
post #39

Earlier quoted context omitted.

Of course. You can do it in a single pass/just parse the token stream. There are various implementations like: https://zserge.com/jsmn/

It requires manual allocation of an array of tokens. So it needs a backing "stack vector" of sorts. And what about escapes?

For escapes you can mutate the raw buffer with data in place, since a single escape always expands to fewer characters than the escape itself.

Re: Love C, hate C: Web framework memory problems

#187
post #30

Earlier quoted context omitted.

I've had some fun reviewing some very old code I wrote (1980's) to see what it looked like to me after such a long time of gaining experience. It's not unlike what the OP did here, it reads cleanly but I can see many issues that escaped my attention at the time. I always compared C with a very fast car: you can take some corners on two wheels but if you make a habit of that you're going to end up in a wall somewhere.…

I think the correct comparison is a sharp knife. It is extremely useful and while there is a risk it is fully acceptable. The idea that we should all use plastic knifes because there are often accidents with knifes is wrong and so is the idea that we use should abandon C because of memory safety. I follow computer security issues for several decades, and while I think we should have memory safety IMHO the push and ar…

The real problem with C is that it's not just a sharp knife, it's a knife with poor ergonomics that makes it more prone to cutting yourself.

The answer to that though is probably more something like Zig than something like Rust.

Re: Love C, hate C: Web framework memory problems

#188

Earlier quoted context omitted.

Yup, unsigned math is just nasty. Actually, unchecked math on an integer is going to be bad regardless of whether it's signed or unsigned. The difference is that with signed integers, your sanity check is simple and always the same and requires no thought for edge cases: `if(index max)`. Plus ubsan, as mentioned above. My policy is: Always use signed, unless you have a specific reason to use unsigned (such as memory…

unsigned is easier: 'if(index >= max)' and has fewer edge cases because you don't need to worry about undefined behavior when computing index.

Just because it's UB doesn't mean it's not a problem, though. If you do unsigned arithmetics but don't account for the possibility of wraparound on overflow, the resulting value is well-defined, but it does you no good if you then try to e.g. index using it and cause a buffer overflow.

Re: Love C, hate C: Web framework memory problems

#189

Earlier quoted context omitted.

> Could a determined hacker get to your server without even knowing what weird software you cooked up and how to exploit your binary? Yes.

Yes but how? After the overflow they still have to know the address of the next call site and the server would be in a UB state.

UB state doesn’t mean totally uncontrollable or opaque.

There are lots of ways the server could leak information about its internal state, and exploits have absolutely been implemented in the past based only on what was visible remotely.

Re: Love C, hate C: Web framework memory problems

#190

Earlier quoted context omitted.

> Good C code will try to avoid allocations as much as possible in the first place. I've upvoted you, but I'm not so sure I agree though. Sure, each allocation imposes a new obligation to track that allocation, but on the downside, passing around already-allocated blocks imposes a new burden for each call to ensure that the callees have the correct permissions (modify it, reallocate it, free it, etc). If you're doing…

is there any system where doing the basics of http (everything up to framework handoff of structured data) are done outside of a single concurrency unit?

Not exactly what you’re looking for, but https://github.com/simdjson/simdjson absolutely uses micro-parallel techniques for parsing, and those do need to think about concurrency and how processors handle shared memory in pipelined and branch-predicted operations.
Post reply on HN