Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vla: Use proper C99 flexible array #81895

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

ceolin
Copy link
Member

@ceolin ceolin commented Nov 25, 2024

As part of upcoming hardening buffer checks on Zephyr we need to sanitize the usage of flexible array. This pr properly declare flexible arrays according to C99 standard, not relying on non-portable GNU extension.

0 length array is a GNU extension. Use proper flexible array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
@ceolin ceolin force-pushed the security/vla branch 2 times, most recently from 9179363 to 2a2a658 Compare November 27, 2024 06:57
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
0 length array is a GNU extension. Use proper C99 flexible
array.

Signed-off-by: Flavio Ceolin <[email protected]>
@jhedberg
Copy link
Member

Do I understand correctly that [] is the C99 standard way and [0] is the GNU extension that should be avoided, and that the standard way can't be used in some corner cases (as outlined in the documentation of FLEXIBLE_ARRAY_DECLARE()?

Since using the macro doesn't really improve readability (quite the opposite), I'm wondering, why don't we limit the macro usage only to the situations where the standard way can't directly be used, and use just [] everywhere else?

@@ -755,7 +755,7 @@ struct bt_hci_handle_count {
#define BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS BT_OP(BT_OGF_BASEBAND, 0x0035) /* 0x0c35 */
struct bt_hci_cp_host_num_completed_packets {
uint8_t num_handles;
struct bt_hci_handle_count h[0];
FLEXIBLE_ARRAY_DECLARE(struct bt_hci_handle_count, h);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

why not simply use foo[] instead of macro which adds some extra __unused_foo members under the hood?

also, this declares unpacked structures inside packed structure, are those guaranteed to not affect packed struct alignment or no padding requirements?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjanc this was also my worry (see my earlier comment). I'd prefer if the macro was only used in the corner cases where you can't directly use [].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

6 participants