Skip to content

Commit

Permalink
Apply the rule of 3/5/0 to JByteArrayCritical et al.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Fabrice Benhamouda committed Aug 5, 2024
1 parent cbe3441 commit ef73412
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions csrc/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -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_;
};
Expand All @@ -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_;
Expand All @@ -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_;
Expand Down

0 comments on commit ef73412

Please sign in to comment.