From 80a4698a7fe544806401a5efa253de1abd256c94 Mon Sep 17 00:00:00 2001 From: Jean-Michel Picod Date: Fri, 6 Jan 2023 13:03:10 +0100 Subject: [PATCH] Initial rework on TLV to avoid mem leaks --- client/src/emv/tlv.c | 73 ++++++++++++++++++++++++++++++++++++-------- client/src/emv/tlv.h | 19 +++++++++++- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/client/src/emv/tlv.c b/client/src/emv/tlv.c index f22bfea4c..67fa51b58 100644 --- a/client/src/emv/tlv.c +++ b/client/src/emv/tlv.c @@ -44,19 +44,6 @@ // const typeof( ((type *)0)->member ) *__mptr = (ptr); // (type *)( (char *)__mptr - offsetof(type,member) );}) -struct tlvdb { - struct tlv tag; - struct tlvdb *next; - struct tlvdb *parent; - struct tlvdb *children; -}; - -struct tlvdb_root { - struct tlvdb db; - size_t len; - unsigned char buf[0]; -}; - static tlv_tag_t tlv_parse_tag(const unsigned char **buf, size_t *len) { tlv_tag_t tag; @@ -212,6 +199,7 @@ struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len) { err: tlvdb_free(&root->db); + free(root); return NULL; } @@ -248,9 +236,53 @@ struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len) { err: tlvdb_free(&root->db); + free(root); return NULL; } +bool tlvdb_parse_root(struct tlvdb_root *root) { + if (root == NULL || root->len == 0) { + return false; + } + + const uint8_t *tmp; + size_t left; + + tmp = root->buf; + left = root->len; + if (tlvdb_parse_one(&root->db, NULL, &tmp, &left) == true) { + if (left == 0) { + return true; + } + } + return false; +} + +bool tlvdb_parse_root_multi(struct tlvdb_root *root) { + if (root == NULL || root->len == 0) { + return false; + } + + const uint8_t *tmp; + size_t left; + + tmp = root->buf; + left = root->len; + if (tlvdb_parse_one(&root->db, NULL, &tmp, &left) == true) { + while (left > 0) { + struct tlvdb *db = calloc(1, sizeof(*db)); + if (tlvdb_parse_one(db, NULL, &tmp, &left) == true) { + tlvdb_add(&root->db, db); + } else { + free(db); + return false; + } + } + return true; + } + return false; +} + struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value) { struct tlvdb_root *root = calloc(1, sizeof(*root) + len); @@ -291,6 +323,21 @@ void tlvdb_free(struct tlvdb *tlvdb) { } } +void tlvdb_root_free(struct tlvdb_root *root) { + if (root == NULL) { + return; + } + if (root->db.children) { + tlvdb_free(root->db.children); + root->db.children = NULL; + } + if (root->db.next) { + tlvdb_free(root->db.next); + root->db.next = NULL; + } + free(root); +} + struct tlvdb *tlvdb_find_next(struct tlvdb *tlvdb, tlv_tag_t tag) { if (!tlvdb) return NULL; diff --git a/client/src/emv/tlv.h b/client/src/emv/tlv.h index 50d046659..39f1bcacd 100644 --- a/client/src/emv/tlv.h +++ b/client/src/emv/tlv.h @@ -31,14 +31,31 @@ struct tlv { const unsigned char *value; }; -struct tlvdb; +struct tlvdb { + struct tlv tag; + struct tlvdb *next; + struct tlvdb *parent; + struct tlvdb *children; +}; + +struct tlvdb_root { + struct tlvdb db; + size_t len; + unsigned char buf[0]; +}; + typedef void (*tlv_cb)(void *data, const struct tlv *tlv, int level, bool is_leaf); struct tlvdb *tlvdb_fixed(tlv_tag_t tag, size_t len, const unsigned char *value); struct tlvdb *tlvdb_external(tlv_tag_t tag, size_t len, const unsigned char *value); struct tlvdb *tlvdb_parse(const unsigned char *buf, size_t len); struct tlvdb *tlvdb_parse_multi(const unsigned char *buf, size_t len); + +bool tlvdb_parse_root(struct tlvdb_root *root); +bool tlvdb_parse_root_multi(struct tlvdb_root *root); + void tlvdb_free(struct tlvdb *tlvdb); +void tlvdb_root_free(struct tlvdb_root *root); struct tlvdb *tlvdb_elm_get_next(struct tlvdb *tlvdb); struct tlvdb *tlvdb_elm_get_children(struct tlvdb *tlvdb);