mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 05:56:15 +02:00
Avoid casting function pointer in libutil test support
Casting function pointers seems to be almost always UB. See https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type Fixed by doing the casting of `void*` to `std::string*` inside the function instead. Caught by UBSan.
This commit is contained in:
parent
9c6678da0e
commit
5b6a21acc5
2 changed files with 5 additions and 5 deletions
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
namespace nix::testing {
|
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, void * user_data)
|
||||||
{
|
{
|
||||||
*user_data = std::string(start);
|
auto user_data_casted = reinterpret_cast<std::string *>(user_data);
|
||||||
|
*user_data_casted = std::string(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,13 @@
|
||||||
|
|
||||||
namespace nix::testing {
|
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, void * user_data);
|
||||||
|
|
||||||
inline void * observe_string_cb_data(std::string & out)
|
inline void * observe_string_cb_data(std::string & out)
|
||||||
{
|
{
|
||||||
return (void *) &out;
|
return (void *) &out;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define OBSERVE_STRING(str) \
|
#define OBSERVE_STRING(str) nix::testing::observe_string_cb, nix::testing::observe_string_cb_data(str)
|
||||||
(nix_get_string_callback) nix::testing::observe_string_cb, nix::testing::observe_string_cb_data(str)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue