From 29070c8f41c354e032e763a2eb01079ce221fb69 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Mon, 20 Nov 2023 09:51:08 -0500 Subject: [PATCH] Fix memory leak in code generated by `j2s` code. Closes #17. --- CHANGELOG.md | 2 ++ tools/j2s.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b7881f..3bfd31c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ releases. Final change log entries are published as [Releases](releases). - Added an option to `j2s` to allow additional fields in structures and ignore them in encoding and decoding. Note that additional fields are totally untouched—they are not even initialized to a default value. +- Fixed a memory leak that would occur in code generated by `j2s` under + specific circumstances. - Added `JsonMerge()` to the JSON API to merge two JSON objects together. ## v0.4.0 diff --git a/tools/j2s.c b/tools/j2s.c index 674d6d3..57a8b6e 100644 --- a/tools/j2s.c +++ b/tools/j2s.c @@ -660,11 +660,15 @@ Main(Array * args) StreamPrintf(implFile, " }\n\n"); if (StrEquals(fieldType, "array")) { - StreamPrintf(implFile, " out->%s = JsonValueAsArray(JsonValueDuplicate(val));\n", key); + StreamPrintf(implFile, " val = JsonValueDuplicate(val);\n"); + StreamPrintf(implFile, " out->%s = JsonValueAsArray(val);\n", key); + StreamPrintf(implFile, " Free(val); /* Not JsonValueFree() because we want the inner value. */\n"); } else if (StrEquals(fieldType, "object")) { - StreamPrintf(implFile, " out->%s = JsonValueAsObject(JsonValueDuplicate(val));\n", key); + StreamPrintf(implFile, " val = JsonValueDuplicate(val);\n"); + StreamPrintf(implFile, " out->%s = JsonValueAsObject(val);\n", key); + StreamPrintf(implFile, " Free(val); /* Not JsonValueFree() because we want the inner value. */\n"); } else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']') {