From 96c2dd5dfc7359936ca02ac5e18142caeb485a33 Mon Sep 17 00:00:00 2001 From: Brian Silverman Date: Wed, 20 Jan 2016 23:05:25 -0500 Subject: Avoid upcasting uninitialized pointers Fixes google/protobuf#693 msan flags this as being undefined behavior. I think it's triggering because the compiler has to insert a branch to avoid changing the pointer's value if it starts out NULL. I can't figure out if this is actually undefined behavior or not, but it definitely seems to be a gray area of the standard which is best avoided. --- src/google/protobuf/stubs/statusor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/google/protobuf/stubs/statusor.h') diff --git a/src/google/protobuf/stubs/statusor.h b/src/google/protobuf/stubs/statusor.h index a9d2b374..ad848701 100644 --- a/src/google/protobuf/stubs/statusor.h +++ b/src/google/protobuf/stubs/statusor.h @@ -224,14 +224,14 @@ inline StatusOr& StatusOr::operator=(const StatusOr& other) { template template inline StatusOr::StatusOr(const StatusOr& other) - : status_(other.status_), value_(other.value_) { + : status_(other.status_), value_(other.status_.ok() ? other.value_ : NULL) { } template template inline StatusOr& StatusOr::operator=(const StatusOr& other) { status_ = other.status_; - value_ = other.value_; + if (status_.ok()) value_ = other.value_; return *this; } -- cgit v1.2.3