Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Aug 9, 2025

libc is allowed to provide any functions as macros (7.1.4p1 in C23).

This means that libc is allowed to provide a free(3) macro, which could look like

#define free(p)  __free(p)

And that would be expanded by the preprocessor in our code, turning our structure members into some code that won't work (or even worse, it might misbehave).

So, fix this undefined behavior.


Revisions:

v1b
  • Rebase
$ git rd 
1:  9bd4846f = 1:  40c6de86 lib/commonio.c: Parenthesize calls that look like libc APIs
v1c
  • Rebase
$ git rd 
1:  40c6de86 = 1:  cfd05116 lib/commonio.c: Parenthesize calls that look like libc APIs
v1d
  • Rebase
$ git rd 
1:  cfd05116 = 1:  190be125 lib/commonio.c: Parenthesize calls that look like libc APIs
v2
  • Rename members instead of adding parentheses. This is more conventional.
$ git range-diff shadow/master gh/macro macro 
1:  190be125 < -:  -------- lib/commonio.c: Parenthesize calls that look like libc APIs
-:  -------- > 1:  8e79275b lib/commonio.c: struct commonio_ops: Add prefix 'cio_' to structure members
v2b
  • Tweak commit message subject.
$ git range-diff shadow/master gh/macro macro 
1:  8e79275b ! 1:  f38b58dd lib/commonio.c: struct commonio_ops: Add prefix 'cio_' to structure members
    @@ Metadata
     Author: Alejandro Colomar <alx@kernel.org>
     
      ## Commit message ##
    -    lib/commonio.c: struct commonio_ops: Add prefix 'cio_' to structure members
    +    lib/commonio.[ch]: struct commonio_ops: Add prefix 'cio_' to structure members
     
         This structure has members that are named like libc APIs.
     
v2c
  • Rebase
$ git rd 
1:  f38b58dd = 1:  6b8a5d2a lib/commonio.[ch]: struct commonio_ops: Add prefix 'cio_' to structure members

@hallyn
Copy link
Member

hallyn commented Aug 9, 2025

Does any libc do this?

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Aug 10, 2025

Does any libc do this?

I'm experimenting with an _Optional qualifier that is being debated within the C Committee.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3422.pdf

I've installed the following header (written by the committee member that authored the proposal of _Optional) in my /usr/local/include/:
https://github.com/chrisbazley/CBUtilLib/blob/main/Optional.h

It wraps libc APIs to add the qualifier where appropriate. And it indeed does this by necessity.

Cc: @chrisbazley

@alejandro-colomar alejandro-colomar force-pushed the macro branch 2 times, most recently from 40c6de8 to cfd0511 Compare August 16, 2025 20:26
@alejandro-colomar alejandro-colomar added the Simpler A good issue for a new beginner label Aug 16, 2025
@alejandro-colomar alejandro-colomar marked this pull request as draft September 15, 2025 12:07
@alejandro-colomar alejandro-colomar marked this pull request as ready for review September 15, 2025 12:07
@alejandro-colomar
Copy link
Collaborator Author

@hallyn

How about changing the names of the structure members to be prefixed with io_ (or something else), similar to struct tm members being tm_*?

@alejandro-colomar alejandro-colomar force-pushed the macro branch 2 times, most recently from 190be12 to 8e79275 Compare September 25, 2025 13:42
@alejandro-colomar alejandro-colomar changed the title lib/commonio.c: Parenthesize calls that look like libc APIs lib/commonio.c: Rename structure members that look like libc APIs Sep 25, 2025
…e members

This structure has members that are named like libc APIs.

libc is allowed to provide any functions as macros (7.1.4p1 in C23).

This means that libc is allowed to provide a free(3) macro, which could
look like

	#define free(p)  __free(p)

And that would be expanded by the preprocessor in our code, turning our
structure members into some code that won't work (or even worse, it
might misbehave).

So, fix this undefined behavior.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
@hallyn hallyn merged commit db2a06e into shadow-maint:master Oct 18, 2025
11 checks passed
@alejandro-colomar alejandro-colomar deleted the macro branch October 19, 2025 09:07
@alejandro-colomar alejandro-colomar self-assigned this Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Simpler A good issue for a new beginner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants