diff --git a/src/TNL/Pointers/SharedPointerCuda.h b/src/TNL/Pointers/SharedPointerCuda.h index 1cee1d3900c7cd59544b4a4d7df156e3b30a00c0..b948cb0dd1f51ee62e58993b732282c3241a515e 100644 --- a/src/TNL/Pointers/SharedPointerCuda.h +++ b/src/TNL/Pointers/SharedPointerCuda.h @@ -142,21 +142,25 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer const Object* operator->() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return &this->pd->data; } Object* operator->() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return &this->pd->data; } const Object& operator *() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } Object& operator *() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } @@ -176,6 +180,7 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer __cuda_callable__ const Object& getData() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } @@ -183,6 +188,7 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer __cuda_callable__ Object& modifyData() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } @@ -203,7 +209,7 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer { this->free(); this->pd = (PointerData*) ptr.pd; - if( this->pd != nullptr ) + if( this->pd != nullptr ) this->pd->counter += 1; return *this; } @@ -399,22 +405,26 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer const Object* operator->() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return &this->pd->data; } Object* operator->() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); this->pd->maybe_modified = true; return &this->pd->data; } const Object& operator *() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } Object& operator *() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); this->pd->maybe_modified = true; return this->pd->data; } @@ -466,7 +476,8 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer this->free(); this->pd = (PointerData*) ptr.pd; this->cuda_pointer = ptr.cuda_pointer; - this->pd->counter += 1; + if( this->pd != nullptr ) + this->pd->counter += 1; #ifdef TNL_DEBUG_SHARED_POINTERS std::cerr << "Copy-assigned shared pointer: counter = " << this->pd->counter << ", type: " << demangle(typeid(ObjectType).name()) << std::endl; #endif @@ -481,7 +492,8 @@ class SharedPointer< Object, Devices::Cuda > : public SmartPointer this->free(); this->pd = (PointerData*) ptr.pd; this->cuda_pointer = ptr.cuda_pointer; - this->pd->counter += 1; + if( this->pd != nullptr ) + this->pd->counter += 1; #ifdef TNL_DEBUG_SHARED_POINTERS std::cerr << "Copy-assigned shared pointer: counter = " << this->pd->counter << ", type: " << demangle(typeid(ObjectType).name()) << std::endl; #endif diff --git a/src/TNL/Pointers/SharedPointerHost.h b/src/TNL/Pointers/SharedPointerHost.h index b33799539ac07c073ecaf99399a8b45b1c7ae6f7..ecafefcea4f73f8318b9e1e0ee0480b413d09bfd 100644 --- a/src/TNL/Pointers/SharedPointerHost.h +++ b/src/TNL/Pointers/SharedPointerHost.h @@ -118,21 +118,25 @@ class SharedPointer< Object, Devices::Host > : public SmartPointer const Object* operator->() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return &this->pd->data; } Object* operator->() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return &this->pd->data; } const Object& operator *() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } Object& operator *() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } @@ -152,6 +156,7 @@ class SharedPointer< Object, Devices::Host > : public SmartPointer __cuda_callable__ const Object& getData() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } @@ -159,6 +164,7 @@ class SharedPointer< Object, Devices::Host > : public SmartPointer __cuda_callable__ Object& modifyData() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } diff --git a/src/TNL/Pointers/SharedPointerMic.h b/src/TNL/Pointers/SharedPointerMic.h index c42e8b991b5e0e862f47dca4f2d8352f83b9c3b8..39727534f71bb9516ce5f99ae2cb32325165fd08 100644 --- a/src/TNL/Pointers/SharedPointerMic.h +++ b/src/TNL/Pointers/SharedPointerMic.h @@ -126,17 +126,20 @@ class SharedPointer< Object, Devices::MIC > : public SmartPointer const Object* operator->() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return &this->pd->data; } Object* operator->() { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); this->pd->maybe_modified = true; return &this->pd->data; } const Object& operator *() const { + TNL_ASSERT( this->pd != nullptr, "Attempt of dereferencing of null pointer" ); return this->pd->data; } diff --git a/src/UnitTests/Pointers/SharedPointerCudaTest.cu b/src/UnitTests/Pointers/SharedPointerCudaTest.cu index 06c40db0cf992050e251beb366238def206e71f7..76241e28fc5889aec8fa92c251f29190c18509c4 100644 --- a/src/UnitTests/Pointers/SharedPointerCudaTest.cu +++ b/src/UnitTests/Pointers/SharedPointerCudaTest.cu @@ -107,6 +107,20 @@ TEST( SharedPointerCudaTest, getDataArrayTest ) #endif }; +TEST( SharedPointerCudaTest, nullptrAssignement ) +{ +#ifdef HAVE_CUDA + using TestType = Pointers::SharedPointer< double, Devices::Cuda >; + TestType p1( 5 ), p2( nullptr ); + + // This should not crash + p1 = p2; + + ASSERT_FALSE( p1 ); + ASSERT_FALSE( p2 ); +#endif +} + #endif diff --git a/src/UnitTests/Pointers/SharedPointerHostTest.cpp b/src/UnitTests/Pointers/SharedPointerHostTest.cpp index db9cec42a9e5997d9654041ecd0cfb9e03c4617f..49fe6531df026697fa0babe9d14bd2237945c9ec 100644 --- a/src/UnitTests/Pointers/SharedPointerHostTest.cpp +++ b/src/UnitTests/Pointers/SharedPointerHostTest.cpp @@ -38,6 +38,19 @@ TEST( SharedPointerHostTest, ConstructorTest ) ASSERT_EQ( ptr1->x(), 1 ); ASSERT_EQ( ptr1->y(), 2 ); }; + +TEST( SharedPointerCudaTest, nullptrAssignement ) +{ + using TestType = Pointers::SharedPointer< double, Devices::Host >; + TestType p1( 5 ), p2( nullptr ); + + // This should not crash + p1 = p2; + + ASSERT_FALSE( p1 ); + ASSERT_FALSE( p2 ); +} + #endif #include "../GtestMissingError.h"