diff mbox series

parse_json: check json_object_put frees the object

Message ID 20220605235955.1296104-1-dominique.martinet@atmark-techno.com
State Accepted
Delegated to: Stefano Babic
Headers show
Series parse_json: check json_object_put frees the object | expand

Commit Message

Dominique MARTINET June 5, 2022, 11:59 p.m. UTC
coverity detected that we always check json_object_put return value, so check
here as well.
In practice, wrong return value here means we've leaked some memory so output
a warning and continue.

Tested with a single-script json example

Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
---
 parser/parser.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Stefano Babic June 24, 2022, 9:37 a.m. UTC | #1
> coverity detected that we always check json_object_put return value, so check
> here as well.
> In practice, wrong return value here means we've leaked some memory so output
> a warning and continue.
> Tested with a single-script json example
> Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic
Stefano Babic June 24, 2022, 9:38 a.m. UTC | #2
On 24.06.22 11:37, sbabic@denx.de wrote:
>> coverity detected that we always check json_object_put return value, so check
>> here as well.
>> In practice, wrong return value here means we've leaked some memory so output
>> a warning and continue.
>> Tested with a single-script json example
>> Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
> Applied to u-boot-imx, master, thanks !
>                ^-------

Ok, I have to fix the script, of course it is not u-boot...

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/parser/parser.c b/parser/parser.c
index 49c531d32c19..d720de22a78c 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -995,6 +995,9 @@  int parse_cfg (struct swupdate_cfg __attribute__ ((__unused__)) *swcfg,
 #endif
 
 #ifdef CONFIG_JSON
+
+#define JSON_OBJECT_FREED 1
+
 int parse_json(struct swupdate_cfg *swcfg, const char *filename)
 {
 	int fd, ret;
@@ -1048,7 +1051,9 @@  int parse_json(struct swupdate_cfg *swcfg, const char *filename)
 
 	ret = parser(p, cfg, swcfg);
 
-	json_object_put(cfg);
+	if (json_object_put(cfg) != JSON_OBJECT_FREED) {
+		WARN("Leaking cfg json object");
+	}
 
 	free(string);