diff --git a/src/jansson-2.6/src/dump.c b/src/jansson-2.6/src/dump.c index 776f294d..f1c3bc26 100644 --- a/src/jansson-2.6/src/dump.c +++ b/src/jansson-2.6/src/dump.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2009-2013 Petri Lehtinen + * Copyright (c) 2015 Con Kolivas * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -427,13 +428,6 @@ static int do_dump(const json_t *json, size_t flags, int depth, } } -char *json_dump_dup(const char *str, size_t flags) -{ - if (flags & JSON_EOL) - return jsonp_eolstrdup(str); - return jsonp_strdup(str); -} - char *json_dumps(const json_t *json, size_t flags) { strbuffer_t strbuff; @@ -444,10 +438,11 @@ char *json_dumps(const json_t *json, size_t flags) if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) result = NULL; + else if (flags & JSON_EOL) + result = jsonp_eolstrsteal(&strbuff); else - result = json_dump_dup(strbuffer_value(&strbuff), flags); + result = jsonp_strsteal(&strbuff); - strbuffer_close(&strbuff); return result; } diff --git a/src/jansson-2.6/src/jansson_private.h b/src/jansson-2.6/src/jansson_private.h index 9de9c1eb..103f25bc 100644 --- a/src/jansson-2.6/src/jansson_private.h +++ b/src/jansson-2.6/src/jansson_private.h @@ -86,7 +86,8 @@ void _jsonp_free(void **ptr); char *jsonp_strndup(const char *str, size_t length); char *jsonp_strdup(const char *str); -char *jsonp_eolstrdup(const char *str); +char *jsonp_strsteal(strbuffer_t *strbuff); +char *jsonp_eolstrsteal(strbuffer_t *strbuff); /* Windows compatibility */ #ifdef _WIN32 diff --git a/src/jansson-2.6/src/memory.c b/src/jansson-2.6/src/memory.c index 5a00bafc..89c4671d 100644 --- a/src/jansson-2.6/src/memory.c +++ b/src/jansson-2.6/src/memory.c @@ -51,23 +51,22 @@ char *jsonp_strdup(const char *str) return new_str; } -char *jsonp_eolstrdup(const char *str) +char *jsonp_strsteal(strbuffer_t *strbuff) { - char *new_str; - size_t len; + size_t len = strbuff->length + 1; + char *ret = realloc(strbuff->value, len); - len = strlen(str); - if(len == (size_t)-1) - return NULL; + return ret; +} - new_str = jsonp_malloc(len + 2); - if(!new_str) - return NULL; +char *jsonp_eolstrsteal(strbuffer_t *strbuff) +{ + size_t len = strbuff->length + 2; + char *ret = realloc(strbuff->value, len); - memcpy(new_str, str, len); - new_str[len] = '\n'; - new_str[len + 1] = '\0'; - return new_str; + ret[strbuff->length] = '\n'; + ret[strbuff->length + 1] = '\0'; + return ret; } void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) diff --git a/src/jansson-2.6/src/strbuffer.c b/src/jansson-2.6/src/strbuffer.c index 86be2b38..fa26565f 100644 --- a/src/jansson-2.6/src/strbuffer.c +++ b/src/jansson-2.6/src/strbuffer.c @@ -16,7 +16,7 @@ #include "jansson_private.h" #include "strbuffer.h" -#define STRBUFFER_MIN_SIZE 16 +#define STRBUFFER_MIN_SIZE 4096 #define STRBUFFER_FACTOR 2 #define STRBUFFER_SIZE_MAX ((size_t)-1) @@ -74,7 +74,8 @@ int strbuffer_append_byte(strbuffer_t *strbuff, char byte) int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) { - if(size >= strbuff->size - strbuff->length) + /* Leave room for EOL and NULL bytes */ + if(size + 2 > strbuff->size - strbuff->length) { int backoff = 1; size_t new_size;