News for hackers. Stuff that matters.
Re:Porting to GCC 14: C language issues by actionfromafar on Monday February 19, 2024 @10:13AM (#39430599)
I like how delightfully stable GCC and C is when things from C99 is mandatory only now, a quarter of a century later.
Re:Porting to GCC 14: C language issues by fl0ki on Monday February 19, 2024 @10:32AM (#39430843)
And still not even mandatory if you use -fpermissive or -std=c89.

I don't envy how much tech debt they have to deal with, while still trying to make it possible for new code to have a more modern experience. For all the excitement around new languages, we're still going to have a lot of C code for a long time, and difficult work like this should be appreciated.

Re:Porting to GCC 14: C language issues by spookie on Monday February 19, 2024 @11:15AM (#39431413)
The other day I tried to compile a really old program, the Power Crust and its predecessor. It just worked, for some reason I was expecting it not to. I didn't even have the filetype it was supposed to accept, but it was fine, it was just a list of points. One per line.

Sanity is such a rare thing these days.

Re:Porting to GCC 14: C language issues by lelanthran on Monday February 19, 2024 @11:23AM (#39431526)
> I like how delightfully stable GCC and C is when things from C99 is mandatory only now, a quarter of a century later.

This is how stable it is - they were still supporting C constructs from prior to C89 standardisation (which is when, I believe, void pointers were introduced).

"maybe consider using void * in more places (particularly for old programs that predate the introduction of void * into the C language). "

Re:Porting to GCC 14: C language issues by estebank on Monday February 19, 2024 @10:55AM (#39431135)
I can't help but wonder how many bugs could have been detected and fixed a quarter of a century earlier if those lints had been adopted more diligently by the C ecosystem.
Re:Porting to GCC 14: C language issues by loeg on Monday February 19, 2024 @11:27AM (#39431594)
My perception is that more and better warnings, and common use of -Wall, came into vogue around the time of GCC 4, in the mid 2000s. It would have been pretty abrupt to immediately enforce C99 in 1999-2000.
Re:Porting to GCC 14: C language issues by estebank on Monday February 19, 2024 @11:34AM (#39431678)
Yet in 2022 this change from warn by default to error by default was consequential enough for Fedora:

>> So what is the current status? How many packages are going to be affected by this change? How are we going to track the progress?

> I see an unaudited rebuild failure rate of about 10% for rebuilds of source packages that produce arch-full binary packages. This number does not include packages which fail to build in rawhide without the compiler change. It includes packages which configure checks for something that we really do not support (like setproctitle or strlcpy). After the first pass completes, I'll have to do a second pass with the expected-to-be-undeclared functions gathered from the first pass filtered out. That should give us better numbers.

Re:Porting to GCC 14: C language issues by pm215 on Monday February 19, 2024 @12:07PM (#39432097)
My impression is that a lot of the fallout was related to checks in configure scripts and equivalent. That kind of code was traditionally written a lot more sloppily and was much less likely to have the "standard" set of warnings or the make-warnings-into-errors flag enabled. So, yes, quite a lot of packages failed to build, but the offending code was typically not code that was run when somebody was actually using the program and not code where bugs were consequential. And of course Fedora's a big collection of software with a pretty long tail -- 10% of packages likely works out to a lot less than 10% of user-executing-a-program hours.
Re:Porting to GCC 14: C language issues by jcranmer on Monday February 19, 2024 @12:15PM (#39432181)
I would guess that the main cause of failures is autoconf, which has a habit of relying on extremely dubious C code to check for feature availability. On top of that, the compiler runs are done in a way to guarantee that no one will see any warnings, even errors can tend to be silently ignored (!), and a particular version is often baked into a source package, so upgrading autoconf to a more sane version tends to take eons.
Re:Porting to GCC 14: C language issues by loeg on Monday February 19, 2024 @11:49AM (#39431887)
If this was intended to be responsive to my comment, I'm missing the connection.
Re:Porting to GCC 14: C language issues by haolez on Monday February 19, 2024 @10:21AM (#39430712)
Didn't some parts of C99 get rolled back on C11 and other newer ones?
Re:Porting to GCC 14: C language issues by fl0ki on Monday February 19, 2024 @10:40AM (#39430941)
Variable Length Arrays were mandatory in C99, optional in C11, and are mandatory again in C23.

I'm curious how these dynamics play out in less popular compilers. If a compiler implemented VLAs in C99, they almost certainly still have that feature for backwards compatibility even after they support C11.

Is there any compiler which appeared on the scene between C11 and C23, and during that window, chose not to support VLAs and thus C99? It's not like C11 itself was very widely adopted, precisely because of the long implementation and industry rollout windows.

Re:Porting to GCC 14: C language issues by Joker_vD on Monday February 19, 2024 @10:47AM (#39431025)
Uh, are they mandatory again?

                       For these reasons, we propose to make variably-modified types
    mandatory in C23. VLAs with automatic storage duration remain an optional language
    feature due to their higher implementation overhead and security concerns on some
    implementations (i.e. when allocated on the stack and not using stack probing).
from N2778 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2778.pdf)
Re:Porting to GCC 14: C language issues by fl0ki on Monday February 19, 2024 @11:01AM (#39431216)
I'm not tuned in to the nuances here but I note that whoever felt they understood it well enough to update Wikipedia summarized this as "The C23 standard makes VLA types mandatory again. Only creation of VLA objects with automatic storage duration is optional."

https://en.wikipedia.org/wiki/Variable-length_array#C99

If that phrasing isn't accurate I'm sure they'd appreciate an edit.

Re:Porting to GCC 14: C language issues by fractalb on Monday February 19, 2024 @10:54AM (#39431120)
Thanks for the link. As I understood it, it's just the support for the syntax that is mandatory.

> Variable length arrays with automatic storage duration are a conditional feature that implementations need not support

Re:Porting to GCC 14: C language issues by LegionMammal978 on Monday February 19, 2024 @11:22AM (#39431504)
The syntax and also the semantics. For instance, you can take the sizeof() a variably-modified type, or the offsetof() one of its fields, and the compiler has to do all the layout calculations implied by the type declaration at runtime. These features are partially what motivated the mandatory support. The only part that is still optional is using such types by value as stack variables (i.e., variables with automatic storage duration).
Re:Porting to GCC 14: C language issues by actionfromafar on Monday February 19, 2024 @11:26AM (#39431562)
How do you create a variable array on the heap?

confused

Re:Porting to GCC 14: C language issues by klodolph on Monday February 19, 2024 @11:38AM (#39431749)
With `malloc`, and converting the pointer to the correct type.

    void function(int n) {
      int (*arr)[n] = malloc(sizeof(*arr));
    }
Re:Porting to GCC 14: C language issues by actionfromafar on Monday February 19, 2024 @11:51AM (#39431907)
wow, that's wild
Re:Porting to GCC 14: C language issues by uecker on Monday February 19, 2024 @01:59PM (#39433345)
That is exactly how you create any other type of object on the heap.
Re:Porting to GCC 14: C language issues by actionfromafar on Monday February 19, 2024 @04:43PM (#39435174)
Snarky or just know a lot? It changes quite a bit how the compiler works. It has to know to make sure malloc gets the array element size argument multiplied in runtime by n. To a mere user it broke my mental shorthand of how a C compiler works.
Re:Porting to GCC 14: C language issues by Joker_vD on Monday February 19, 2024 @09:14PM (#39437272)
> It has to know to make sure malloc gets the array element size argument multiplied in runtime by n

Um, C compilers already do that with arrays with compile-time lengths.

    #include <stdio.h>

    void main(void) {
        char x[20][30];
        printf("%zu\n%zu\n", sizeof(x), sizeof(x[0]));
    }
prints

    600
    30
so you can have "char *y = malloc(sizeof(x)); memcpy(y, x, sizeof(x));" and it must work since C89 at least. The main problem with VLAs is that they make exact the stack frame size unknown until runtime which complicates function prologues/epilogues but that's the problem in the codegen part of the backend, the semantics machinery is mostly in the place already.

P.S. And yes, uecker is a member of the ISO C WG14 and GCC contributor, according to his profile.

Re:Porting to GCC 14: C language issues by LegionMammal978 on Tuesday February 20, 2024 @12:32AM (#39438349)
I think there is a real difference: in the static case, the compiler can just recurse into the type definition at any point, compute fully-baked sizes and offsets, and cache them for the rest of the compilation. But in the dynamic case, you end up with the novel dataflow of types that depend on runtime values, and more machinery is necessary to track those dependencies.

Of course, this runtime tracking has always been necessary for C99 VLA support, but I can easily see how it would be surprising for someone not deeply familiar with VLAs, especially given how the naive mental model of "T array[n]; is just syntax sugar for T *array = alloca(n * sizeof(T));" is sufficient for almost all of their uses in existing code. (In any case, it's obviously not "creating an object on the heap" that's the unusual part here!)

Re:Porting to GCC 14: C language issues by Joker_vD on Tuesday February 20, 2024 @06:30AM (#39440064)
> more machinery is necessary to track those dependencies.

Well, is it much more machinery? IIRC doing

    void f(size_t n) {
        int x[n];
        n += 1;
        ...
does not resize x, so there is no dataflow dependency or rather, x depends on a hidden const variable so no additional dataflow analysis necessary.
Re:Porting to GCC 14: C language issues by LegionMammal978 on Tuesday February 20, 2024 @11:09AM (#39442998)
Yet

  void f(size_t n, int cond) {
      if (cond) { n += 1; }
      int x[n];
      ...
does resize x depending on the value of cond, so the size can't necessarily be known until the point where the type (int[n] in this case) is named.

Also, the compiler has to make sure it keeps around implicit locals to store the variable layouts, so that code like

  void f(size_t n) {
      typedef int array[n];
      n += 1;
      array x;
      ...
functions as specified. This kind of pattern is one of the bigger things setting the feature apart from just "syntax sugar for alloca()".
Re:Porting to GCC 14: C language issues by pjmlp on Tuesday February 20, 2024 @04:46AM (#39439444)
It only works at the same level, the moment they get passed in as arguments, they decay into pointers even if using [].
Re:Porting to GCC 14: C language issues by fractalb on Monday February 19, 2024 @10:48AM (#39431040)
VLA support is mandatory in C23? I'd like to know the rationale behind this decision. Can you provide any references? Thanks.
Re:Porting to GCC 14: C language issues by veltas on Monday February 19, 2024 @01:19PM (#39432933)
If I understand right, this is mandatory:

  void example(int n) { printf("%zu", sizeof(int[n])); }
  void example2(int n, int (*a)[n]) { }
This is optional:

  void example3(int n) { int x[n]; }
Re:Porting to GCC 14: C language issues by garaetjjte on Monday February 19, 2024 @01:07PM (#39432788)
Portable code couldn't use VLAs anyway, it was never supported in MSVC.
Re:Porting to GCC 14: C language issues by uecker on Monday February 19, 2024 @02:49PM (#39433888)
Portable C code could not use MSVC until recently, because MSVC was stuck with C89 because MS wanted everybody to switch to C++.
Re:Porting to GCC 14: C language issues by pjmlp on Monday February 19, 2024 @01:44PM (#39433188)
Among others.
Re:Porting to GCC 14: C language issues by uecker on Monday February 19, 2024 @02:47PM (#39433863)
VLAs were optional in C11 and compilers that supported them in C99 also supported them in C11. The only important compiler not supporting VLAs is MSVC, but this compiler also did not support other features from C99. People using MSVC were stuck using an obsolete version of C for a long time. Recently this changed and MSVC started to support C11 and C17 - skipping C99.