From ef73412d5b131991f9cca1d7043ffabb093f3fd2 Mon Sep 17 00:00:00 2001 From: Fabrice Benhamouda Date: Mon, 5 Aug 2024 16:08:25 +0000 Subject: [PATCH 1/2] Apply the rule of 3/5/0 to JByteArrayCritical et al. Apply the [rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three) to JByteArrayCritical, SimpleBuffer, JBinaryBlob, JIOBlobs. This ensures that these objects cannot be copied. Before, if they were copied (e.g., due to being passed as argument to a function), the destructor would be called twice, which would release the critical region twice or which may release the critical region while another part of the code is still using it. --- csrc/buffer.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/csrc/buffer.h b/csrc/buffer.h index 14e6c633..afea8de1 100644 --- a/csrc/buffer.h +++ b/csrc/buffer.h @@ -598,6 +598,12 @@ class JByteArrayCritical { ~JByteArrayCritical(); unsigned char* get(); +#ifdef HAVE_CPP11 + // deleting copy constructor and copy assignment to satisfy rule of three + JByteArrayCritical(const JByteArrayCritical&) = delete; + JByteArrayCritical& operator=(const JByteArrayCritical&) = delete; +#endif + private: void* ptr_; JNIEnv* env_; @@ -610,6 +616,12 @@ class SimpleBuffer { ~SimpleBuffer(); uint8_t* get_buffer(); +#ifdef HAVE_CPP11 + // deleting copy constructor and copy assignment to satisfy rule of three + SimpleBuffer(const SimpleBuffer&) = delete; + SimpleBuffer& operator=(const SimpleBuffer&) = delete; +#endif + private: uint8_t* buffer_; }; @@ -623,6 +635,12 @@ class JBinaryBlob { ~JBinaryBlob(); uint8_t* get(); +#ifdef HAVE_CPP11 + // deleting copy constructor and copy assignment to satisfy rule of three + JBinaryBlob(const JBinaryBlob&) = delete; + JBinaryBlob& operator=(const JBinaryBlob&) = delete; +#endif + private: // The native pointer that is either backed by a direct ByteBuffer or a byte array. uint8_t* ptr_; @@ -649,6 +667,12 @@ class JIOBlobs { uint8_t* get_input(); uint8_t* get_output(); +#ifdef HAVE_CPP11 + // deleting copy constructor and copy assignment to satisfy rule of three + JIOBlobs(const JIOBlobs&) = delete; + JIOBlobs& operator=(const JIOBlobs&) = delete; +#endif + private: // The native pointers that are either backed by a direct ByteBuffer or a byte array. uint8_t* input_ptr_; From 27c5185d2b3e03beaac69f0db598d3f6eb9c690d Mon Sep 17 00:00:00 2001 From: Fabrice Benhamouda Date: Mon, 5 Aug 2024 19:06:46 +0000 Subject: [PATCH 2/2] Apply rule of 5: delete move operations in JByteArrayCritical et al --- csrc/buffer.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/csrc/buffer.h b/csrc/buffer.h index afea8de1..ef30e0d2 100644 --- a/csrc/buffer.h +++ b/csrc/buffer.h @@ -599,9 +599,11 @@ class JByteArrayCritical { unsigned char* get(); #ifdef HAVE_CPP11 - // deleting copy constructor and copy assignment to satisfy rule of three + // deleting copy & move operations to satisfy rule of five JByteArrayCritical(const JByteArrayCritical&) = delete; JByteArrayCritical& operator=(const JByteArrayCritical&) = delete; + JByteArrayCritical(JByteArrayCritical&&) = delete; + JByteArrayCritical& operator=(JByteArrayCritical&&) = delete; #endif private: @@ -617,9 +619,11 @@ class SimpleBuffer { uint8_t* get_buffer(); #ifdef HAVE_CPP11 - // deleting copy constructor and copy assignment to satisfy rule of three + // deleting copy & move operations to satisfy rule of five SimpleBuffer(const SimpleBuffer&) = delete; SimpleBuffer& operator=(const SimpleBuffer&) = delete; + SimpleBuffer(SimpleBuffer&&) = delete; + SimpleBuffer& operator=(SimpleBuffer&&) = delete; #endif private: @@ -636,9 +640,11 @@ class JBinaryBlob { uint8_t* get(); #ifdef HAVE_CPP11 - // deleting copy constructor and copy assignment to satisfy rule of three + // deleting copy & move operations to satisfy rule of five JBinaryBlob(const JBinaryBlob&) = delete; JBinaryBlob& operator=(const JBinaryBlob&) = delete; + JBinaryBlob(JBinaryBlob&&) = delete; + JBinaryBlob& operator=(JBinaryBlob&&) = delete; #endif private: @@ -668,9 +674,11 @@ class JIOBlobs { uint8_t* get_output(); #ifdef HAVE_CPP11 - // deleting copy constructor and copy assignment to satisfy rule of three + // deleting copy & move operations to satisfy rule of five JIOBlobs(const JIOBlobs&) = delete; JIOBlobs& operator=(const JIOBlobs&) = delete; + JIOBlobs(JIOBlobs&&) = delete; + JIOBlobs& operator=(JIOBlobs&&) = delete; #endif private: