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
This commit is contained in:
Takatoshi Kondo
2020-07-03 09:18:00 +09:00
parent 3bb121fb8f
commit 3bd0172a9a
4 changed files with 17 additions and 4 deletions

View File

@@ -11,6 +11,7 @@
#define MSGPACK_FBUFFER_H
#include <stdio.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
@@ -25,6 +26,9 @@ extern "C" {
static inline int msgpack_fbuffer_write(void* data, const char* buf, size_t len)
{
assert(buf || len == 0);
if(!buf) return 0;
return (1 == fwrite(buf, len, 1, (FILE *)data)) ? 0 : -1;
}

View File

@@ -60,7 +60,9 @@ static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf)
static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
{
msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
assert(buf || len == 0);
if(!buf) return 0;
if(sbuf->alloc - sbuf->size < len) {
void* tmp;
@@ -83,10 +85,9 @@ static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len)
sbuf->alloc = nsize;
}
if(buf) {
memcpy(sbuf->data + sbuf->size, buf, len);
sbuf->size += len;
}
memcpy(sbuf->data + sbuf->size, buf, len);
sbuf->size += len;
return 0;
}

View File

@@ -12,6 +12,7 @@
#include "zone.h"
#include <stdlib.h>
#include <assert.h>
#if defined(unix) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
#include <sys/uio.h>
@@ -114,6 +115,9 @@ static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
{
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
assert(buf || len == 0);
if(!buf) return 0;
if(len < vbuf->ref_size) {
return msgpack_vrefbuffer_append_copy(vbuf, buf, len);

View File

@@ -13,6 +13,7 @@
#include "sysdep.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zlib.h>
#ifdef __cplusplus
@@ -121,6 +122,9 @@ static inline int msgpack_zbuffer_write(void* data, const char* buf, size_t len)
{
msgpack_zbuffer* zbuf = (msgpack_zbuffer*)data;
assert(buf || len == 0);
if(!buf) return 0;
zbuf->stream.next_in = (Bytef*)buf;
zbuf->stream.avail_in = (uInt)len;