223 points · by mariuz · 4y ago · 178 comments
A much more interesting question is if you were writing curl today, what would you do. If the answer is still 'C89' then we as a profession have to wonder why - did we get it exactly right, and there are no lessons from the last 30 years, or is the fact there are no better alternatives deeply depressing.
C89 is still a reasonable choice today. I don't think it's depressing. It's a good language, and hitting the sweet spot of both language design and implementation is really hard, so you'd expect to have very few options.
Particularly the MSVC ecosystem is identified in TFA as being a late adopter.
Once built c99 code can run where it wants.
Is that true on the MSVC ecosystem?
Don't you have to compile separately for each `msvcrt` environment, as I thought they aren't binary compatible? And would a non-C99 msvcrt necessarily have an `snprintf()` implementation in its libc-equivalent dll?
What systems don't support a version gcc that compiles c99 at this point?
Why isn't it possible that the updates aren't as good as the people who wrote them thought they were?
VLAs aren't a problem. VLA in C are.
But that's not because there is any issue with VLAs. The issue is that C concepts are stupid, but nobody fixes the roots of the issues.
If you put something of variable length (a VLA) into something with limited static length (the "stack") it will explode. That's nothing new and nothing special or exclusive to VLAs. Actually, exactly this is one of the main issues with the bad C design since inception: It does not do bound checks (especially no static ones; as this would require proper depended typing for safe VLAs). Out of bound access will just "explode" as always in C (likely leaving a nice security crater).
To be honest I don't get why we're still stuck with the stack / heap nonsense. There is not stack (or heap). There is only memory.
What would be much more interesting would be direct control over the caches… Instead we still use the pure fantasy products "stack" and "heap" which are actually irrelevant (as they don't exist in the end).
There is nothing "special" about "stack" memory. That's just a very primitive region based automatic memory allocator backed into the C runtime!
The whole "using registers" thingy in context of "stack" is also just fake by now. You don't use HW registers—but some virtualization of them presented to you by the VM that runs inside the CPU. So you don't control register allocation anyway! So this could be made completely transparent without any impact. (The VM inside the CPU does the actual register allocation fully automatic. Presenting the "faked virtual ISA registers" to the outside world just to make "legacy" code happy).
That's not what the post said. He didn't say that C99 offered no advantages to anyone. He said no one could come up with benefits to the curl project that would be gained by moving to C99, therefore the risk introduced by doing so was not worth it for now (my paraphrase, obviously).
It sounds to me like a perfectly good reason to stay with the current standard for that project.
[edited to fix a typo]
It's still deeply depressing how costly/impractical it is to apply improved technologies in the long tail of environments that isn't "linux on amd64" and similar, but it's not really a language design question in my opinion. We didn't get it "exactly right", we got it "good enough", and upgrade costs are prohibitively high for the general case.
My opinion is C is already way too rich and complex. I would stick to c89 with benign bits of c99 and c11. The benchmark being "one average system developer coding a naive and real life C compiler in a reasonable amount of time and effort".
That said, I know that my "next" C compiler will probably be a RISC-V assembler with a very conservative usage of a macro preprocessor.
If Curl already has its own "decent and functional replacement" for `snprintf()` that's used extensively throughout the codebase, or if they just don't need that functionality (I haven't checked) then I guess that's not an issue. But that would be the big selling point as far as I'm concerned.
Like 1980s Internet protocol features the rationale for weird things in C is more often "That's how Unix works" than "This is actually a clever safety feature".
... AND where you want to pad the remaining space with zero bytes, so that you don't leak uninitialized memory onto the disk, or network.
The null byte padding behavior of strncpy makes it clear what the intended use was.
Also, the way C initializes character arrays from literals has strncpy-like behavior, because the entire aggregate is initialized, so the extra bytes are all zero:
char a[4] = "a"; // like strncpy(a, "a", 4);
char b[4] = "abcd"; // like strncpy(a, "abcd", 4);
the compiler could literally emit strncpy calls to do these initializations, so we might say that strncpy is a primitive that is directly relevant for run-time support for a C declaration feature.If you look at this function assuming it's a safety feature, that's a huge surprise, and indeed if you were skimming you might miss what it does because (in the context of "it's a safety feature") this is an insane choice. "Why would you do that?". Well, because it's not a safety feature.
The perf cost isn't what you'd expect from a safety feature either. Suppose we have a 1MB buffer, and we strncpy "THIS" into it using n = 1024. That's just four bytes right? Nope. strncpy() will write "THIS" and then 1020 zero bytes.
Not only that, but (because of its actual purpose) it also fills the buffer with nuls, which is a complete waste of resources.
So yes, the original intent of strncpy() germane to the GPs comment, because it makes strncpy actively dangerous and complete shit when working with C strings.
It was completely clear, and can easily be inferred from its specified behaviour.
It's just completely useless nowadays, because its purpose is essentially obsolete, because the data type it works with is almost never used anymore.
strncpy works with fixed-size nul-padded fields as you'd find in e.g. mainframe-type software. That is why it:
- fills the destination buffer with NULs if the source is shorter
- does not nul-terminate if the source is the same size or longer than the destination
strncpy is essentially equivalent to zero-ing a buffer of size `n` then copying the first `n` bytes of src (up to the first nul) in the target
Some typo on the buffer limits and the same hazards as always.
Only fix is hardware memory tagging.
no-nul is not the goal of strncpy, it's the effect of strncpy.
strncpy is designed to work on fixed-size, nul-padded strings. That's why it fills the destination buffer with nuls if the source is too short, and it doesn't guarantee nul-termination (if the source is exactly the size of the destination).
snprintf(dest, sizeof dest, source); // BAD code do not repeat
that looks great at first sight, just another size-checked way of copying strings, but remember that the third argument to `snprintf()` [1] is of course a `printf()`-style formatting string. So if that `source` argument contains any percent symbols, there's gonna be a party in your computer and both Undefined and Behavior are going to show up. You don't want that.Instead, if you want to use `snprintf()` for this, remember to do:
snprintf(dest, sizeof dest, "%s", source);
[1]: https://linux.die.net/man/3/snprintfI mean, it's a trivially replicated function, you can just copy it over from an existing codebase. So it's not exactly a major feature.
char *stecpy(char *d, const char *s, const char *e)
{
if (e) e--;
while (d < e && *s)
*d++ = *s++;
if (d)
*d = '\0';
return d;
}
main() {
char buf[64];
char *ptr, *end = buf+sizeof(buf) ;
ptr = stecpy(buf, "hello", end);
ptr = stecpy(ptr, " world", end);
}
As discussed here https://twitter.com/hyc_symas/status/1382298601641152513The point is that the end of the buffer is invariant, there's no reason to screw around recalculating the length of remaining space after each copy into the buffer. This also fixes the nonsense of strcpy/strcat returning the same dst pointer that was passed in. By returning the pointer to where copying ended, you don't need a separate strcat function any more, nor do you have the Shlemiel The Painter problem with strcat.
Functions can always be implemented with additional header files.
They aren't actual extensions to the language.
> Like improving the test suite, increasing test coverage, making sure more code is exercised by the fuzzers.
I wonder if using a more recent version of C might draw in more developers willing to contribute (in a similar vein to Linux kernel introducing Rust).
struct point p = { getx(obj), gety(obj) }; /* C90 error, C99 OK */
GCC had this as an extension before C99. Coding around that one can get ugly, and it's just a syntactic limitation.Also, the C99 preprocessor is more powerful, with variadic support.
The macros I implemented in the cppawk project (awk with C preprocessor) would be impossible without C99. I was able to make a multi-clause loop macro, with user-definable clauses.
But in retrospect I'm surprised to see how many features we used liberally would have been unavailable in pure C89. snprintf first and foremost. But also // comments, __func__, and stdint.h
void foo(size_t n) { int a[n][n]; }
does not have to be supported outside C99 but void bar(size_t n, void *p) { int (*pa)[n][n] = p; }
has to be in both C99 and C23 (though not in C11 or C17).Is there risk associated with changing? Probably yes in terms of security and limiting compatibility.
It probably doesn't make sense to change now unless there are specific reason(s) that will lead to impactful improvements.
> ... risk opening the flood gates for people rewriting things...
This sounds like "we feel that changes are needed but we will not be able control it".
Only if a C99 compiler is available. As the post points out:
> The slowest of the “big compilers” to adopt C99 was the Microsoft Visual C++ compiler, which did not adopt it properly until 2015 and added more compliance in 2019. A large number of our users/developers are still stuck on older MSVC versions so not even all users of this compiler suite can build C99 programs even today, in late 2022.
(emphasis mine)
[1] Also because the C++ committee ignored Herb and just forced it into later C++ revs from a library perspective.
Naturally without the stuff that got made optional in C11, no need to spend development effort on legacy features.
> [...] we would have to go gently and open up for allowing new C99 features slowly
> A challenge with that approach, is that it is hard to verify which features that are allowed vs used as existing tooling normally don’t have that resolution.
> The question has also been asked that if we consider bumping the requirement, should we then not bump it to C11 at once instead of staying at C99?
The motivation ultimately boils down to:
> Ultimately, not a single person has yet been able to clearly articulate what benefits such a C flavor requirement bump would provide for the curl project
The syntax isn't as nice, and you have to remember to do it (maybe a linter can help with that) but it ends up more of a "nice to have".
Isn't cURL susceptible to this too? (Not to mention i always find that style much nicer :)
What features of C99 isn't this true of today?
IIRC, MSVC stubbornly refuses to add support for variable-length arrays (which AFAIK are required for full C99 support). I don't know if there's anything else on that list of C99 features that MSVC doesn't support yet.
You are correct that VLAs are mandatory for C99, but they turned out to be such a bad idea that they are optional in C11 onwards.
> I don't know if there's anything else on that list of C99 features that MSVC doesn't support yet.
IIRC, MSVC's `snprintf` and friends are broken (returns incorrect values and/or interprets the size parameter incorrectly). I think all of the annexure K stuff is broken in MSVC.
Doesn’t annex k only exist in MSVC because it’s a bunch of crap MS got the committee to add and no one else wanted to implement?
And isn’t it a C11 thing?
They were made optional in C11. It was a good decision because it's a dangerous misfeature.
// VLA
int foo(int n) { int array[n]; }
// flexible array member
struct s { int n; double d[]; }; struct s s1 = malloc(sizeof (struct s) + (sizeof (double) 8));
There is a Blog from Raymond on this[1]. The TL;DR: is that zero length arrays and FAMs weren't legal until C99. Not sure where the support from MSVC factors in. But that's the story and AFAIK he's sticking too it.
[1] https://devblogs.microsoft.com/oldnewthing/20040826-00/?p=38...
[0] https://learn.microsoft.com/en-us/cpp/preprocessor/preproces...
Cool.