Live data from Hacker News

My review of the C standard library in practice

nullprogram.com

81–90 of 94 posts

Re: My review of the C standard library in practice

#81
post #73

Earlier quoted context omitted.

That's a total strawman. Errno has nothing to do with signal handling at all. While errno is archaic, it is a completely sound design. That you need to be very careful in signal handlers is common knowledge, and obvious from what they do; signals handlers as a first approximation behave like executed in a separate thread, but even worse since they hijack a running thread and thus block the hijacked thread from execut…

You call it a strawman, I call it knowing what matters when writing portable code across multiple platforms and compilers. You will notice that I have linked Open Group errno documentation in another post. That is what matters, not what GNU thinks.

So, what does matter? What is your actual statement? That you can't use errno in a multi-threaded environment? That you can't use errno in the presence of signal handlers?

What exactly is your complaint about the GNU link? As far as I can see it more or less regurgitates what POSIX has to say about async-signal-safety.

You made some vague statements that a mechanism that is used on billions of devices is fundamentally unsound, while providing no evidence other than maintaining that it would break in conjunction with signal handlers -- when signal handlers are expressly to be used with caution, no matter what you intend to touch.

If you worry that you can't safely touch errno in a signal handler (provided that you reset it) on some broken platform from decades ago, because a spec like this [0] is too much swallow (understandably so, since this spec is a futile attempt to reconcile all the relevant history into a useful document), then I have a solution... Don't touch errno in a signal handler, which is probably solid practical advice anyway.

Oh, and how do you reconcile the fact that many syscalls are declared async-safe by POSIX that may set errno themselves?

"Operations which obtain the value of errno and operations which assign a value to errno shall be async-signal-safe, provided that the signal-catching function saves the value of errno upon entry and restores it before it returns."

Tada!

[0] https://pubs.opengroup.org/onlinepubs/9699919799/functions/V...

Re: My review of the C standard library in practice

#82
post #59

Earlier quoted context omitted.

In practice it works with major thread implementations even before C11, even though it doesn't say so in the standard. You can't call OS functions from a signal handler so I can't see why signals would matter. Have I missed something?

> You can't call OS functions from a signal handler You can, as long as these functions are "async signal safe", see https://man7.org/linux/man-pages/man7/signal-safety.7.html for details.

Oh wow. I mean, fair enough for simple utilities like strlen(). But open(), write(), close()? I had no idea.

Re: My review of the C standard library in practice

#83
post #76

Earlier quoted context omitted.

In practice it works with major thread implementations even before C11, even though it doesn't say so in the standard. You can't call OS functions from a signal handler so I can't see why signals would matter. Have I missed something?

You can, it is UB in what might happen, you either get lucky, or not. Don't expect a compiler error, this is C, where the programmer knows best.

You can call any system function that is marked as async-signal-safe. Whether that's a great idea is a different question.

> Don't expect a compiler error, this is C, where the programmer knows best.

As for this snark, these considerations are pretty much OS and concurrency stuff that transcend any implementation language [0]. No one prevents you to switch back to single-threaded cushy OOP-y Javascript, however note that someone has to implement and run that for you.

[0] yes, signals aren't beautiful, and in 2023 a beautifully designed OS might better do delivery of asynchronous messages using event handling and/or using dedicated threads. However, that's computing history for you, and it can be worked with. If you don't like it find a different platform. C doesn't really require signals, and if you use Unix with a different language, signals won't just go away.

Re: My review of the C standard library in practice

#84

I was unpleasantly surprised by how much locales are used for seemingly trivial things. It's even the case with the C++ iostreams stuff. I ended up writing my own functions in an 'ascii' namespace to avoid all this as much as possible. This [0] rant about locales and how they're nigh-on impossible to use correctly is quite amusing (slightly NSFW) [0] https://github.com/mpv-player/mpv/commit/1e70e82baa9193f6f02...

I understand that wm4 was probably a pretty difficult person to work with, but he got stuff done well and from the safe distance of my chair he was a rather entertaining fellow. The other mpv devs can't miss him but I kinda do.

Re: My review of the C standard library in practice

#85

Earlier quoted context omitted.

What would those languages be? I'm only used to relatively modern languages with "decent string handling", but they almost always treat strings as opaque high level objects with dynamic memory allocation under the hood. Such an approach wouldn't exactly fit into the C philosophy. Also, once UNICODE is added to the mix (which involves a lot more than just the text encoding), a decent string processing library isn't ex…

Pascal would be one example. > with dynamic memory allocation under the hood C strings are typically dynamically allocated, are they not?

Not in the way that is described here. I took "always treat strings as opaque high level objects with dynamic memory allocation under the hood" to mean that the string would have operations, like append, that would reallocated/resize the string if needed. C strings are definitely not that.

Re: My review of the C standard library in practice

#86
post #53

Earlier quoted context omitted.

>It still boggles the mind how a function that leaves a C string potentially unterminated ever even made it into the standard... Easy to say with 50 years of hindsight. These are the pioneers we are talking about here. It boggles the mind how much they got right.

While strcpy is forgivable considering it came from the primordial soup, there is no excuse for strncpy since it was designed to solve the problems with strcpy. And the string library is full of gaffes like this.

Every teen hacker I knew had their own "my_strncpy" function that did something like strncpy(dst, src, len); dst[len-1] = 0;

Re: My review of the C standard library in practice

#87
post #73

Earlier quoted context omitted.

You call it a strawman, I call it knowing what matters when writing portable code across multiple platforms and compilers. You will notice that I have linked Open Group errno documentation in another post. That is what matters, not what GNU thinks.

So, what does matter? What is your actual statement? That you can't use errno in a multi-threaded environment? That you can't use errno in the presence of signal handlers? What exactly is your complaint about the GNU link? As far as I can see it more or less regurgitates what POSIX has to say about async-signal-safety. You made some vague statements that a mechanism that is used on billions of devices is fundamentall…

One thing one learns about writing specifications in English is the meaning of must, should and shall.

As for soundness, it is C we are talking about here.

Re: My review of the C standard library in practice

#88
post #76

Earlier quoted context omitted.

You can, it is UB in what might happen, you either get lucky, or not. Don't expect a compiler error, this is C, where the programmer knows best.

You can call any system function that is marked as async-signal-safe. Whether that's a great idea is a different question. > Don't expect a compiler error, this is C, where the programmer knows best. As for this snark, these considerations are pretty much OS and concurrency stuff that transcend any implementation language [0]. No one prevents you to switch back to single-threaded cushy OOP-y Javascript, however note…

Indeed,

"Transcending POSIX: The End of an Era?"

https://www.usenix.org/publications/loginonline/transcending...

Re: My review of the C standard library in practice

#89
post #87

Earlier quoted context omitted.

So, what does matter? What is your actual statement? That you can't use errno in a multi-threaded environment? That you can't use errno in the presence of signal handlers? What exactly is your complaint about the GNU link? As far as I can see it more or less regurgitates what POSIX has to say about async-signal-safety. You made some vague statements that a mechanism that is used on billions of devices is fundamentall…

One thing one learns about writing specifications in English is the meaning of must, should and shall. As for soundness, it is C we are talking about here.

Where "shall" is usually understood as equivalent to "must". Are you going to cite me one reliable source for the adventurous claims you're making all the time, or are you just going to continue putting out blatant falsehoods and strawmans?

> As for soundness, it is C we are talking about here.

Well the only thing we're doing here is trolling, nothing of substance has been put on the plate so far.

Re: My review of the C standard library in practice

#90
post #88

Earlier quoted context omitted.

You can call any system function that is marked as async-signal-safe. Whether that's a great idea is a different question. > Don't expect a compiler error, this is C, where the programmer knows best. As for this snark, these considerations are pretty much OS and concurrency stuff that transcend any implementation language [0]. No one prevents you to switch back to single-threaded cushy OOP-y Javascript, however note…

Indeed, "Transcending POSIX: The End of an Era?" https://www.usenix.org/publications/loginonline/transcending...

So you're going back to URL firing mode, instead of making clear statements that are reasonably backed up?
Post reply on HN