Skip to content

Commit 154f891

Browse files
authored
Merge pull request #719 from WilhelmWiens/json_dumps_avoidDuplicatingString
Json dumps avoid duplicating string
2 parents ccd331e + 11a1284 commit 154f891

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/dump.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,15 @@ char *json_dumps(const json_t *json, size_t flags) {
431431

432432
if (json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
433433
result = NULL;
434-
else
435-
result = jsonp_strdup(strbuffer_value(&strbuff));
434+
else {
435+
char *new_result;
436+
result = strbuffer_steal_value(&strbuff);
437+
// technically the resizing is not needed.
438+
new_result = jsonp_realloc(result, strbuff.size, strbuff.length + 1);
439+
if (new_result) { // when realloc fails we just use the original pointer
440+
result = new_result;
441+
}
442+
}
436443

437444
strbuffer_close(&strbuff);
438445
return result;

src/jansson_private.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ void *jsonp_malloc(size_t size) JANSSON_ATTRS((warn_unused_result));
8686
void *jsonp_realloc(void *ptr, size_t originalSize, size_t newSize)
8787
JANSSON_ATTRS((warn_unused_result));
8888
void jsonp_free(void *ptr);
89-
char *jsonp_strndup(const char *str, size_t length) JANSSON_ATTRS((warn_unused_result));
90-
char *jsonp_strdup(const char *str) JANSSON_ATTRS((warn_unused_result));
9189
char *jsonp_strndup(const char *str, size_t len) JANSSON_ATTRS((warn_unused_result));
9290

9391
/* Circular reference check*/

src/memory.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ void *jsonp_realloc(void *ptr, size_t originalSize, size_t newSize) {
6161
}
6262
}
6363

64-
char *jsonp_strdup(const char *str) { return jsonp_strndup(str, strlen(str)); }
65-
6664
char *jsonp_strndup(const char *str, size_t len) {
6765
char *new_str;
6866

0 commit comments

Comments
 (0)