diff mbox

[opensm] osm_subnet.c: Fix memory leak caused by commit dc0760cb8088fbe079e19682570a884ba01e94ff

Message ID 52136886.7090905@dev.mellanox.co.il (mailing list archive)
State Superseded
Delegated to: Hal Rosenstock
Headers show

Commit Message

Hal Rosenstock Aug. 20, 2013, 1 p.m. UTC
From: Vladimir Koushnir <vladimirk@mellanox.com>

double strdup for p_opt->dump_files_dir is causing memory leak

Signed-off-by: Vladimir Koushnir <vladimirk@mellanox.com>
Signed-off-by: Hal Rosenstock <hal@mellanox.com>
---
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Bart Van Assche Aug. 20, 2013, 1:13 p.m. UTC | #1
On 08/20/13 15:00, Hal Rosenstock wrote:
> From: Vladimir Koushnir <vladimirk@mellanox.com>
>
> double strdup for p_opt->dump_files_dir is causing memory leak
>
> Signed-off-by: Vladimir Koushnir <vladimirk@mellanox.com>
> Signed-off-by: Hal Rosenstock <hal@mellanox.com>
> ---
> diff --git a/opensm/osm_subnet.c b/opensm/osm_subnet.c
> index 7ab1671..4b5ef38 100644
> --- a/opensm/osm_subnet.c
> +++ b/opensm/osm_subnet.c
> @@ -1498,7 +1498,7 @@ void osm_subn_set_default_opt(IN osm_subn_opt_t *
> p_opt)
>    	p_opt->dump_files_dir = getenv("OSM_TMP_DIR");
>   	if (!p_opt->dump_files_dir || !(*p_opt->dump_files_dir))
> -		p_opt->dump_files_dir = strdup(OSM_DEFAULT_TMP_DIR);
> +		p_opt->dump_files_dir = (char *) OSM_DEFAULT_TMP_DIR;
>   	p_opt->dump_files_dir = strdup(p_opt->dump_files_dir);
>   	p_opt->log_file = strdup(OSM_DEFAULT_LOG_FILE);
>   	p_opt->log_max_size = 0;

How about avoiding the memory leak via the construct below, which has 
the advantage that no cast is necessary ?

   	if (!p_opt->dump_files_dir || !(*p_opt->dump_files_dir))
		p_opt->dump_files_dir = strdup(OSM_DEFAULT_TMP_DIR);
	else
	   	p_opt->dump_files_dir = strdup(p_opt->dump_files_dir);

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/opensm/osm_subnet.c b/opensm/osm_subnet.c
index 7ab1671..4b5ef38 100644
--- a/opensm/osm_subnet.c
+++ b/opensm/osm_subnet.c
@@ -1498,7 +1498,7 @@  void osm_subn_set_default_opt(IN osm_subn_opt_t *
p_opt)
  	p_opt->dump_files_dir = getenv("OSM_TMP_DIR");
 	if (!p_opt->dump_files_dir || !(*p_opt->dump_files_dir))
-		p_opt->dump_files_dir = strdup(OSM_DEFAULT_TMP_DIR);
+		p_opt->dump_files_dir = (char *) OSM_DEFAULT_TMP_DIR;
 	p_opt->dump_files_dir = strdup(p_opt->dump_files_dir);
 	p_opt->log_file = strdup(OSM_DEFAULT_LOG_FILE);
 	p_opt->log_max_size = 0;