From 747e92e2c4a32429d56b0da106456b0ae8476d47 Mon Sep 17 00:00:00 2001
From: Pawel Kowalski
Date: Thu, 10 Aug 2017 07:58:20 +0200
Subject: [PATCH] Fixed ActionResultHolder for GCC 6
Compiling with GCC 6 lead to segfaults in unit tests written using
the gmock and gtest, because it invoked undefined behavior.
When optimizing, GCC assumes the 'this' pointer can never be null.
In the ActionResultHolder template, methods returned
null pointers and it caused segfaults. These null pointers were
replaced with pointers to dynamically created pointers to the
ActionResultHolder objects.
Source of patch:
https://github.com/google/googletest/issues/705
https://github.com/google/googletest/issues/705#issuecomment-235067917
Change-Id: Id617519921a0b6bdc3df01e417d762028ec132c7
---
include/gmock/gmock-spec-builders.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/include/gmock/gmock-spec-builders.h b/include/gmock/gmock-spec-builders.h
index 312fbe8..2dd733b 100644
--- a/include/gmock/gmock-spec-builders.h
+++ b/include/gmock/gmock-spec-builders.h
@@ -1370,6 +1370,8 @@ class ActionResultHolder : public UntypedActionResultHolderBase {
template <>
class ActionResultHolder : public UntypedActionResultHolderBase {
public:
+ explicit ActionResultHolder() {}
+
void GetValueAndDelete() const { delete this; }
virtual void PrintAsActionResult(::std::ostream* /* os */) const {}
@@ -1381,7 +1383,7 @@ class ActionResultHolder : public UntypedActionResultHolderBase {
const typename Function::ArgumentTuple& args,
const string& call_description) {
func_mocker->PerformDefaultAction(args, call_description);
- return NULL;
+ return new ActionResultHolder();
}
// Performs the given action and returns NULL.
@@ -1390,7 +1392,7 @@ class ActionResultHolder : public UntypedActionResultHolderBase {
const Action& action,
const typename Function::ArgumentTuple& args) {
action.Perform(args);
- return NULL;
+ return new ActionResultHolder();
}
};
--
2.7.4