diff mbox series

[3/5] osm_port.c: osm_port_new fix memory leak

Message ID 20180827064233.3218-3-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): [#def9]
opensm-3.3.20/opensm/osm_port.c:146: alloc_fn: Storage is returned from allocation function "malloc".
opensm-3.3.20/opensm/osm_port.c:146: var_assign: Assigning: "p_port" = storage returned from "malloc(168UL)".
opensm-3.3.20/opensm/osm_port.c:150: noescape: Resource "p_port" is not freed or pointed-to in "memset". [Note: The source code implementation of the function has been overridden by a builtin model.]
opensm-3.3.20/opensm/osm_port.c:151: noescape: Resource "&p_port->mcm_list" is not freed or pointed-to in "cl_qlist_init".
opensm-3.3.20/include/complib/cl_qlist.h:435:56: noescape: "cl_qlist_init(cl_qlist_t * const)" does not free or save its parameter "p_list".
opensm-3.3.20/opensm/osm_port.c:166: leaked_storage: Variable "p_port" going out of scope leaks the storage it points to.
|#  164|   	p_physp = osm_node_get_physp_ptr(p_parent_node, port_num);
|#  165|   	if (!p_physp)
|#  166|-> 		return NULL;
|#  167|
|#  168|   	CL_ASSERT(port_guid == osm_physp_get_port_guid(p_physp));

Signed-off-by: Honggang Li <honli@redhat.com>
---
 opensm/osm_port.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): [#def9]
> opensm-3.3.20/opensm/osm_port.c:146: alloc_fn: Storage is returned from allocation function "malloc".
> opensm-3.3.20/opensm/osm_port.c:146: var_assign: Assigning: "p_port" = storage returned from "malloc(168UL)".
> opensm-3.3.20/opensm/osm_port.c:150: noescape: Resource "p_port" is not freed or pointed-to in "memset". [Note: The source code implementation of the function has been overridden by a builtin model.]
> opensm-3.3.20/opensm/osm_port.c:151: noescape: Resource "&p_port->mcm_list" is not freed or pointed-to in "cl_qlist_init".
> opensm-3.3.20/include/complib/cl_qlist.h:435:56: noescape: "cl_qlist_init(cl_qlist_t * const)" does not free or save its parameter "p_list".
> opensm-3.3.20/opensm/osm_port.c:166: leaked_storage: Variable "p_port" going out of scope leaks the storage it points to.
> |#  164|   	p_physp = osm_node_get_physp_ptr(p_parent_node, port_num);
> |#  165|   	if (!p_physp)
> |#  166|-> 		return NULL;
> |#  167|
> |#  168|   	CL_ASSERT(port_guid == osm_physp_get_port_guid(p_physp));
> 
> Signed-off-by: Honggang Li <honli@redhat.com>

Thanks. Applied.

-- Hal
diff mbox series

Patch

diff --git a/opensm/osm_port.c b/opensm/osm_port.c
index c732f990..1246edf9 100644
--- a/opensm/osm_port.c
+++ b/opensm/osm_port.c
@@ -162,8 +162,10 @@  osm_port_t *osm_port_new(IN const ib_node_info_t * p_ni,
 	   only the singular part that has this GUID is owned.
 	 */
 	p_physp = osm_node_get_physp_ptr(p_parent_node, port_num);
-	if (!p_physp)
+	if (!p_physp) {
+		free(p_port);
 		return NULL;
+	}
 
 	CL_ASSERT(port_guid == osm_physp_get_port_guid(p_physp));
 	p_port->p_physp = p_physp;