summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@250bpm.com>2010-10-14 09:34:09 +0200
committerMartin Sustrik <sustrik@250bpm.com>2010-10-14 09:34:09 +0200
commitcafcdbbe2bc9c78899568bb6cb957b700e12c66b (patch)
treeeeb4c0db2e380b3fc9aec0cab47b49286000d098
parentb174ad2c45c746e56b41671a3d68ef496bba6b97 (diff)
Safety measure in zmq_msg_close implemented
zmq_msg_close now empties the message on zmq_msg_close, thus not leaving random data in the structure, that may be mistaken for a valid message. Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
-rw-r--r--src/zmq.cpp32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/zmq.cpp b/src/zmq.cpp
index 9a15b6b..7101f34 100644
--- a/src/zmq.cpp
+++ b/src/zmq.cpp
@@ -154,24 +154,30 @@ int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
int zmq_msg_close (zmq_msg_t *msg_)
{
// For VSMs and delimiters there are no resources to free.
- if (msg_->content == (zmq::msg_content_t*) ZMQ_DELIMITER ||
- msg_->content == (zmq::msg_content_t*) ZMQ_VSM)
- return 0;
+ if (msg_->content != (zmq::msg_content_t*) ZMQ_DELIMITER &&
+ msg_->content != (zmq::msg_content_t*) ZMQ_VSM) {
- // If the content is not shared, or if it is shared and the reference.
- // count has dropped to zero, deallocate it.
- zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
- if (!(msg_->flags & ZMQ_MSG_SHARED) || !content->refcnt.sub (1)) {
+ // If the content is not shared, or if it is shared and the reference.
+ // count has dropped to zero, deallocate it.
+ zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
+ if (!(msg_->flags & ZMQ_MSG_SHARED) || !content->refcnt.sub (1)) {
- // We used "placement new" operator to initialize the reference.
- // counter so we call its destructor now.
- content->refcnt.~atomic_counter_t ();
+ // We used "placement new" operator to initialize the reference.
+ // counter so we call its destructor now.
+ content->refcnt.~atomic_counter_t ();
- if (content->ffn)
- content->ffn (content->data, content->hint);
- free (content);
+ if (content->ffn)
+ content->ffn (content->data, content->hint);
+ free (content);
+ }
}
+ // As a safety measure, let's make the deallocated message look like
+ // an empty message.
+ msg_->content = (zmq::msg_content_t*) ZMQ_VSM;
+ msg_->flags = 0;
+ msg_->vsm_size = 0;
+
return 0;
}