From: Maged Michael Date: Tue, 3 Oct 2017 14:56:57 +0000 (-0700) Subject: Fixes: prevent compiler reporting UB, hazptr_array move operator, empty array test X-Git-Tag: v2017.10.09.00~16 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=36b8f9c6be3f6f2a8f6bb111627b277ee3707b03;p=folly.git Fixes: prevent compiler reporting UB, hazptr_array move operator, empty array test Summary: Three fixes: (1) The compiler reports UB in line 432 for the case M > HAZPTR_TC_SIZE even though it is executed only if M <= HAZPTR_TC_SIZE. Added a condition M <= HAZPTR_TC_SIZE to help the compiler determine that line 432 is not executed in that case. (2) Added missing management of empty state in hazptr_array move operator (3) Added nullptr argument to empty hazptr_array in Array test Reviewed By: djwatson Differential Revision: D5951283 fbshipit-source-id: cb8e61421ab06c7733f67bf2d2274d3311260ac4 --- diff --git a/folly/experimental/hazptr/hazptr-impl.h b/folly/experimental/hazptr/hazptr-impl.h index 043d04a1..5250a5de 100644 --- a/folly/experimental/hazptr/hazptr-impl.h +++ b/folly/experimental/hazptr/hazptr-impl.h @@ -427,7 +427,7 @@ FOLLY_ALWAYS_INLINE hazptr_array::~hazptr_array() { if (LIKELY(ptc != nullptr)) { auto& tc = *ptc; auto count = tc.count(); - if (count + M <= HAZPTR_TC_SIZE) { + if ((M <= HAZPTR_TC_SIZE) && (count + M <= HAZPTR_TC_SIZE)) { for (size_t i = 0; i < M; ++i) { tc[count + i].hprec_ = h[i].hazptr_; DEBUG_PRINT(i << " " << &h[i]); @@ -455,6 +455,8 @@ FOLLY_ALWAYS_INLINE hazptr_array& hazptr_array::operator=( h[i] = std::move(other[i]); DEBUG_PRINT(i << " " << &h[i] << " " << &other[i]); } + empty_ = other.empty_; + other.empty_ = true; return *this; } diff --git a/folly/experimental/hazptr/test/HazptrTest.cpp b/folly/experimental/hazptr/test/HazptrTest.cpp index 8468ad57..75d4669a 100644 --- a/folly/experimental/hazptr/test/HazptrTest.cpp +++ b/folly/experimental/hazptr/test/HazptrTest.cpp @@ -360,7 +360,7 @@ TEST_F(HazptrTest, Array) { // Protect object hptr[9].reset(x); // Empty array - hazptr_array<10> h; + hazptr_array<10> h(nullptr); // Move assignment h = std::move(hptr); // Retire object