Skip to content

lib/: Use the comma operator to perform lvalue conversion#1491

Open
alejandro-colomar wants to merge 1 commit intoshadow-maint:masterfrom
alejandro-colomar:void0
Open

lib/: Use the comma operator to perform lvalue conversion#1491
alejandro-colomar wants to merge 1 commit intoshadow-maint:masterfrom
alejandro-colomar:void0

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Jan 10, 2026

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


Revisions:

v1b
  • Rebase
$ git rd 
1:  9558736a5b42 = 1:  5732c14f453e lib/: Use the comma operator to perform lvalue conversion
v1c
  • Rebase
$ git rd 
1:  5732c14f = 1:  8956e27a lib/: Use the comma operator to perform lvalue conversion
v1d
  • Rebase
$ git rd 
1:  8956e27a = 1:  5a03c618 lib/: Use the comma operator to perform lvalue conversion
v1e
  • Clarify portability.
$ git rd
1:  5a03c618 ! 1:  b7ab3f97 lib/: Use the comma operator to perform lvalue conversion
    @@ Commit message
     
         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).
    +    simpler, and is more portable than the statement expression: it is valid
    +    all the way back to C99 (the comma operator and the (void)0 expression
    +    are portable to C89, but the compound literal is from C99).
     
         By using a simpler feature, we have a smaller risk of running into
         a compiler bug.

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 C99 (the comma operator and the (void)0 expression
are portable to C89, but the compound literal is from C99).

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

Suggested-by: Martin Uecker <uecker@tugraz.at>
Cc: Christopher Bazley <chris.bazley@arm.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Richard Russon <rich@flatcap.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
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