mirror of
https://github.com/NixOS/nix
synced 2025-06-30 07:33:16 +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:
parent
5b9cb8b372
commit
01bad63c72
8 changed files with 79 additions and 29 deletions
|
@ -56,7 +56,11 @@ void nix_store_free(Store * store)
|
|||
delete store;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
|
@ -67,7 +71,11 @@ nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callbac
|
|||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
|
@ -128,13 +136,15 @@ nix_err nix_store_realise(
|
|||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
std::string_view name = store_path->path.name();
|
||||
((nix_get_string_callback) callback)(name.data(), name.size(), user_data);
|
||||
}
|
||||
|
||||
|
||||
void nix_store_path_free(StorePath * sp)
|
||||
{
|
||||
delete sp;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue