mirror of
https://github.com/NixOS/nix
synced 2025-07-08 19:23:54 +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
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue