Skip to contentSkip to conversation

scripts/sorttable: Fix resource leak in parse_symbols()

PATCH In review Dheeraj Reddy Jonnalagadda 2 participants 2 messages Feb 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/sorttable: Fix resource leak in parse_symbols()

Fix a resource leak in parse_symbols() where the file pointer fp was not closed if add_field() returned an error. This caused an open file descriptor to remain unclosed. Ensure that fp is properly closed before returning an error.

Fixes ef378c3b8233 ("scripts/sorttable: Zero out weak functions in mcount_loc table")
---
 scripts/sorttable.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/sorttable.c +3 -1
index 23c7e0e6c024..b9b066c1afee 100644
@@ -662,8 +662,10 @@ static int parse_symbols(const char *fname)
 
 		addr = strtoull(addr_str, NULL, 16);
 		size = strtoull(size_str, NULL, 16);
-		if (add_field(addr, size) < 0)
+		if (add_field(addr, size) < 0) {
+			fclose(fp);
 			return -1;
+		}
 	}
 	fclose(fp);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · On Tue, 25 Feb 2025 11:07:24 +0530 Dheeraj Reddy Jonnalagadda…

Fix a resource leak in parse_symbols() where the file pointer fp was not closed if add_field() returned an error. This caused an open file descriptor to remain unclosed. Ensure that fp is properly closed before returning an error.

Fixes ef378c3b8233 ("scripts/sorttable: Zero out weak functions in mcount_loc table")

Thanks, but this isn't needed. If it returns -1 it will exit the program. When a program exits, it will close all opened file descriptors.

I may add it just because it's proper to close descriptors in case this function is used for something else (highly unlikely). But it's not fixing anything, and there is no "leak".

-- Steve

Quoted 21 lines
---
 scripts/sorttable.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/sorttable.c +3 -1
index 23c7e0e6c024..b9b066c1afee 100644
@@ -662,8 +662,10 @@ static int parse_symbols(const char *fname)
 
 		addr = strtoull(addr_str, NULL, 16);
 		size = strtoull(size_str, NULL, 16);
-		if (add_field(addr, size) < 0)
+		if (add_field(addr, size) < 0) {
+			fclose(fp);
 			return -1;
+		}
 	}
 	fclose(fp);