Skip to contentSkip to conversation

perf jevents: Fix resource leak in process_mapfile()

PATCH Superseded Yunfeng Ye 3 participants 6 messages Oct 16, 2019 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

PATCH 1/1 perf jevents: Fix resource leak in process_mapfile()

There are memory leaks and file descriptor resource leaks in process_mapfile().

Fix this by adding free() and fclose() on the error paths.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/perf/pmu-events/jevents.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/pmu-events/jevents.c +7 -2
index e2837260ca4d..6e60d4cff592 100644
@@ -758,6 +758,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
 	char *line, *p;
 	int line_num;
 	char *tblname;
+	int ret = 0;
 
 	pr_info("%s: Processing mapfile %s\n", prog, fpath);
 
@@ -769,6 +770,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
 	if (!mapfp) {
 		pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
 				fpath);
+		free(line);
 		return -1;
 	}
 
@@ -795,7 +797,8 @@ static int process_mapfile(FILE *outfp, char *fpath)
 			/* TODO Deal with lines longer than 16K */
 			pr_info("%s: Mapfile %s: line %d too long, aborting\n",
 					prog, fpath, line_num);
-			return -1;
+			ret = -1;
+			goto out;
 		}
 		line[strlen(line)-1] = '\0';
 
@@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
 
 out:
 	print_mapping_table_suffix(outfp);
-	return 0;
+	fclose(mapfp);
+	free(line);
+	return ret;
 }
 
 /*
Signature · 1 line
2.7.4.3
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Yunfeng Ye

There are memory leaks and file descriptor resource leaks in process_mapfile().

Fix this by adding free() and fclose() on the error paths.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")

thanks,
jirka

Quoted 48 lines
---
 tools/perf/pmu-events/jevents.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/pmu-events/jevents.c +7 -2
index e2837260ca4d..6e60d4cff592 100644
@@ -758,6 +758,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
 	char *line, *p;
 	int line_num;
 	char *tblname;
+	int ret = 0;
 
 	pr_info("%s: Processing mapfile %s\n", prog, fpath);
 
@@ -769,6 +770,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
 	if (!mapfp) {
 		pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
 				fpath);
+		free(line);
 		return -1;
 	}
 
@@ -795,7 +797,8 @@ static int process_mapfile(FILE *outfp, char *fpath)
 			/* TODO Deal with lines longer than 16K */
 			pr_info("%s: Mapfile %s: line %d too long, aborting\n",
 					prog, fpath, line_num);
-			return -1;
+			ret = -1;
+			goto out;
 		}
 		line[strlen(line)-1] = '\0';
 
@@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
 
 out:
 	print_mapping_table_suffix(outfp);
-	return 0;
+	fclose(mapfp);
+	free(line);
+	return ret;
 }
 
 /*
Signature · 1 line
2.7.4.3
#2 lore raw
In reply to Yunfeng Ye
Quoted 32 lines · Yunfeng Ye

There are memory leaks and file descriptor resource leaks in process_mapfile().

Fix this by adding free() and fclose() on the error paths.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/perf/pmu-events/jevents.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/pmu-events/jevents.c +2 -0
index e2837260ca4d..6e60d4cff592 100644
@@ -758,6 +758,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
 	char *line, *p;
 	int line_num;
 	char *tblname;
+	int ret = 0;
 
 	pr_info("%s: Processing mapfile %s\n", prog, fpath);
 
@@ -769,6 +770,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
 	if (!mapfp) {
 		pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
 				fpath);
+		free(line);
 		return -1;
 	}
 

tools/perf/pmu-events/jevents.c static int process_mapfile(FILE *outfp, char *fpath) · line 801

Quoted 6 lines
tools/perf/pmu-events/jevents.c +2 -1
@@ -795,7 +797,8 @@ static int process_mapfile(FILE *outfp, char *fpath)
 			/* TODO Deal with lines longer than 16K */
 			pr_info("%s: Mapfile %s: line %d too long, aborting\n",
 					prog, fpath, line_num);
-			return -1;
+			ret = -1;
+			goto out;

There's a subtle change of behaviour here, i.e. now calling print_mapping_table_suffix(), but I don't think that it makes any difference.

However, does outfp remain open also in this case:

main(int argc, char *argv[])
{
...

if (process_mapfile(eventsfp, mapfile)) {
	pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
	/* Make build fail */
	return 1;
}

return 0;

empty_map:

	fclose(eventsfp);
	...
}

I think that this code works on the basis that the program exits on any sort of error and releases resources automatically. Having said that, it is a good practice to tidy up.

John

Quoted 15 lines
tools/perf/pmu-events/jevents.c +3 -1
 		}
 		line[strlen(line)-1] = '\0';
 
@@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
 
 out:
 	print_mapping_table_suffix(outfp);
-	return 0;
+	fclose(mapfp);
+	free(line);
+	return ret;
 }
 
 /*
author
#3 lore raw
Quoted 32 lines · John Garry
Quoted 31 lines · Yunfeng Ye

There are memory leaks and file descriptor resource leaks in process_mapfile().

Fix this by adding free() and fclose() on the error paths.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")

---
 tools/perf/pmu-events/jevents.c | 9 +++++++--

 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/pmu-events/jevents.c +2 -0
index e2837260ca4d..6e60d4cff592 100644
@@ -758,6 +758,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
      char *line, *p;
      int line_num;
      char *tblname;
+    int ret = 0;
 
      pr_info("%s: Processing mapfile %s\n", prog, fpath);
 
@@ -769,6 +770,7 @@ static int process_mapfile(FILE *outfp, char *fpath)
      if (!mapfp) {
          pr_info("%s: Error %s opening %s\n", prog, strerror(errno),
                  fpath);
+        free(line);
          return -1;
      }
 
Quoted 10 lines

tools/perf/pmu-events/jevents.c static int process_mapfile(FILE *outfp, char *fpath) · line 801

Quoted 7 lines
tools/perf/pmu-events/jevents.c +2 -1
@@ -795,7 +797,8 @@ static int process_mapfile(FILE *outfp, char *fpath)
              /* TODO Deal with lines longer than 16K */
              pr_info("%s: Mapfile %s: line %d too long, aborting\n",
                      prog, fpath, line_num);
-            return -1;
+            ret = -1;
+            goto out;

There's a subtle change of behaviour here, i.e. now calling print_mapping_table_suffix(), but I don't think that it makes any difference.

yes, I know that "goto out" will run print_mapping_table_suffix(outfp), because the error path before is done like this. so I think it should be use "goto out" to run run print_mapping_table_suffix(outfp).

Quoted 2 lines

However, does outfp remain open also in this case:

Because it has a comment that "Make build fail", so I am not handle the outfp, only modify the process_mapfile() function.

Quoted 10 lines

main(int argc, char *argv[])
{
...

if (process_mapfile(eventsfp, mapfile)) {
    pr_info("%s: Error processing mapfile %s\n", prog, mapfile); /* Make build fail */
    return 1;
}

Quoted 9 lines

return 0;

empty_map:
    fclose(eventsfp);
    ...
}

I think that this code works on the basis that the program exits on any sort of error and releases resources automatically. Having said that, it is a good practice to tidy up.

I agree with you, when program exits, it will releases resources automatically. It's just to make the program clearer and more correct.

Quoted 22 lines

John

Quoted 13 lines
tools/perf/pmu-events/jevents.c +0 -0
          }
          line[strlen(line)-1] = '\0';
 
@@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
 

 out:
     print_mapping_table_suffix(outfp);
-    return 0;
+    fclose(mapfp);
+    free(line);
+    return ret;
 }

Quoted 2 lines

 /*

.

#4 lore raw
Quoted 24 lines
Quoted 5 lines

tools/perf/pmu-events/jevents.c static int process_mapfile(FILE *outfp, char *fpath) · line 801

Quoted 2 lines
tools/perf/pmu-events/jevents.c +2 -0
+            ret = -1;
+            goto out;

There's a subtle change of behaviour here, i.e. now calling print_mapping_table_suffix(), but I don't think that it makes any difference.

yes, I know that "goto out" will run print_mapping_table_suffix(outfp), because the error path before is done like this. so I think it should be use "goto out" to run run print_mapping_table_suffix(outfp).

Quoted 2 lines

However, does outfp remain open also in this case:

Because it has a comment that "Make build fail", so I am not handle the outfp, only modify the process_mapfile() function.

Quoted 12 lines

main(int argc, char *argv[])
{
...

if (process_mapfile(eventsfp, mapfile)) {
    pr_info("%s: Error processing mapfile %s\n", prog, mapfile);
    /* Make build fail */
    return 1;
}

return 0;

Quoted 8 lines
Quoted 7 lines

empty_map:

    fclose(eventsfp);
    ...
}

I think that this code works on the basis that the program exits on any sort of error and releases resources automatically. Having said that, it is a good practice to tidy up.

I agree with you, when program exits, it will releases resources automatically. It's just to make the program clearer and more correct.

So can you make that change also (to close outfp)?

Thanks,
John

Quoted 27 lines
Quoted 16 lines

John

Quoted 13 lines
         }
         line[strlen(line)-1] = '\0';
tools/perf/pmu-events/jevents.c +3 -1
@@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
 
 out:
     print_mapping_table_suffix(outfp);
-    return 0;
+    fclose(mapfp);
+    free(line);
+    return ret;
 }
 
Quoted 7 lines

Review static int process_mapfile(FILE *outfp, char *fpath) · line 836

Quoted 2 lines
(quoted diff) +0 -0
 /*

.

.

author
#5 lore raw
Quoted 29 lines · John Garry
Quoted 29 lines
Quoted 5 lines
Quoted 2 lines

+            ret = -1;
+            goto out;

There's a subtle change of behaviour here, i.e. now calling print_mapping_table_suffix(), but I don't think that it makes any difference.

yes, I know that "goto out" will run print_mapping_table_suffix(outfp), because the error path before is done like this. so I think it should be use "goto out" to run run print_mapping_table_suffix(outfp).

Quoted 2 lines

However, does outfp remain open also in this case:

Because it has a comment that "Make build fail", so I am not handle the outfp, only modify the process_mapfile() function.

Quoted 17 lines

main(int argc, char *argv[])
{
...

if (process_mapfile(eventsfp, mapfile)) {
    pr_info("%s: Error processing mapfile %s\n", prog, mapfile); /* Make build fail */
    return 1;
}

return 0;

empty_map:
    fclose(eventsfp);
    ...
}

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

I think that this code works on the basis that the program exits on any sort of error and releases resources automatically. Having said that, it is a good practice to tidy up.

I agree with you, when program exits, it will releases resources automatically. It's just to make the program clearer and more correct.

So can you make that change also (to close outfp)?

ok, I will modify by adding fclose(eventsfp) on the error path, In addition, free_arch_std_events() is need too.

Quoted 35 lines

Thanks,
John

Quoted 25 lines
Quoted 16 lines

John

Quoted 13 lines

         }
         line[strlen(line)-1] = '\0';

(quoted diff) +0 -0
@@ -825,7 +828,9 @@ static int process_mapfile(FILE *outfp, char *fpath)
 

 out:
     print_mapping_table_suffix(outfp);
-    return 0;
+    fclose(mapfp);
+    free(line);
+    return ret;
 }

Quoted 7 lines
Quoted 2 lines

 /*

.

Quoted 2 lines

.

.