Skip to content

[C++] Enhance functions in util/ubsan.h to support types that do not have a default constructor #48339

@andishgar

Description

@andishgar

Describe the enhancement requested

Currently, the functions in util/ubsan.h require a type that is trivially copyable. However, before performing its intended operation, it calls the default constructor of the type, as shown in the code below:

inline std::enable_if_t<std::is_trivially_copyable_v<T>, T> SafeLoadAs(
const uint8_t* unaligned) {
std::remove_const_t<T> ret;
std::memcpy(&ret, unaligned, sizeof(T));
return ret;
}

It is possible, however, for a type to be trivially copyable without having a default constructor, for example:

struct A {
  A(int a) : a(a) {}
  int a;
};

Component(s)

C++

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions