Skip to content

Commit 96443f2

Browse files
committed
_msgpack_buffer_add_new_chunk zero-out the newly allocated tail
Fix: #342 Reseting the memory in _msgpack_buffer_alloc_new_chunk was pointless because the previous `tail` is immediately copied into it, and it's the `tail` that is then used by the caller. So it's the `tail` we should zero-out.
1 parent 96b21a4 commit 96443f2

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ext/msgpack/buffer.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ static inline msgpack_buffer_chunk_t* _msgpack_buffer_alloc_new_chunk(msgpack_bu
257257
} else {
258258
chunk = xmalloc(sizeof(msgpack_buffer_chunk_t));
259259
}
260-
memset(chunk, 0, sizeof(msgpack_buffer_chunk_t));
261260
return chunk;
262261
}
263262

264263
static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
265264
{
266265
if(b->head == &b->tail) {
267266
if(b->tail.first == NULL) {
268-
/* empty buffer */
267+
/* The buffer is empty, we can just use the embeded tail directly */
268+
memset(&b->tail, 0, sizeof(msgpack_buffer_chunk_t));
269269
return;
270270
}
271271

@@ -295,6 +295,7 @@ static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
295295
before_tail->next = nc;
296296
nc->next = &b->tail;
297297
}
298+
memset(&b->tail, 0, sizeof(msgpack_buffer_chunk_t));
298299
}
299300

300301
static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE string)
@@ -315,7 +316,6 @@ static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE s
315316
b->tail.first = (char*) data;
316317
b->tail.last = (char*) data + length;
317318
b->tail.mapped_string = mapped_string;
318-
b->tail.mem = NULL;
319319

320320
/* msgpack_buffer_writable_size should return 0 for mapped chunk */
321321
b->tail_buffer_end = b->tail.last;
@@ -344,6 +344,8 @@ static inline void* _msgpack_buffer_chunk_malloc(
344344
msgpack_buffer_t* b, msgpack_buffer_chunk_t* c,
345345
size_t required_size, size_t* allocated_size)
346346
{
347+
c->mapped_string = NO_MAPPED_STRING;
348+
347349
if(required_size <= MSGPACK_RMEM_PAGE_SIZE) {
348350
c->rmem = true;
349351

0 commit comments

Comments
 (0)