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

Commit 400005b

Browse files
Merge pull request #1113 from igchor/ubsan_1.6
[stable-1.6] Ubsan fixes
2 parents dc5348d + 2405ce2 commit 400005b

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

include/libpmemobj++/detail/persistent_ptr_base.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018, Intel Corporation
2+
* Copyright 2016-2021, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -372,7 +372,10 @@ class persistent_ptr_base {
372372
inline ptrdiff_t
373373
calculate_offset() const
374374
{
375-
static const ptrdiff_t ptr_offset_magic = 0xDEADBEEF;
375+
static const ptrdiff_t ptr_offset_magic = 0xF00000000000000;
376+
377+
static_assert(ptr_offset_magic % alignof(U) == 0, "");
378+
static_assert(ptr_offset_magic % alignof(T) == 0, "");
376379

377380
U *tmp{reinterpret_cast<U *>(ptr_offset_magic)};
378381
T *diff = static_cast<T *>(tmp);

include/libpmemobj++/experimental/contiguous_iterator.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020, Intel Corporation
2+
* Copyright 2018-2021, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -245,7 +245,7 @@ struct range_snapshotting_iterator
245245
{
246246
assert(data <= ptr);
247247

248-
if (snapshot_size > 0)
248+
if (snapshot_size && ptr)
249249
snapshot_range(ptr);
250250
}
251251

tests/ptr/ptr.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019, Intel Corporation
2+
* Copyright 2015-2021, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -344,6 +344,38 @@ test_offset(nvobj::pool<root> &pop)
344344
UT_ASSERT(0);
345345
}
346346
}
347+
348+
/*
349+
* test_offset_with_alignment -- test offset calculation within a hierarchy of
350+
* objects with different alignments
351+
*/
352+
void
353+
test_offset_alignment(nvobj::pool<root> &pop)
354+
{
355+
struct A {
356+
char a;
357+
};
358+
359+
struct B {
360+
uint64_t b;
361+
};
362+
363+
struct C : public A, public B {
364+
uint64_t c;
365+
};
366+
367+
try {
368+
nvobj::transaction::run(pop, [] {
369+
auto cptr = nvobj::make_persistent<C>();
370+
nvobj::persistent_ptr<B> bptr = cptr;
371+
UT_ASSERT((bptr.raw().off - cptr.raw().off) ==
372+
alignof(B));
373+
nvobj::delete_persistent<C>(cptr);
374+
});
375+
} catch (...) {
376+
UT_ASSERT(0);
377+
}
378+
}
347379
}
348380

349381
int
@@ -370,6 +402,7 @@ main(int argc, char *argv[])
370402
test_ptr_transactional(pop);
371403
test_ptr_array(pop);
372404
test_offset(pop);
405+
test_offset_alignment(pop);
373406

374407
pop.close();
375408

0 commit comments

Comments
 (0)