Skip to content

Commit

Permalink
UB: reinterpret_cast pointer without using launder
Browse files Browse the repository at this point in the history
Signed-off-by: wineway <[email protected]>
  • Loading branch information
wineway committed Dec 6, 2023
1 parent 068d5ee commit e40943c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/leveldb/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
#endif // defined(DeleteFile)
#endif // defined(_WIN32)

#ifdef __cpp_lib_launder
#define LAUNDER(x) std::launder((x))
#else
#define LAUNDER(x) x
#endif

namespace leveldb {

class FileLock;
Expand Down
2 changes: 1 addition & 1 deletion util/env_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ class SingletonEnv {
SingletonEnv(const SingletonEnv&) = delete;
SingletonEnv& operator=(const SingletonEnv&) = delete;

Env* env() { return reinterpret_cast<Env*>(&env_storage_); }
Env* env() { return LAUNDER(reinterpret_cast<Env*>(&env_storage_)); }

static void AssertEnvNotInitialized() {
#if !defined(NDEBUG)
Expand Down
2 changes: 1 addition & 1 deletion util/env_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ class SingletonEnv {
SingletonEnv(const SingletonEnv&) = delete;
SingletonEnv& operator=(const SingletonEnv&) = delete;

Env* env() { return reinterpret_cast<Env*>(&env_storage_); }
Env* env() { return LAUNDER(reinterpret_cast<Env*>(&env_storage_)); }

static void AssertEnvNotInitialized() {
#if !defined(NDEBUG)
Expand Down

0 comments on commit e40943c

Please sign in to comment.