verify_energy_info() opens multiple sysfs files for each attribute
but does not close them, leaking file descriptors during directory
iteration. The directory stream opened by opendir() is also never
closed.

Add a helper to check file contents and close each file after
reading. Check the return value of opendir() and ensure the directory
stream is closed on both success and failure paths.

Signed-off-by: zhangchujun <[email protected]>

diff --git a/tools/testing/selftests/powerpc/papr_attributes/attr_test.c 
b/tools/testing/selftests/powerpc/papr_attributes/attr_test.c
index 9b655be641c9..892aff137644 100644
--- a/tools/testing/selftests/powerpc/papr_attributes/attr_test.c
+++ b/tools/testing/selftests/powerpc/papr_attributes/attr_test.c
@@ -55,6 +55,21 @@ static int value_type(int id)
        return val_type;
 }
 
+static int check_file(const char *file_name)
+{
+       FILE *f;
+       int c;
+
+       f = fopen(file_name, "r");
+       FAIL_IF(!f);
+
+       c = fgetc(f);
+       fclose(f);
+       FAIL_IF(c == EOF);
+
+       return 0;
+}
+
 static int verify_energy_info(void)
 {
        const char *path = "/sys/firmware/papr/energy_scale_info";
@@ -71,11 +86,11 @@ static int verify_energy_info(void)
        FAIL_IF(!S_ISDIR(s.st_mode));
 
        dirp = opendir(path);
+       FAIL_IF(!dirp);
 
        while ((entry = readdir(dirp)) != NULL) {
                char file_name[64];
                int id, attr_type;
-               FILE *f;
 
                if (strcmp(entry->d_name, ".") == 0 ||
                    strcmp(entry->d_name, "..") == 0)
@@ -83,28 +98,34 @@ static int verify_energy_info(void)
 
                id = atoi(entry->d_name);
                attr_type = value_type(id);
-               FAIL_IF(attr_type == INVALID);
+               if (attr_type == INVALID) {
+                       fprintf(stderr, "[FAIL] Test FAILED on line %d\n",
+                               __LINE__);
+                       goto out_fail;
+               }
 
                /* Check if the files exist and have data in them */
                sprintf(file_name, "%s/%d/desc", path, id);
-               f = fopen(file_name, "r");
-               FAIL_IF(!f);
-               FAIL_IF(fgetc(f) == EOF);
+               if (check_file(file_name))
+                       goto out_fail;
 
                sprintf(file_name, "%s/%d/value", path, id);
-               f = fopen(file_name, "r");
-               FAIL_IF(!f);
-               FAIL_IF(fgetc(f) == EOF);
+               if (check_file(file_name))
+                       goto out_fail;
 
                if (attr_type == STR_VAL) {
                        sprintf(file_name, "%s/%d/value_desc", path, id);
-                       f = fopen(file_name, "r");
-                       FAIL_IF(!f);
-                       FAIL_IF(fgetc(f) == EOF);
+                       if (check_file(file_name))
+                               goto out_fail;
                }
        }
 
+       closedir(dirp);
        return 0;
+
+out_fail:
+       closedir(dirp);
+       return 1;
 }
 
 int main(void)
-- 
2.50.1.windows.1




Reply via email to