nix-super/tests/unit/libutil-support/tests/string_callback.cc
Las Safin 5b6a21acc5
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.
2024-07-16 22:01:34 +00:00

11 lines
257 B
C++

#include "string_callback.hh"
namespace nix::testing {
void observe_string_cb(const char * start, unsigned int n, void * user_data)
{
auto user_data_casted = reinterpret_cast<std::string *>(user_data);
*user_data_casted = std::string(start);
}
}