Live data from Hacker News

Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

github.com

31–40 of 103 posts

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#31

Earlier quoted context omitted.

> I really need to learn more about Zig, but from what I know, there are still worlds of possibilities that a modern, well-designed language offers over something like lib0xc. Doesn't Apple have a nice `defer { }` block for cleanup? Did you include that in lib0xc? I didn't see in on your README.

In C++ you can implement such a thing using destructors, which are guaranteed to run in reverse order on scope exit even in the presence of exceptions. Alexei Alexandrescu's Scopeguard did this (in the 90s I think, long before C++11). But in standard C, there's no mechanism that this could be attached to (especially if you want to use "C exceptions", a.k.a. setjmp()/longjmp()). Maybe the compilers they support all ha…

Yes, because all compilers support a non-standard defer mechanism, its now being considered for inclusion into standard C. [0]

And that suggested defer standard, is already available from GCC 9 and clang 22.

[0] https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3734.pdf

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#32
post #23

Interesting that a project from Microsoft doesn't support MSVC or Windows.

I suspect in 20 years Windows will be a Linux distribution with a compatibility layer.

People say that kind of thing on HN every now and then. I have no idea why this idea is around, it's a complete fantasy in my opinion. I say this as someone who mostly uses Linux.

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#33
post #20
post #17

Earlier quoted context omitted.

> This might be a dumb question, but using this + clang bounds-safety, whats the difference between this and something like Zig or Odin. I really need to learn more about Zig, but from what I know, there are still worlds of possibilities that a modern, well-designed language offers over something like lib0xc. Zig's ability to evaluate any expression at compile-time is one such example. But generally, lib0xc gives you…

Wouldn't the last case (void *) hurt embedded C development, or retrogaming with direct memory access and pointers?

Not really. You'd still be able to address memory as bytes. The problem with void * is that it strips all bounds information by its nature. Most of the time when you're passing a void * without an associated length (e.g. context pointers, objects that you pinky-swear are of a certain type), it indicates a failure in the language. That's the stuff I think needs to be eliminated.

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#35
post #17

Earlier quoted context omitted.

> This might be a dumb question, but using this + clang bounds-safety, whats the difference between this and something like Zig or Odin. I really need to learn more about Zig, but from what I know, there are still worlds of possibilities that a modern, well-designed language offers over something like lib0xc. Zig's ability to evaluate any expression at compile-time is one such example. But generally, lib0xc gives you…

> I really need to learn more about Zig, but from what I know, there are still worlds of possibilities that a modern, well-designed language offers over something like lib0xc. Doesn't Apple have a nice `defer { }` block for cleanup? Did you include that in lib0xc? I didn't see in on your README.

I think defer has been included in the next round of working group proposals for C2y, but I don't think Apple's clang has it. Maybe it's there as a language extension and I just didn't see it.

What lib0xc has is some cleanup attributes that you can apply to variables to e.g. automatically free a heap allocation or close a file descriptor, at end of scope. Personally, I like variable annotations much more than defer for these uses, but they accomplish the same thing. I've also found that using those attributes inherently pushes your code to make ownership more explicit. I personally stopped being terrified of double-pointers and started using them for ownership transfers, which eliminates a large class of bugs.

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#36
post #11

Author here, I posted this in Show HN but someone clearly beat me to it. So I'll repost my blurb from there. Various patterns for safer C programming have been cargo-culting around the industry for decades. Because the language evolves intentionally slowly, these patterns rarely get folded into the language as first-class constructs and are passed down through the generations in a sort of oral tradition of programmin…

Glad to see you’re still doing great stuff, and also very glad to see your new employer supports such things, especially compared to our old employer! Part of why I retired around the same time you left was because I wanted to make and share things.

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#39
post #11

Author here, I posted this in Show HN but someone clearly beat me to it. So I'll repost my blurb from there. Various patterns for safer C programming have been cargo-culting around the industry for decades. Because the language evolves intentionally slowly, these patterns rarely get folded into the language as first-class constructs and are passed down through the generations in a sort of oral tradition of programmin…

Thanks!

Two notes: GCC has its "access" attributes which can give you similar bounds safety as clang.

Please see also my experimental library. https://codeberg.org/uecker/noplate/ While I do not had enough time to polish it yet, I think it provides some very nice interfaces with improve type and bounds safety, and are also rather convenient.

Also I wonder what parts are redundant if you have FORTIFY_SOURCE ?

(And thank you for working in this topic. If you continue, please reach out to us)

Re: Lib0xc: A set of C standard library-adjacent APIs for safer systems programming

#40
post #31

Earlier quoted context omitted.

In C++ you can implement such a thing using destructors, which are guaranteed to run in reverse order on scope exit even in the presence of exceptions. Alexei Alexandrescu's Scopeguard did this (in the 90s I think, long before C++11). But in standard C, there's no mechanism that this could be attached to (especially if you want to use "C exceptions", a.k.a. setjmp()/longjmp()). Maybe the compilers they support all ha…

Yes, because all compilers support a non-standard defer mechanism, its now being considered for inclusion into standard C. [0] And that suggested defer standard, is already available from GCC 9 and clang 22. [0] https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3734.pdf

[flagged]
Post reply on HN