[PATCH 2/5] wpa_supplicant: fix possible memory leak

Ilan Peer ilan.peer
Wed Jun 17 06:16:33 PDT 2015


From: Eytan Lifshitz <eytan.lifshitz at intel.com>

In wpa_config_read(), if the configuration file can't be opened,
some memory allocated by wpa_config_alloc_empty() won't be freed.

Signed-off-by: Eytan Lifshitz <eytan.lifshitz at intel.com>
---
 wpa_supplicant/config_file.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/wpa_supplicant/config_file.c b/wpa_supplicant/config_file.c
index e946567..00078c0 100644
--- a/wpa_supplicant/config_file.c
+++ b/wpa_supplicant/config_file.c
@@ -372,6 +372,16 @@ struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
 
 	if (name == NULL)
 		return NULL;
+
+	wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
+	f = fopen(name, "r");
+	if (f == NULL) {
+		wpa_printf(MSG_ERROR,
+			   "Failed to open config file '%s', error: %s",
+			   name, strerror(errno));
+		return NULL;
+	}
+
 	if (cfgp)
 		config = cfgp;
 	else
@@ -379,6 +389,7 @@ struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
 	if (config == NULL) {
 		wpa_printf(MSG_ERROR, "Failed to allocate config file "
 			   "structure");
+		fclose(f);
 		return NULL;
 	}
 	tail = head = config->ssid;
@@ -388,15 +399,6 @@ struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
 	while (cred_tail && cred_tail->next)
 		cred_tail = cred_tail->next;
 
-	wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
-	f = fopen(name, "r");
-	if (f == NULL) {
-		wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
-			   "error: %s", name, strerror(errno));
-		os_free(config);
-		return NULL;
-	}
-
 	while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
 		if (os_strcmp(pos, "network={") == 0) {
 			ssid = wpa_config_read_network(f, &line, id++);
-- 
1.9.1




More information about the Hostap mailing list