Skip to content

Commit

Permalink
C API: Safer function pointer casting
Browse files Browse the repository at this point in the history
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)()`
  • Loading branch information
jlesquembre committed Apr 12, 2024
1 parent 5b9cb8b commit 01bad63
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 29 deletions.
15 changes: 6 additions & 9 deletions src/libexpr-c/nix_api_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include "value/context.hh"

#ifdef HAVE_BOEHMGC
# 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"
#endif

// Helper function to throw an exception if value is null
Expand Down Expand Up @@ -537,7 +537,7 @@ nix_realised_string * nix_string_realise(nix_c_context * context, EvalState * st
if (context)
context->last_err_code = NIX_OK;
try {
auto &v = check_value_not_null(value);
auto & v = check_value_not_null(value);
nix::NixStringContext stringContext;
auto rawStr = state->state.coerceToString(nix::noPos, v, stringContext, "while realising a string").toOwned();
nix::StorePathSet storePaths;
Expand All @@ -547,14 +547,11 @@ nix_realised_string * nix_string_realise(nix_c_context * context, EvalState * st

// Convert to the C API StorePath type and convert to vector for index-based access
std::vector<StorePath> vec;
for (auto &sp : storePaths) {
for (auto & sp : storePaths) {
vec.push_back(StorePath{sp});
}

return new nix_realised_string {
.str = s,
.storePaths = vec
};
return new nix_realised_string{.str = s, .storePaths = vec};
}
NIXC_CATCH_ERRS_NULL
}
Expand Down
18 changes: 14 additions & 4 deletions src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions src/libstore-c/nix_api_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*
/**
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 17 additions & 4 deletions src/libutil-c/nix_api_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions src/libutil-c/nix_api_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ nix_err nix_libutil_init(nix_c_context * context);
* @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was retrieved
* successfully.
*/
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);

/**
* @brief Sets a setting in the nix global configuration.
Expand Down Expand Up @@ -241,8 +245,11 @@ const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, uns
* @see nix_get_string_callback
* @return NIX_OK if there were no errors, an error code otherwise.
*/
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);

/**
* @brief Retrieves the error name from a context.
Expand All @@ -260,7 +267,11 @@ nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, vo
* @see nix_get_string_callback
* @return NIX_OK if there were no errors, an error code otherwise.
*/
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);

/**
* @brief Retrieves the most recent error code from a nix_c_context
Expand Down
3 changes: 2 additions & 1 deletion src/libutil-c/nix_api_util_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ nix_err nix_context_error(nix_c_context * context);
* @return NIX_OK if there were no errors.
* @see nix_get_string_callback
*/
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);

#define NIXC_CATCH_ERRS \
catch (...) \
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/libutil-support/tests/string_callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace nix::testing {

void observe_string_cb(const char * start, unsigned int n, std::string * user_data) {
void observe_string_cb(const char * start, unsigned int n, std::string * user_data)
{
*user_data = std::string(start);
}

Expand Down
9 changes: 7 additions & 2 deletions tests/unit/libutil-support/tests/string_callback.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
namespace nix::testing {

void observe_string_cb(const char * start, unsigned int n, std::string * user_data);
inline void * observe_string_cb_data(std::string & out) {

inline void * observe_string_cb_data(std::string & out)
{
return (void *) &out;
};
#define OBSERVE_STRING(str) (void *)nix::testing::observe_string_cb, nix::testing::observe_string_cb_data(str)

#define OBSERVE_STRING(str) \
(void (*)(const char *, unsigned int, void *)) nix::testing::observe_string_cb, \
nix::testing::observe_string_cb_data(str)

}

0 comments on commit 01bad63

Please sign in to comment.