fix leaking memory in cert_dump_blob
authorPetr Štetiar <ynezz@true.cz>
Mon, 16 Dec 2019 13:49:40 +0000 (14:49 +0100)
committerPetr Štetiar <ynezz@true.cz>
Mon, 16 Dec 2019 19:52:58 +0000 (20:52 +0100)
Fixes following valgrind reported memory leak:

 189 bytes in 1 blocks are definitely lost in loss record 3 of 4
    at realloc
    by blobmsg_format_json_with_cb
    by blobmsg_format_json_indent
    by cert_dump_blob (ucert.c:386)
    by cert_dump (ucert.c:405)
    by main (ucert.c:728)

Signed-off-by: Petr Štetiar <ynezz@true.cz>
ucert.c

diff --git a/ucert.c b/ucert.c
index b9c5c88..76960a2 100644 (file)
--- a/ucert.c
+++ b/ucert.c
@@ -371,6 +371,7 @@ clean_and_return:
 /* dump single chain element to console */
 static void cert_dump_blob(struct blob_attr *cert[CERT_ATTR_MAX]) {
        int i;
+       char *json = NULL;
 
        for (i = 0; i < CERT_ATTR_MAX; i++) {
                struct blob_attr *v = cert[i];
@@ -383,7 +384,13 @@ static void cert_dump_blob(struct blob_attr *cert[CERT_ATTR_MAX]) {
                        fprintf(stdout, "signature:\n---\n%s---\n", (char *) blob_data(v));
                        break;
                case BLOB_ATTR_NESTED:
-                       fprintf(stdout, "payload:\n---\n%s\n---\n", blobmsg_format_json_indent(blob_data(v), false, 0));
+                       json = blobmsg_format_json_indent(blob_data(v), false, 0);
+                       if (!json) {
+                               DPRINTF("cannot parse payload\n");
+                               continue;
+                       }
+                       fprintf(stdout, "payload:\n---\n%s\n---\n", json);
+                       free(json);
                        break;
                }
        }