[OpenWrt-Devel] [PATCH ucert 8/9] fix leaking memory in cert_dump_blob

Petr Štetiar ynezz at true.cz
Thu Dec 19 17:04:20 EST 2019


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 at true.cz>
---
 ucert.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/ucert.c b/ucert.c
index b9c5c889ddfa..76960a200be0 100644
--- 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;
 		}
 	}

_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list