Skip to content

Commit 716255b

Browse files
committed
Fix memory leak when json stringify throws exception
This was happening on invalid UTF-8 exception.
1 parent 73395b3 commit 716255b

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/json.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,13 @@ char *json_encode_string(const char *str)
399399
SB sb;
400400
sb_init(&sb);
401401

402-
emit_string(&sb, str);
402+
try {
403+
emit_string(&sb, str);
404+
}
405+
catch (std::exception &e) {
406+
sb_free(&sb);
407+
throw;
408+
}
403409

404410
return sb_finish(&sb);
405411
}
@@ -409,10 +415,16 @@ char *json_stringify(const JsonNode *node, const char *space)
409415
SB sb;
410416
sb_init(&sb);
411417

412-
if (space != NULL)
413-
emit_value_indented(&sb, node, space, 0);
414-
else
415-
emit_value(&sb, node);
418+
try {
419+
if (space != NULL)
420+
emit_value_indented(&sb, node, space, 0);
421+
else
422+
emit_value(&sb, node);
423+
}
424+
catch (std::exception &e) {
425+
sb_free(&sb);
426+
throw;
427+
}
416428

417429
return sb_finish(&sb);
418430
}

0 commit comments

Comments
 (0)