News for hackers. Stuff that matters.
Re:Best aspects of C by pornel on Tuesday November 17, 2020 @11:20AM (#25125540)
There's a lot of subtly broken stuff in C stdlib. For example, time.h calls that may touch the TZ env var aren't thread-safe, because getenv is unsafe (it gives a pointer to its internal globally mutable data structure without locks).

Because C is so bare-bones, it usually leans on POSIX as its extended standard library, and that is also full of old cruft.

Re:Best aspects of C by einpoklum on Tuesday November 17, 2020 @03:12PM (#25129100)
One thing that is not-so-subtly broken is re-entrancy (e.g. in functions like `strtok()`; fixed later with `strtok_s()` in C11:

https://en.cppreference.com/w/c/string/byte/strtok

About getenv: This sort-of fixed with `getenv_s()` in C11

https://en.cppreference.com/w/c/program/getenv

Re:Best aspects of C by loeg on Tuesday November 17, 2020 @03:17PM (#25129184)
Re: getenv_s: Annex K is (still) optional. Compliant C11+ implementations may not have getenv_s.
Re:Best aspects of C by josephcsible on Tuesday November 17, 2020 @05:39PM (#25130875)
This is an understatement. The only relevant platform that supports Annex K is MSVC, and this is unlikely to change any time soon.
Re:Best aspects of C by loeg on Tuesday November 17, 2020 @07:39PM (#25131791)
And even MSVC does not actually implement the API specified in Annex K. Annex K was inspired by MSVC's design, but they diverged:

> Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. For example, it doesn't provide the set_constraint_handler_s function but instead defines a _invalid_parameter_handler _set_invalid_parameter_handler(_invalid_parameter_handler) function with similar behavior but a slightly different and incompatible signature. It also doesn't define the abort_handler_s and ignore_handler_s functions, the memset_s function (which isn't part of the TR), or the RSIZE_MAX macro.The Microsoft implementation also doesn't treat overlapping source and destination sequences as runtime-constraint violations and instead has undefined behavior in such cases.

[0]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm

Re:Best aspects of C by icedchai on Wednesday November 18, 2020 @10:38PM (#25145959)
This makes sense when you consider the history of C. Multi-threading was a relatively late add on, and outside of the standard library.

When I learned C (early 90's), multi-threading wasn't even something you considered. It was either multiple processes or an event loop with select/poll.

Re:Best aspects of C by kevin_thibedeau on Tuesday November 17, 2020 @03:49PM (#25129620)
strtok_r() is everywhere and easy to retrofit. It's also easy to implement using strspn() and strcspn() if you don't have it.