Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memleak in genversion.cpp #401

Closed
Ace17 opened this issue Jul 5, 2022 · 0 comments
Closed

Memleak in genversion.cpp #401

Ace17 opened this issue Jul 5, 2022 · 0 comments

Comments

@Ace17
Copy link

Ace17 commented Jul 5, 2022

This is minor, although as running genversion is part of the build process, this leak prevents building the project with asan enabled ( -fsanitized=address in CXXFLAGS and LDFLAGS ).

Here's a potential fix, which completely avoids dynamic memory allocation (the buffers were fixed-size anyway).

diff --git a/tools/genversion.cpp b/tools/genversion.cpp
index f589e057f..262201882 100644
--- a/tools/genversion.cpp
+++ b/tools/genversion.cpp
@@ -39,7 +39,7 @@ int main(int argc, char * argv[]) {
 	FILE * file;
 	int old_ver[4] = { -1, -1, -1, -1 };
 	int new_ver[4] = { -1, -1, -1, -1 };
-	char* old_rev = (char*)calloc(sizeof(char), 1024);
+	char old_rev[1024] {};
 
 	if ( argc != 3 )
 		return 1;
@@ -62,9 +62,10 @@ int main(int argc, char * argv[]) {
 	}
 
 	file = fopen(".git/HEAD", "r");
+	char git_rev_buf[1024] {};
 	char *git_rev, *gitrevfile;
 	if ( file ) {
-		git_rev = (char*)calloc(sizeof(char), 1024);
+		git_rev = git_rev_buf;
 		if (fscanf(file, "ref: %s", git_rev) != 1 ) {
 			int ignored = fscanf(file, "%s", git_rev);
 		}
@@ -74,7 +75,7 @@ int main(int argc, char * argv[]) {
 		file = fopen(gitrevfile, "r");
 		free(gitrevfile);
 		if (file) {
-			git_rev = (char*)calloc(sizeof(char), 1024);
+			git_rev = git_rev_buf;
 			int ignored = fscanf(file, "%s", git_rev);
 			fclose(file);
 		}
@Ace17 Ace17 closed this as completed Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant