axboe / liburing

Library providing helpers for the Linux kernel io_uring support
MIT License
2.86k stars 402 forks source link

Zero length arrays are a compiler specific thing! #952

Closed the-moisrex closed 3 weeks ago

the-moisrex commented 1 year ago

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

tavianator commented 1 year ago

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!

ghost commented 9 months ago

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.

the-moisrex commented 8 months ago

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