Skip to content

C API: Safer function pointer casting#10486

Merged
edolstra merged 3 commits intoNixOS:masterfrom
tweag:jl/c-api_function-pointer
Apr 16, 2024
Merged

C API: Safer function pointer casting#10486
edolstra merged 3 commits intoNixOS:masterfrom
tweag:jl/c-api_function-pointer

Conversation

@jlesquembre
Copy link
Member

Motivation

See #8699 (comment)

Casting a function pointer to void* is undefined behavior in the C spec, since there are platforms with different sizes for these two kinds of pointers. A safe alternative might be void (*callback)()

Context

Stabilize the C interface introduced in #8699

Milestone: https://github.com/NixOS/nix/milestone/52

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

See NixOS#8699 (comment)

Casting a function pointer to `void*` is undefined behavior in the C
spec, since there are platforms with different sizes for these two kinds
of pointers. A safe alternative might be `void (*callback)()`
Comment on lines 18 to 20
# include "gc/gc.h"
# define GC_INCLUDE_NEW 1
# include "gc_cpp.h"
#include "gc/gc.h"
#define GC_INCLUDE_NEW 1
#include "gc_cpp.h"
Copy link
Member

Choose a reason for hiding this comment

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

Please don't do this, the indentation between ifdef and endif helps legibility.

Copy link
Member

Choose a reason for hiding this comment

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

Did clang-format with our settings do this by any chance?

Copy link
Member

Choose a reason for hiding this comment

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

If so, #10493 will ensure it won't anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, that was caused by cland-format settings (my editor auto-formats it on save)
I'm going to revert it

@Ericson2314
Copy link
Member

void (const char * start, unsigned int n, void * user_data) seems worthy of a typedef?

@jlesquembre
Copy link
Member Author

void (const char * start, unsigned int n, void * user_data) seems worthy of a typedef?

right, and we already had it but I totally missed it 🤦

typedef void (*nix_get_string_callback)(const char * start, unsigned int n, void * user_data);

Updated to use the typedef

@edolstra edolstra merged commit 74e4bc9 into NixOS:master Apr 16, 2024
@jlesquembre jlesquembre deleted the jl/c-api_function-pointer branch April 16, 2024 17:34
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.

3 participants