Skip to content

Commit

Permalink
Fix memory leak when json stringify throws exception
Browse files Browse the repository at this point in the history
This was happening on invalid UTF-8 exception.
  • Loading branch information
mgreter committed Oct 19, 2016
1 parent 73395b3 commit 716255b
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,13 @@ char *json_encode_string(const char *str)
SB sb;
sb_init(&sb);

emit_string(&sb, str);
try {
emit_string(&sb, str);
}
catch (std::exception &e) {
sb_free(&sb);
throw;
}

return sb_finish(&sb);
}
Expand All @@ -409,10 +415,16 @@ char *json_stringify(const JsonNode *node, const char *space)
SB sb;
sb_init(&sb);

if (space != NULL)
emit_value_indented(&sb, node, space, 0);
else
emit_value(&sb, node);
try {
if (space != NULL)
emit_value_indented(&sb, node, space, 0);
else
emit_value(&sb, node);
}
catch (std::exception &e) {
sb_free(&sb);
throw;
}

return sb_finish(&sb);
}
Expand Down

0 comments on commit 716255b

Please # to comment.