From b44293b115bbe69033ab9c066d301e1f7aaf64cf Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Mon, 21 May 2018 14:32:34 +0800 Subject: buffer_append: support NULL argument s to just allocate the space --- src/protocol/internal/buffer.c | 3 ++- src/protocol/internal/buffer.h | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/protocol/internal/buffer.c b/src/protocol/internal/buffer.c index f75d33e..dc53781 100644 --- a/src/protocol/internal/buffer.c +++ b/src/protocol/internal/buffer.c @@ -51,7 +51,8 @@ static int ensure(buffer *b, size_t len) int buffer_append(buffer *b, const uint8_t *s, size_t len) { if (ensure(b, len)) { - memcpy(b->p + b->offs + b->sz, s, len); + if (s != NULL) + memcpy(b->p + b->offs + b->sz, s, len); b->sz += len; return 1; } else { diff --git a/src/protocol/internal/buffer.h b/src/protocol/internal/buffer.h index 0e0b635..0cfdca6 100644 --- a/src/protocol/internal/buffer.h +++ b/src/protocol/internal/buffer.h @@ -44,6 +44,9 @@ static inline void buffer_clear(buffer *b) { b->offs = b->sz = 0; } static inline void buffer_deinit(buffer *b) { free(b->p); } static inline uint8_t *buffer_data(buffer *b) { return b->p + b->offs; } static inline size_t buffer_len(buffer *b) { return b->sz; } +/* buffer_append: append data in s to buffer b, + * if s is NULL, then just allocate len bytes of space in b + */ int buffer_append(buffer *b, const uint8_t *s, size_t len); int buffer_append_string(buffer *b, const char *s, size_t len); int buffer_append_byte(buffer *b, uint8_t); -- cgit v1.2.3