Skip to content

Commit fd9e377

Browse files
committed
check null pointer before using memcpy()
1 parent 82ddfc1 commit fd9e377

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

include/msgpack/sbuffer.h

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

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

1617
#ifdef __cplusplus
1718
extern "C" {
@@ -59,6 +60,7 @@ static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf)
5960
static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
6061
{
6162
msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
63+
assert(buf || len == 0);
6264

6365
if(sbuf->alloc - sbuf->size < len) {
6466
void* tmp;
@@ -81,8 +83,10 @@ static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
8183
sbuf->alloc = nsize;
8284
}
8385

84-
memcpy(sbuf->data + sbuf->size, buf, len);
85-
sbuf->size += len;
86+
if(buf) {
87+
memcpy(sbuf->data + sbuf->size, buf, len);
88+
sbuf->size += len;
89+
}
8690
return 0;
8791
}
8892

0 commit comments

Comments
 (0)