Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

Compound literals are lvalues, and thus somewhat dangerous. Their address can be taken, and they can be assigned to.

We were using statement expressions to perform lvalue conversion on compound literals, transforming them to rvalues, and thus removing their dangers. However, statement expressions are non-standard, and quite complex within the compiler, so it would be interesting to use simpler compiler features to achieve the same.

The comma operator also performs lvalue conversion, and we can use a dummy (void)0 expression to introduce it. This is significantly simpler, and is more portable than the statement expression (it is valid all the way back to ANSI C89).

By using a simpler feature, we have a smaller risk of running into a compiler bug.

Suggested-by: @uecker
Cc: @chrisbazley
Cc: @kees
Cc: @flatcap

Compound literals are lvalues, and thus somewhat dangerous.  Their
address can be taken, and they can be assigned to.

We were using statement expressions to perform lvalue conversion
on compound literals, transforming them to rvalues, and thus removing
their dangers.  However, statement expressions are non-standard, and
quite complex within the compiler, so it would be interesting to use
simpler compiler features to achieve the same.

The comma operator also performs lvalue conversion, and we can use
a dummy (void)0 expression to introduce it.  This is significantly
simpler, and is more portable than the statement expression (it is valid
all the way back to ANSI C89).

By using a simpler feature, we have a smaller risk of running into
a compiler bug.

Suggested-by: Martin Uecker <[email protected]>
Cc: Christopher Bazley <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Richard Russon <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
@alejandro-colomar alejandro-colomar marked this pull request as ready for review January 10, 2026 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant