Skip to contentSkip to conversation

powertop: fix various resource leaks

PATCH Superseded Mohammad Merajul Islam Molla 2 participants 4 messages Aug 4, 2014 – Aug 5, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Mohammad Merajul Islam Molla

author
#0 lore raw

PATCH 1/1 powertop: fix various resource leaks

fixes some resource leaks detected by valgrind and coverity scan.

Signed-off-by Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
---
 src/devices/ahci.cpp     | 4 +++-
 src/perf/perf_bundle.cpp | 4 +++-
 src/tuning/bluetooth.cpp | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
src/devices/ahci.cpp +3 -1
index 3b4627a..a4d1345 100644
@@ -67,8 +67,10 @@ static string disk_name(char *path, char *target, char *shortname)
 		sprintf(line, "%s/%s/model", pathname, dirent->d_name);
 		file = fopen(line, "r");
 		if (file) {
-			if (fgets(line, 4096, file) == NULL)
+			if (fgets(line, 4096, file) == NULL) {
+				fclose(file);
 				break;
+			}
 			fclose(file);
 			c = strchr(line, '\n');
 			if (c)
src/perf/perf_bundle.cpp +3 -1
index b0e982b..cf1ae11 100644
@@ -142,8 +142,10 @@ static void parse_event_format(const char *event_name)
 
 	buf = read_file(file);
 	free(file);
-	if (!buf)
+	if (!buf) {
+		free(name);
 		return;
+	}
 
 	pevent_parse_event(perf_event::pevent, buf, strlen(buf), sys);
 	free(name);
src/tuning/bluetooth.cpp +3 -1
index 92f5835..2c1e283 100644
@@ -144,8 +144,10 @@ int bt_tunable::good_bad(void)
 		if (file) {
 			char line[2048];
 			/* first line is standard header */
-			if (fgets(line, 2047, file) == NULL)
+			if (fgets(line, 2047, file) == NULL) {
+				pclose(file);
 				goto out;
+			}
 			memset(line, 0, 2048);
 			if (fgets(line, 2047, file) == NULL) {
 				result = last_check_result = TUNE_GOOD;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 54 lines · Mohammad Merajul Islam Molla

Date: Mon, 4 Aug 2014 19:50:57 +0600
From: Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
To: powertop(a)lists.01.org
Subject: [Powertop] [PATCH] powertop: fix various resource leaks
X-Mailer: git-send-email 1.9.1

fixes some resource leaks detected by valgrind and coverity scan.

Signed-off-by Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
---
 src/devices/ahci.cpp     | 4 +++-
 src/perf/perf_bundle.cpp | 4 +++-
 src/tuning/bluetooth.cpp | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
src/devices/ahci.cpp +3 -1
index 3b4627a..a4d1345 100644
@@ -67,8 +67,10 @@ static string disk_name(char *path, char *target, char *shortname)
 		sprintf(line, "%s/%s/model", pathname, dirent->d_name);
 		file = fopen(line, "r");
 		if (file) {
-			if (fgets(line, 4096, file) == NULL)
+			if (fgets(line, 4096, file) == NULL) {
+				fclose(file);
 				break;
+			}
 			fclose(file);
 			c = strchr(line, '\n');
 			if (c)
src/perf/perf_bundle.cpp +3 -1
index b0e982b..cf1ae11 100644
@@ -142,8 +142,10 @@ static void parse_event_format(const char *event_name)
 
 	buf = read_file(file);
 	free(file);
-	if (!buf)
+	if (!buf) {
+		free(name);
 		return;
+	}
 
 	pevent_parse_event(perf_event::pevent, buf, strlen(buf), sys);
 	free(name);
src/tuning/bluetooth.cpp +0 -0
index 92f5835..2c1e283 100644
@@ -144,8 +144,10 @@ int bt_tunable::good_bad(void)
 		if (file) {

src/tuning/bluetooth.cpp int bt_tunable::good_bad(void) · line 153

Quoted 10 lines
src/tuning/bluetooth.cpp +3 -1
 			char line[2048];
 			/* first line is standard header */
-			if (fgets(line, 2047, file) == NULL)
+			if (fgets(line, 2047, file) == NULL) {
+				pclose(file);
 				goto out;
+			}
 			memset(line, 0, 2048);
 			if (fgets(line, 2047, file) == NULL) {
 				result = last_check_result = TUNE_GOOD;

how about moving pclose(file) to `out:' label scope? 3 out of 11 lines are doing same work here: cleaning up resource before jump.

	-ss
author
#2 lore raw

Hi Sergey,

Thanks for your review.

For this portion of changes, is the below diff ok? I will resend the full patch if it is.

src/tuning/bluetooth.cpp +4 -6
index 2c1e283..9be327e 100644
@@ -108,7 +108,7 @@ static int last_check_result;
 int bt_tunable::good_bad(void)
 {
        struct hci_dev_info devinfo;
-       FILE *file;
+       FILE *file = 0;
        int fd;
        int thisbytes = 0;
        int ret;
@@ -144,18 +144,14 @@ int bt_tunable::good_bad(void)
                if (file) {
                        char line[2048];
                        /* first line is standard header */
-                       if (fgets(line, 2047, file) == NULL) {
-                               pclose(file);
+                       if (fgets(line, 2047, file) == NULL)
                                goto out;
-                       }
                        memset(line, 0, 2048);
                        if (fgets(line, 2047, file) == NULL) {
                                result = last_check_result = TUNE_GOOD;
-                               pclose(file);
                                goto out;
                        }
 
-                       pclose(file);
                        if (strlen(line) > 0) {
                                result = last_check_result = TUNE_GOOD;
                                goto out;
@@ -168,6 +164,8 @@ int bt_tunable::good_bad(void)
 
 out:
        previous_bytes = thisbytes;
+       if (file)
+               pclose(file);
        close(fd);
        return result;
 }

--
Thanks,
-Meraj

Quoted 70 lines · Sergey Senozhatsky
Quoted 54 lines · Mohammad Merajul Islam Molla

Date: Mon, 4 Aug 2014 19:50:57 +0600
From: Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
To: powertop(a)lists.01.org
Subject: [Powertop] [PATCH] powertop: fix various resource leaks
X-Mailer: git-send-email 1.9.1

fixes some resource leaks detected by valgrind and coverity scan.

Signed-off-by Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
---
 src/devices/ahci.cpp     | 4 +++-
 src/perf/perf_bundle.cpp | 4 +++-
 src/tuning/bluetooth.cpp | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
src/devices/ahci.cpp +3 -1
index 3b4627a..a4d1345 100644
@@ -67,8 +67,10 @@ static string disk_name(char *path, char *target, char *shortname)
              sprintf(line, "%s/%s/model", pathname, dirent->d_name);
              file = fopen(line, "r");
              if (file) {
-                     if (fgets(line, 4096, file) == NULL)
+                     if (fgets(line, 4096, file) == NULL) {
+                             fclose(file);
                              break;
+                     }
                      fclose(file);
                      c = strchr(line, '\n');
                      if (c)
src/perf/perf_bundle.cpp +3 -1
index b0e982b..cf1ae11 100644
@@ -142,8 +142,10 @@ static void parse_event_format(const char *event_name)
 
      buf = read_file(file);
      free(file);
-     if (!buf)
+     if (!buf) {
+             free(name);
              return;
+     }
 
      pevent_parse_event(perf_event::pevent, buf, strlen(buf), sys);
      free(name);
src/tuning/bluetooth.cpp +0 -0
index 92f5835..2c1e283 100644
@@ -144,8 +144,10 @@ int bt_tunable::good_bad(void)
              if (file) {

src/tuning/bluetooth.cpp int bt_tunable::good_bad(void) · line 153

Quoted 10 lines
src/tuning/bluetooth.cpp +3 -1
                      char line[2048];
                      /* first line is standard header */
-                     if (fgets(line, 2047, file) == NULL)
+                     if (fgets(line, 2047, file) == NULL) {
+                             pclose(file);
                              goto out;
+                     }
                      memset(line, 0, 2048);
                      if (fgets(line, 2047, file) == NULL) {
                              result = last_check_result = TUNE_GOOD;

how about moving pclose(file) to `out:' label scope? 3 out of 11 lines are doing same work here: cleaning up resource before jump.

        -ss
#3 lore raw
Quoted 7 lines · Mohammad Merajul Islam Molla

Hi Sergey,

Thanks for your review.

For this portion of changes, is the below diff ok? I will resend the full patch if it is.

looks good.

	-ss
Quoted 123 lines
src/tuning/bluetooth.cpp +4 -7
index 2c1e283..9be327e 100644
@@ -108,7 +108,7 @@ static int last_check_result;
 int bt_tunable::good_bad(void)
 {
        struct hci_dev_info devinfo;
-       FILE *file;
+       FILE *file = 0;
        int fd;
        int thisbytes = 0;
        int ret;
@@ -144,18 +144,14 @@ int bt_tunable::good_bad(void)
                if (file) {
                        char line[2048];
                        /* first line is standard header */
-                       if (fgets(line, 2047, file) == NULL) {
-                               pclose(file);
+                       if (fgets(line, 2047, file) == NULL)
                                goto out;
-                       }
                        memset(line, 0, 2048);
                        if (fgets(line, 2047, file) == NULL) {
                                result = last_check_result = TUNE_GOOD;
-                               pclose(file);
                                goto out;
                        }
 
-                       pclose(file);
                        if (strlen(line) > 0) {
                                result = last_check_result = TUNE_GOOD;
                                goto out;
@@ -168,6 +164,8 @@ int bt_tunable::good_bad(void)
 
 out:
        previous_bytes = thisbytes;
+       if (file)
+               pclose(file);
        close(fd);
        return result;
 }
 
 
--

Thanks,
-Meraj

Quoted 70 lines · Sergey Senozhatsky
Quoted 54 lines · Mohammad Merajul Islam Molla

Date: Mon, 4 Aug 2014 19:50:57 +0600
From: Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
To: powertop(a)lists.01.org
Subject: [Powertop] [PATCH] powertop: fix various resource leaks
X-Mailer: git-send-email 1.9.1

fixes some resource leaks detected by valgrind and coverity scan.

Signed-off-by Mohammad Merajul Islam Molla <meraj.enigma(a)gmail.com>
---
 src/devices/ahci.cpp     | 4 +++-
 src/perf/perf_bundle.cpp | 4 +++-
 src/tuning/bluetooth.cpp | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
src/devices/ahci.cpp +3 -1
index 3b4627a..a4d1345 100644
@@ -67,8 +67,10 @@ static string disk_name(char *path, char *target, char *shortname)
              sprintf(line, "%s/%s/model", pathname, dirent->d_name);
              file = fopen(line, "r");
              if (file) {
-                     if (fgets(line, 4096, file) == NULL)
+                     if (fgets(line, 4096, file) == NULL) {
+                             fclose(file);
                              break;
+                     }
                      fclose(file);
                      c = strchr(line, '\n');
                      if (c)
src/perf/perf_bundle.cpp +3 -1
index b0e982b..cf1ae11 100644
@@ -142,8 +142,10 @@ static void parse_event_format(const char *event_name)
 
      buf = read_file(file);
      free(file);
-     if (!buf)
+     if (!buf) {
+             free(name);
              return;
+     }
 
      pevent_parse_event(perf_event::pevent, buf, strlen(buf), sys);
      free(name);
src/tuning/bluetooth.cpp +0 -0
index 92f5835..2c1e283 100644
@@ -144,8 +144,10 @@ int bt_tunable::good_bad(void)
              if (file) {

src/tuning/bluetooth.cpp int bt_tunable::good_bad(void) · line 153

Quoted 10 lines
src/tuning/bluetooth.cpp +3 -1
                      char line[2048];
                      /* first line is standard header */
-                     if (fgets(line, 2047, file) == NULL)
+                     if (fgets(line, 2047, file) == NULL) {
+                             pclose(file);
                              goto out;
+                     }
                      memset(line, 0, 2048);
                      if (fgets(line, 2047, file) == NULL) {
                              result = last_check_result = TUNE_GOOD;

how about moving pclose(file) to `out:' label scope? 3 out of 11 lines are doing same work here: cleaning up resource before jump.

        -ss