From 98c8bfbc3ecfb022dc2d077031d4c3eee29d4e0b Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Tue, 19 Nov 2024 16:51:51 -0800 Subject: [PATCH] fix violations of warning maybe-uninitialized under GCC in Optional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Addresses the following warning violation reports. ``` In member function ‘void std::__shared_count<_Lp>::_M_swap(std::__shared_count<_Lp>&) [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’, inlined from ‘void std::__shared_ptr<_Tp, _Lp>::swap(std::__shared_ptr<_Tp, _Lp>&) [with _Tp = int; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/rh/gcc-toolset-13/root/usr/include/c++/13/bits/shared_ptr_base.h:1687:21, inlined from ‘std::__shared_ptr<_Tp, _Lp>& std::__shared_ptr<_Tp, _Lp>::operator=(std::__shared_ptr<_Tp, _Lp>&&) [with _Tp = int; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ at /opt/rh/gcc-toolset-13/root/usr/include/c++/13/bits/shared_ptr_base.h:1620:35, inlined from ‘std::shared_ptr<_Tp>& std::shared_ptr<_Tp>::operator=(std::shared_ptr<_Tp>&&) [with _Tp = int]’ at /opt/rh/gcc-toolset-13/root/usr/include/c++/13/bits/shared_ptr.h:440:36, inlined from ‘void folly::Optional::assign(Value&&) [with Value = std::shared_ptr]’ at /data/users/yfeldblum/scratch/dataZusersZyfeldblumZfbsource-s1/fbcode_builder_getdeps/shipit/folly/folly/Optional.h:254:22, inlined from ‘folly::Optional& folly::Optional::operator=(Arg&&) [with Arg = std::shared_ptr; Value = std::shared_ptr]’ at folly/Optional.h:276:11, inlined from ‘virtual void folly::Optional_Shared_Test::TestBody()’ at folly/test/OptionalTest.cpp:332:22: /opt/rh/gcc-toolset-13/root/usr/include/c++/13/bits/shared_ptr_base.h:1099:32: warning: ‘((std::__shared_count<__gnu_cxx::_S_atomic>*)((char*)&opt + offsetof(folly::Optional >,folly::Optional >::storage_.folly::Optional >::StorageNonTriviallyDestructible::)))[1].std::__shared_count<>::_M_pi’ may be used uninitialized [-Wmaybe-uninitialized] 1099 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; | ^~~~~ folly/test/OptionalTest.cpp: In member function ‘virtual void folly::Optional_Shared_Test::TestBody()’: folly/test/OptionalTest.cpp:315:29: note: ‘opt’ declared here 315 | Optional> opt; | ^~~ ``` ``` In member function ‘std::size_t folly::Function::exec(Op, Data*, Data*) const [with FunctionType = void()]’, inlined from ‘folly::Function::~Function() [with FunctionType = void()]’ at folly/Function.h:789:21, inlined from ‘void folly::Optional::StorageNonTriviallyDestructible::clear() [with Value = folly::Function]’ at folly/Optional.h:479:21, inlined from ‘folly::Optional::StorageNonTriviallyDestructible::~StorageNonTriviallyDestructible() [with Value = folly::Function]’ at folly/Optional.h:474:47, inlined from ‘folly::Optional >::~Optional()’ at folly/Optional.h:111:7, inlined from ‘folly::Optional folly::UnboundedQueue::tryDequeueUntilSC(Segment*, const std::chrono::time_point<_Clock, _Duration>&) [with Clock = std::chrono::_V2::steady_clock; Duration = std::chrono::duration >; T = folly::Function; bool SingleProducer = false; bool SingleConsumer = true; bool MayBlock = true; long unsigned int LgSegmentSize = 8; long unsigned int LgAlign = 7; Atom = std::atomic]’ at folly/concurrency/UnboundedQueue.h:468:3, inlined from ‘folly::Optional folly::UnboundedQueue::tryDequeueUntil(const std::chrono::time_point<_Clock, _Duration>&) [with Clock = std::chrono::_V2::steady_clock; Duration = std::chrono::duration >; T = folly::Function; bool SingleProducer = false; bool SingleConsumer = true; bool MayBlock = true; long unsigned int LgSegmentSize = 8; long unsigned int LgAlign = 7; Atom = std::atomic]’ at folly/concurrency/UnboundedQueue.h:439:43, inlined from ‘folly::Optional folly::UnboundedQueue::try_dequeue() [with T = folly::Function; bool SingleProducer = false; bool SingleConsumer = true; bool MayBlock = true; long unsigned int LgSegmentSize= 8; long unsigned int LgAlign = 7; Atom = std::atomic]’ at folly/concurrency/UnboundedQueue.h:299:72, inlined from ‘bool folly::UnboundedQueue::try_dequeue(T&) [with T = folly::Function; bool SingleProducer = false; bool SingleConsumer = true; bool MayBlock = true; long unsigned int LgSegmentSize = 8; long unsigned int LgAlign = 7; Atom = std::atomic]’ at folly/concurrency/UnboundedQueue.h:290:10, inlined from ‘size_t folly::TimedDrivableExecutor::run()’ at folly/executors/TimedDrivableExecutor.cpp:58:41: folly/Function.h:642:10: warning: ‘ret.folly::Optional >::storage_.folly::Optional >::StorageNonTriviallyDestructible::.folly::Optional >::StorageNonTriviallyDestructible::._anon_158::value.folly::Function ::exec_’ may be used uninitialized [-Wmaybe-uninitialized] 642 | if (!exec_) { | ^~~~~ folly/concurrency/UnboundedQueue.h: In member function ‘size_t folly::TimedDrivableExecutor::run()’: folly/concurrency/UnboundedQueue.h:463:24: note: ‘ret’ declared here 463 | folly::Optional ret = e.takeItem(); | ^~~ ``` Reviewed By: Gownta Differential Revision: D65738230 fbshipit-source-id: 714753de7ff8be89b2fb3446b1104c89be0f6763 --- folly/Optional.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/folly/Optional.h b/folly/Optional.h index 68ba53afb9d..8d7e5463094 100644 --- a/folly/Optional.h +++ b/folly/Optional.h @@ -251,7 +251,10 @@ class Optional { void assign(Value&& newValue) { if (hasValue()) { + FOLLY_PUSH_WARNING + FOLLY_GCC_DISABLE_WARNING("-Wmaybe-uninitialized") storage_.value = std::move(newValue); + FOLLY_POP_WARNING } else { construct(std::move(newValue)); } @@ -259,7 +262,10 @@ class Optional { void assign(const Value& newValue) { if (hasValue()) { + FOLLY_PUSH_WARNING + FOLLY_GCC_DISABLE_WARNING("-Wmaybe-uninitialized") storage_.value = newValue; + FOLLY_POP_WARNING } else { construct(newValue); } @@ -476,7 +482,10 @@ class Optional { void clear() { if (hasValue) { hasValue = false; + FOLLY_PUSH_WARNING + FOLLY_GCC_DISABLE_WARNING("-Wmaybe-uninitialized") value.~Value(); + FOLLY_POP_WARNING } } };