Commit 676035fe authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Fix optional default initialization

Summary: Initialize Optional internal memory, so that "un-initialized memory" tools don't flag its callsites.

Reviewed By: yfeldblum

Differential Revision: D3960462

fbshipit-source-id: 3bd0109959fb93e040fa2e874f586b3508e46dd2
parent a30215d9
......@@ -67,17 +67,6 @@ typedef int detail::NoneHelper::*None;
const None none = nullptr;
/**
* gcc-4.7 warns about use of uninitialized memory around the use of storage_
* even though this is explicitly initialized at each point.
*/
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wuninitialized"
# pragma GCC diagnostic ignored "-Wpragmas"
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // __GNUC__
class OptionalEmptyException : public std::runtime_error {
public:
OptionalEmptyException()
......@@ -273,9 +262,18 @@ class Optional {
}
struct StorageTriviallyDestructible {
// uninitialized
union { Value value; };
// The union trick allows to initialize the Optional's memory,
// so that compiler/tools don't complain about unitialized memory,
// without actually calling Value's default constructor.
// The rest of the implementation enforces that hasValue/value are
// synchronized.
union {
bool hasValue;
struct {
bool paddingForHasValue_[1];
Value value;
};
};
StorageTriviallyDestructible() : hasValue{false} {}
......@@ -285,12 +283,16 @@ class Optional {
};
struct StorageNonTriviallyDestructible {
// uninitialized
union { Value value; };
// See StorageTriviallyDestructible's union
union {
bool hasValue;
struct {
bool paddingForHasValue_[1];
Value value;
};
};
StorageNonTriviallyDestructible() : hasValue{false} {}
~StorageNonTriviallyDestructible() {
clear();
}
......@@ -311,10 +313,6 @@ class Optional {
Storage storage_;
};
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
template<class T>
const T* get_pointer(const Optional<T>& opt) {
return opt.get_pointer();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment