mirror of
https://github.com/NixOS/nix
synced 2025-06-26 15:51: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:
parent
5b9cb8b372
commit
01bad63c72
8 changed files with 79 additions and 29 deletions
|
@ -64,7 +64,11 @@ const char * nix_version_get()
|
|||
|
||||
// Implementations
|
||||
|
||||
nix_err nix_setting_get(nix_c_context * context, const char * key, void * callback, void * user_data)
|
||||
nix_err nix_setting_get(
|
||||
nix_c_context * context,
|
||||
const char * key,
|
||||
void (*callback)(const char * start, unsigned int n, void * user_data),
|
||||
void * user_data)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
|
@ -115,7 +119,11 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_con
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data)
|
||||
nix_err nix_err_name(
|
||||
nix_c_context * context,
|
||||
const nix_c_context * read_context,
|
||||
void (*callback)(const char * start, unsigned int n, void * user_data),
|
||||
void * user_data)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
|
@ -125,7 +133,11 @@ nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context
|
|||
return call_nix_get_string_callback(read_context->name, callback, user_data);
|
||||
}
|
||||
|
||||
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, void * callback, void * user_data)
|
||||
nix_err nix_err_info_msg(
|
||||
nix_c_context * context,
|
||||
const nix_c_context * read_context,
|
||||
void (*callback)(const char * start, unsigned int n, void * user_data),
|
||||
void * user_data)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
|
@ -141,7 +153,8 @@ nix_err nix_err_code(const nix_c_context * read_context)
|
|||
}
|
||||
|
||||
// internal
|
||||
nix_err call_nix_get_string_callback(const std::string str, void * callback, void * user_data)
|
||||
nix_err call_nix_get_string_callback(
|
||||
const std::string str, void (*callback)(const char * start, unsigned int n, void * user_data), void * user_data)
|
||||
{
|
||||
((nix_get_string_callback) callback)(str.c_str(), str.size(), user_data);
|
||||
return NIX_OK;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue