Skip to content

Commit 3bd0172

Browse files
committed
Unified NULL buf handling for all *buffer.
*buffer means sbuffer, zbuffer, fbuffer, and vrefbuffer. The logic is as follows: if buf is NULL if len is 0 do nothing return 0 (success) else assertion fail else set contants to *buffer
1 parent 3bb121f commit 3bd0172

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

include/msgpack/fbuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define MSGPACK_FBUFFER_H
1212

1313
#include <stdio.h>
14+
#include <assert.h>
1415

1516
#ifdef __cplusplus
1617
extern "C" {
@@ -25,6 +26,9 @@ extern "C" {
2526

2627
static inline int msgpack_fbuffer_write(void* data, const char* buf, size_t len)
2728
{
29+
assert(buf || len == 0);
30+
if(!buf) return 0;
31+
2832
return (1 == fwrite(buf, len, 1, (FILE *)data)) ? 0 : -1;
2933
}
3034

include/msgpack/sbuffer.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf)
6060
static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
6161
{
6262
msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
63+
6364
assert(buf || len == 0);
65+
if(!buf) return 0;
6466

6567
if(sbuf->alloc - sbuf->size < len) {
6668
void* tmp;
@@ -83,10 +85,9 @@ static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
8385
sbuf->alloc = nsize;
8486
}
8587

86-
if(buf) {
87-
memcpy(sbuf->data + sbuf->size, buf, len);
88-
sbuf->size += len;
89-
}
88+
memcpy(sbuf->data + sbuf->size, buf, len);
89+
sbuf->size += len;
90+
9091
return 0;
9192
}
9293

include/msgpack/vrefbuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "zone.h"
1414
#include <stdlib.h>
15+
#include <assert.h>
1516

1617
#if defined(unix) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
1718
#include <sys/uio.h>
@@ -114,6 +115,9 @@ static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
114115
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
115116
{
116117
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
118+
assert(buf || len == 0);
119+
120+
if(!buf) return 0;
117121

118122
if(len < vbuf->ref_size) {
119123
return msgpack_vrefbuffer_append_copy(vbuf, buf, len);

include/msgpack/zbuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "sysdep.h"
1414
#include <stdlib.h>
1515
#include <string.h>
16+
#include <assert.h>
1617
#include <zlib.h>
1718

1819
#ifdef __cplusplus
@@ -121,6 +122,9 @@ static inline int msgpack_zbuffer_write(void* data, const char* buf, size_t len)
121122
{
122123
msgpack_zbuffer* zbuf = (msgpack_zbuffer*)data;
123124

125+
assert(buf || len == 0);
126+
if(!buf) return 0;
127+
124128
zbuf->stream.next_in = (Bytef*)buf;
125129
zbuf->stream.avail_in = (uInt)len;
126130

0 commit comments

Comments
 (0)