diff mbox series

[5/5] osm_ucast_nue.c: fix resource leak in nue_create_context

Message ID 20180827064233.3218-5-honli@redhat.com (mailing list archive)
State Not Applicable
Delegated to: Hal Rosenstock
Headers show
Series [1/5] Adjust BuildRequires for covscan | expand

Commit Message

Honggang LI Aug. 27, 2018, 6:42 a.m. UTC
From: Honggang Li <honli@redhat.com>

Issue was found by Coverity.

Error: RESOURCE_LEAK (CWE-772):
opensm-3.3.20/opensm/osm_ucast_nue.c:3159: alloc_fn: Storage is returned from allocation function "malloc".
opensm-3.3.20/opensm/osm_ucast_nue.c:3159: var_assign: Assigning: "nue_ctx" = storage returned from "malloc(336UL)".
opensm-3.3.20/opensm/osm_ucast_nue.c:3164: noescape: Resource "nue_ctx" is not freed or pointed-to in "create_context".
opensm-3.3.20/opensm/osm_ucast_nue.c:3109:43: noescape: "create_context(nue_context_t *)" does not free or save its parameter "nue_ctx".
opensm-3.3.20/opensm/osm_ucast_nue.c:3166: leaked_storage: Variable "nue_ctx" going out of scope leaks the storage it points to.
|# 3164|   		err = create_context(nue_ctx);
|# 3165|   		if (err)
|# 3166|-> 			return NULL;
|# 3167|   	} else {
|# 3168|   		OSM_LOG(osm->sm.ucast_mgr.p_log, OSM_LOG_ERROR,

Signed-off-by: Honggang Li <honli@redhat.com>
---
 opensm/osm_ucast_nue.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Hal Rosenstock Aug. 27, 2018, 4:40 p.m. UTC | #1
On 8/27/2018 2:42 AM, Honggang LI wrote:
> From: Honggang Li <honli@redhat.com>
> 
> Issue was found by Coverity.
> 
> Error: RESOURCE_LEAK (CWE-772):
> opensm-3.3.20/opensm/osm_ucast_nue.c:3159: alloc_fn: Storage is returned from allocation function "malloc".
> opensm-3.3.20/opensm/osm_ucast_nue.c:3159: var_assign: Assigning: "nue_ctx" = storage returned from "malloc(336UL)".
> opensm-3.3.20/opensm/osm_ucast_nue.c:3164: noescape: Resource "nue_ctx" is not freed or pointed-to in "create_context".
> opensm-3.3.20/opensm/osm_ucast_nue.c:3109:43: noescape: "create_context(nue_context_t *)" does not free or save its parameter "nue_ctx".
> opensm-3.3.20/opensm/osm_ucast_nue.c:3166: leaked_storage: Variable "nue_ctx" going out of scope leaks the storage it points to.
> |# 3164|   		err = create_context(nue_ctx);
> |# 3165|   		if (err)
> |# 3166|-> 			return NULL;
> |# 3167|   	} else {
> |# 3168|   		OSM_LOG(osm->sm.ucast_mgr.p_log, OSM_LOG_ERROR,
> 
> Signed-off-by: Honggang Li <honli@redhat.com>

Thanks. Applied.

-- Hal
diff mbox series

Patch

diff --git a/opensm/osm_ucast_nue.c b/opensm/osm_ucast_nue.c
index 13684738..0ef84ebc 100644
--- a/opensm/osm_ucast_nue.c
+++ b/opensm/osm_ucast_nue.c
@@ -3162,8 +3162,10 @@  static nue_context_t *nue_create_context(const osm_opensm_t * osm,
 		nue_ctx->routing_type = routing_type;
 		nue_ctx->mgr = (osm_ucast_mgr_t *) & (osm->sm.ucast_mgr);
 		err = create_context(nue_ctx);
-		if (err)
+		if (err) {
+			free(nue_ctx);
 			return NULL;
+		}
 	} else {
 		OSM_LOG(osm->sm.ucast_mgr.p_log, OSM_LOG_ERROR,
 			"ERR NUE01: cannot allocate memory for nue_ctx\n");