Skip to content

Zero-Length Array Bounds Errors Under -Os Optimisation #32

@GaryPartis

Description

@GaryPartis

Hello,

Firstly, I would like to say that TinyFrame is an excellent library; it has proven very useful in our project and is greatly appreciated.

I would like to report an issue that arises when the following configuration is used:

    // Frame ID listeners (wait for response / multi-part message)
    #define TF_MAX_ID_LST   0
    // Frame Type listeners (wait for frame with a specific first payload byte)
    #define TF_MAX_TYPE_LST 20
    // Generic listeners (fallback if no other listener catches it)
    #define TF_MAX_GEN_LST  0

With this configuration, zero-length arrays are declared and subsequently referenced. When compiling with GCC at optimisation level -Os, this produces errors of the following form:

    xxxx/tinyframe-2.3.0/tinyframe.c:1066:32: error: array subscript 254 is outside
    the bounds of an interior zero-length array 'struct TF_IdListener_[0]'
    [-Werror=zero-length-bounds]
    1066 |         lst = &tf->id_listeners[i];
         |                ~~~~~~~~~~~~~~~~^~~

Whilst it is possible to suppress this diagnostic via -Wno-zero-length-bounds, a more robust solution would be to guard the affected code with conditional compilation directives, ensuring that array accesses are only compiled when the corresponding listener count is non-zero.

For example:

#if TF_MAX_ID_LST > 0
    /* code referencing id_listeners */
#endif

This approach would eliminate the warnings without requiring callers to modify their compiler flags.

Please let me know if any further information would be helpful.

Kind regards,
Gary (& Claude 😁 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions