diff mbox series

osm_console.c: fix resource leak in dump_portguid_parse

Message ID 20180830092300.10931-1-honli@redhat.com (mailing list archive)
State Not Applicable
Delegated to: Hal Rosenstock
Headers show
Series osm_console.c: fix resource leak in dump_portguid_parse | expand

Commit Message

Honggang LI Aug. 30, 2018, 9:23 a.m. UTC
From: Honggang Li <honli@redhat.com>

Issue was found by Coverity.

To observer this resource leak, just input this command in the console:

OpenSM $ dump_portguid file /tmp/c file /tmp/d .

then:

ls -l /proc/<opensm-pid>/fd

You will find /tmp/c was not closed.

Error: RESOURCE_LEAK (CWE-772): [#def5]
opensm-3.3.20/opensm/osm_console.c:1682: alloc_fn: Storage is returned from allocation function "fopen".
opensm-3.3.20/opensm/osm_console.c:1682: var_assign: Assigning: "output" = storage returned from "fopen(p_cmd, "w+")".
opensm-3.3.20/opensm/osm_console.c:1682: overwrite_var: Overwriting "output" in "output = fopen(p_cmd, "w+")" leaks the storage that "output" points to.
|# 1680|   			p_cmd = next_token(p_last);
|# 1681|   			if (p_cmd) {
|# 1682|-> 				output = fopen(p_cmd, "w+");
|# 1683|   				if (output == NULL) {
|# 1684|   					fprintf(out,

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

Comments

Hal Rosenstock Aug. 30, 2018, 1:26 p.m. UTC | #1
On 8/30/2018 5:23 AM, Honggang LI wrote:
> From: Honggang Li <honli@redhat.com>
> 
> Issue was found by Coverity.
> 
> To observer this resource leak, just input this command in the console:
> 
> OpenSM $ dump_portguid file /tmp/c file /tmp/d .
> 
> then:
> 
> ls -l /proc/<opensm-pid>/fd
> 
> You will find /tmp/c was not closed.
> 
> Error: RESOURCE_LEAK (CWE-772): [#def5]
> opensm-3.3.20/opensm/osm_console.c:1682: alloc_fn: Storage is returned from allocation function "fopen".
> opensm-3.3.20/opensm/osm_console.c:1682: var_assign: Assigning: "output" = storage returned from "fopen(p_cmd, "w+")".
> opensm-3.3.20/opensm/osm_console.c:1682: overwrite_var: Overwriting "output" in "output = fopen(p_cmd, "w+")" leaks the storage that "output" points to.
> |# 1680|   			p_cmd = next_token(p_last);
> |# 1681|   			if (p_cmd) {
> |# 1682|-> 				output = fopen(p_cmd, "w+");
> |# 1683|   				if (output == NULL) {
> |# 1684|   					fprintf(out,
> 
> Signed-off-by: Honggang Li <honli@redhat.com>

Thanks. Applied.

-- Hal
diff mbox series

Patch

diff --git a/opensm/osm_console.c b/opensm/osm_console.c
index 9b91a42a..641bdcd5 100644
--- a/opensm/osm_console.c
+++ b/opensm/osm_console.c
@@ -1679,6 +1679,8 @@  static void dump_portguid_parse(char **p_last, osm_opensm_t * p_osm, FILE * out)
 		if (strcmp(p_cmd, "file") == 0) {
 			p_cmd = next_token(p_last);
 			if (p_cmd) {
+				if (output != out)
+					fclose(output);
 				output = fopen(p_cmd, "w+");
 				if (output == NULL) {
 					fprintf(out,