Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 90c9428

Browse files
committed
self_relative_ptr: fix UB when adding non-zero offset to possibly null pointer
Cast 'this' to integer type instead of casting to pointer type. Original implementation resulted in the following error for self_relative_ptr_atomic test: "runtime error: pointer index expression with base 0x7ffc7773e7c0 overflowed to 0xfffffffffffffffe"
1 parent 2153423 commit 90c9428

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

include/libpmemobj++/detail/self_relative_ptr_base_impl.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2020, Intel Corporation */
2+
/* Copyright 2020-2021, Intel Corporation */
33

44
/**
55
* @file
@@ -222,9 +222,8 @@ class self_relative_ptr_base_impl {
222222
*/
223223
uintptr_t mask = other_offset == nullptr_offset;
224224
--mask;
225-
uintptr_t ptr = reinterpret_cast<uintptr_t>(
226-
reinterpret_cast<const_byte_ptr_type>(this) +
227-
other_offset + 1);
225+
uintptr_t ptr = static_cast<uintptr_t>(
226+
reinterpret_cast<intptr_t>(this) + other_offset + 1);
228227
ptr &= mask;
229228
return reinterpret_cast<void *>(ptr);
230229
}

0 commit comments

Comments
 (0)