Blob Blame History Raw
From a98f2f8f2f1c14646ec9c80faecf14e9bf4bbd2c Mon Sep 17 00:00:00 2001
From: Petr Lautrbach <plautrba@redhat.com>
Date: Thu, 5 Aug 2021 16:26:44 +0200
Subject: [PATCH] mcstrans: fix RESOURCE_LEAK (CWE-772)

Fixes:
    Error: RESOURCE_LEAK (CWE-772): [#def1]
    mcstrans-3.2/src/mcstrans.c:1527: alloc_fn: Storage is returned from allocation function "compute_trans_from_raw".
    mcstrans-3.2/src/mcstrans.c:1527: var_assign: Assigning: "trans" = storage returned from "compute_trans_from_raw(range, domain)".
    mcstrans-3.2/src/mcstrans.c:1529: noescape: Resource "trans" is not freed or pointed-to in "add_cache".
    mcstrans-3.2/src/mcstrans.c:1515: overwrite_var: Overwriting "trans" in "trans = find_in_hashtable(range, domain, domain->raw_to_trans)" leaks the storage that "trans" points to.
    # 1513|   	domain_t *domain = domains;
    # 1514|   	for (;domain; domain = domain->next) {
    # 1515|-> 		trans = find_in_hashtable(range, domain, domain->raw_to_trans);
    # 1516|   		if (trans) break;
    # 1517|

    Error: RESOURCE_LEAK (CWE-772): [#def2]
    mcstrans-3.2/src/mcstrans.c:1654: alloc_fn: Storage is returned from allocation function "compute_raw_from_trans".
    mcstrans-3.2/src/mcstrans.c:1654: var_assign: Assigning: "raw" = storage returned from "compute_raw_from_trans(range, domain)".
    mcstrans-3.2/src/mcstrans.c:1656: noescape: Resource "raw" is not freed or pointed-to in "find_in_hashtable".
    mcstrans-3.2/src/mcstrans.c:1669: noescape: Resource "raw" is not freed or pointed-to in "add_cache".
    mcstrans-3.2/src/mcstrans.c:1642: overwrite_var: Overwriting "raw" in "raw = find_in_hashtable(range, domain, domain->trans_to_raw)" leaks the storage that "raw" points to.
    # 1640|   	domain_t *domain = domains;
    # 1641|   	for (;domain; domain = domain->next) {
    # 1642|-> 		raw = find_in_hashtable(range, domain, domain->trans_to_raw);
    # 1643|   		if (raw) break;
    # 1644|

Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
---
 mcstrans/src/mcstrans.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mcstrans/src/mcstrans.c b/mcstrans/src/mcstrans.c
index 8678418a1570..4e110e02f73a 100644
--- a/mcstrans/src/mcstrans.c
+++ b/mcstrans/src/mcstrans.c
@@ -1598,6 +1598,10 @@ trans_context(const char *incon, char **rcon) {
 		}
 		if (dashp)
 			*dashp = '-';
+		if (trans) {
+			free(trans);
+			trans = NULL;
+		}
 	}
 
 	if (trans) {
@@ -1769,6 +1773,10 @@ untrans_context(const char *incon, char **rcon) {
 		}
 		if (dashp)
 			*dashp = '-';
+		if (raw) {
+			free(raw);
+			raw = NULL;
+		}
 	}
 
 	if (raw) {
-- 
2.32.0