From 7053c3b3b35275a052b2411bf73c1c4f578a0322 Mon Sep 17 00:00:00 2001 From: Daryl McDaniel Date: Tue, 16 Feb 2016 10:47:30 -0800 Subject: [PATCH] StdLib/BsdSocketLib: Fix minor memory leak by freeing rrecp on error return. The error return is triggered by one of two conditions: 1. rrecp is NULL (calloc failed) 2. strdup(dname) returns NULL Previously, the function just returned NULL. This patch adds a call to free rrecp before returning NULL. Since the free() function will properly do nothing when called with a NULL parameter, it is not necessary to separate the two tests into separate if clauses. Reported-by: Colin King Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Daryl McDaniel Reviewed-by: Jaben Carsey --- StdLib/BsdSocketLib/res_mkupdate.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/StdLib/BsdSocketLib/res_mkupdate.c b/StdLib/BsdSocketLib/res_mkupdate.c index d81d7d6f1518..db8540ab4b85 100644 --- a/StdLib/BsdSocketLib/res_mkupdate.c +++ b/StdLib/BsdSocketLib/res_mkupdate.c @@ -438,8 +438,10 @@ res_mkupdrec(int section, const char *dname, u_int class, u_int type, u_long ttl) { ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec)); - if (!rrecp || !(rrecp->r_dname = strdup(dname))) + if (!rrecp || !(rrecp->r_dname = strdup(dname))) { + free(rrecp); return (NULL); + } rrecp->r_class = (u_int16_t)class; rrecp->r_type = (u_int16_t)type; rrecp->r_ttl = (u_int32_t)ttl;