1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 02:11:15 +02:00

C API: Safer function pointer casting

See https://github.com/NixOS/nix/pull/8699#discussion_r1554312181

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)()`
This commit is contained in:
José Luis Lafuente 2024-04-12 21:41:15 +02:00
parent 5b9cb8b372
commit 01bad63c72
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
8 changed files with 79 additions and 29 deletions

View file

@ -76,7 +76,11 @@ void nix_store_free(Store * store);
* @see nix_get_string_callback
* @return error code, NIX_OK on success.
*/
nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callback, void * user_data);
nix_err nix_store_get_uri(
nix_c_context * context,
Store * store,
void (*callback)(const char * start, unsigned int n, void * user_data),
void * user_data);
// returns: owned StorePath*
/**
@ -97,7 +101,10 @@ StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const c
* @param[in] callback called with the name
* @param[in] user_data arbitrary data, passed to the callback when it's called.
*/
void nix_store_path_name(const StorePath *store_path, void * callback, void * user_data);
void nix_store_path_name(
const StorePath * store_path,
void (*callback)(const char * start, unsigned int n, void * user_data),
void * user_data);
/**
* @brief Copy a StorePath
@ -130,7 +137,8 @@ bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath *
*
* Blocking, calls callback once for each realised output.
*
* @note When working with expressions, consider using e.g. nix_string_realise to get the output. `.drvPath` may not be accurate or available in the future. See https://github.com/NixOS/nix/issues/6507
* @note When working with expressions, consider using e.g. nix_string_realise to get the output. `.drvPath` may not be
* accurate or available in the future. See https://github.com/NixOS/nix/issues/6507
*
* @param[out] context Optional, stores error information
* @param[in] store Nix Store reference
@ -155,7 +163,11 @@ nix_err nix_store_realise(
* @see nix_get_string_callback
* @return error code, NIX_OK on success.
*/
nix_err nix_store_get_version(nix_c_context * context, Store * store, void * callback, void * user_data);
nix_err nix_store_get_version(
nix_c_context * context,
Store * store,
void (*callback)(const char * start, unsigned int n, void * user_data),
void * user_data);
// cffi end
#ifdef __cplusplus