From 6d4928d585cc35fa7fa3d6599934b07599c9ca7a Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 16 Jul 2020 10:12:25 +0200
Subject: [PATCH] ENH: extend nullptr check for tmp ptr() method (#1775)

- Cannot call ptr_->clone() with a null pointer!
---
 src/OpenFOAM/memory/tmp/tmpI.H    | 15 ++++++++-------
 src/OpenFOAM/memory/tmp/tmpNrcI.H | 14 +++++++-------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H
index 53fa27f1a78..729a13fedbd 100644
--- a/src/OpenFOAM/memory/tmp/tmpI.H
+++ b/src/OpenFOAM/memory/tmp/tmpI.H
@@ -260,15 +260,16 @@ inline T& Foam::tmp<T>::constCast() const
 template<class T>
 inline T* Foam::tmp<T>::ptr() const
 {
+    if (!ptr_)
+    {
+        FatalErrorInFunction
+            << typeName() << " deallocated"
+            << abort(FatalError);
+    }
+
     if (isTmp())
     {
-        if (!ptr_)
-        {
-            FatalErrorInFunction
-                << typeName() << " deallocated"
-                << abort(FatalError);
-        }
-        else if (!ptr_->unique())
+        if (!ptr_->unique())
         {
             FatalErrorInFunction
                 << "Attempt to acquire pointer to object referred to"
diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H
index 19046f3c3f2..36ec5e8b8a1 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrcI.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H
@@ -224,15 +224,15 @@ inline T& Foam::tmpNrc<T>::constCast() const
 template<class T>
 inline T* Foam::tmpNrc<T>::ptr() const
 {
-    if (isTmp())
+    if (!ptr_)
     {
-        if (!ptr_)
-        {
-            FatalErrorInFunction
-                << typeName() << " deallocated"
-                << abort(FatalError);
-        }
+        FatalErrorInFunction
+            << typeName() << " deallocated"
+            << abort(FatalError);
+    }
 
+    if (isTmp())
+    {
         T* p = ptr_;
         ptr_ = nullptr;
 
-- 
GitLab