commit d24f46c8ce16fd3fb0da2987f8197b1e71b74a7a5ffe31581b16f74d80b842fe from: Mark Jamsek date: Tue Jan 21 15:22:19 2025 UTC plug memleak and ensure fd is valid before passing to close(2) While here, fix potential NULL deref in gotadmin cleanup and pack error paths. ok stsp@ commit - ad52156f9e918051844eb400a4e6e2cbc36772ca2dfa37d33f37a8854bc9b78a commit + d24f46c8ce16fd3fb0da2987f8197b1e71b74a7a5ffe31581b16f74d80b842fe blob - f109076b9e1535aed6d69c591fe0359f793d01dd8e999d8d9fa6ee909a225d6a blob + 467e6f1a43ba8c05f98b8445b5077fd3295a8268abda8f88937314bce5ba8f33 --- lib/repository_admin.c +++ lib/repository_admin.c @@ -162,8 +162,10 @@ create_temp_packfile(int *packfd, char **tmpfile_path, if (fchmod(*packfd, GOT_DEFAULT_PACK_MODE) == -1) err = got_error_from_errno2("fchmod", *tmpfile_path); done: + free(path); if (err) { - close(*packfd); + if (*packfd != -1) + close(*packfd); *packfd = -1; free(*tmpfile_path); *tmpfile_path = NULL; @@ -287,7 +289,8 @@ done: free(theirs[i]); free(theirs); if (packfd != -1 && close(packfd) == -1 && err == NULL) - err = got_error_from_errno2("close", packfile_path); + err = got_error_from_errno2("close", + packfile_path ? packfile_path : tmpfile_path); if (delta_cache && fclose(delta_cache) == EOF && err == NULL) err = got_error_from_errno("fclose"); if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL) @@ -1593,7 +1596,8 @@ got_repo_cleanup(struct got_repository *repo, if (traversed_ids) got_object_idset_free(traversed_ids); if (packfd != -1 && close(packfd) == -1 && err == NULL) - err = got_error_from_errno2("close", packfile_path); + err = got_error_from_errno2("close", + packfile_path ? packfile_path : tmpfile_path); if (delta_cache && fclose(delta_cache) == EOF && err == NULL) err = got_error_from_errno("fclose"); if (tmpfile_path && unlink(tmpfile_path) == -1 && err == NULL)