liburing icon indicating copy to clipboard operation
liburing copied to clipboard

Zero length arrays are a compiler specific thing!

Open the-moisrex opened this issue 2 years ago • 3 comments

Zero length arrays are a compiler-specific features and probably should not be used because even though this library is a Linux specific thing, people using it may want to use it with compiler flags like -std=c17 -pedantic which means they can't have these kinda features be used by a library, specially in header files.

https://github.com/axboe/liburing/blob/58ec7c1aea2d57acbc3d398d0c8b2625746eaf04/src/include/liburing/io_uring.h#L637

https://github.com/axboe/liburing/blob/58ec7c1aea2d57acbc3d398d0c8b2625746eaf04/src/include/liburing/io_uring.h#L96

the-moisrex avatar Sep 24 '23 07:09 the-moisrex

I don't think there is a conforming way to achieve what io-uring wants: a flexible array as a union member.

union foo {
    int bar[]; // error: flexible array member in union
};

We could wrap it in an anonymous struct:

union foo {
    struct {
        int bar[]; // error: flexible array member in a struct with no named members
    };
};

And then we could add an empty field before it (this is what the kernel's DECLARE_FLEX_ARRAY() macro does)

union foo {
    struct {
        struct { } __empty_bar; // error: struct has no members [-Werror=pedantic]
        int bar[];
    };
};

At this point I would just give up and add __extension__:

union foo {
    struct {
        __extension__ struct { } __empty_bar;
        int bar[];
    };
};

It works!

tavianator avatar Sep 27 '23 21:09 tavianator

This is an XY problem, where io_uring doesn't actually need to use flexible arrays. Both CQE and SQE have their fixed lengths: CQE is either 16 bits or 32 bits, and SQE is either 64 bits or 128 bits.

ghost avatar Jan 17 '24 05:01 ghost

In C++, this is a good place to use templates!

the-moisrex avatar Feb 23 '24 07:02 the-moisrex