From: Omar Polo Subject: last (?) round of memleaks To: gameoftrees@openbsd.org Date: Tue, 06 Sep 2022 12:11:16 +0200 played again a bit with gotwebd under valgrind and found some other leaks. two are in gotwebd and the others in lib/. some of the got_repo_get_* functions allocate a string that we forget to free. I did a round of checking and fixed the ones that were (possibly) leaking memory. Should we consider to rename some of this functions to avoid the ambiguity? (some of the got_repo_get_* returns const char *, other don't.) diff /tmp/got commit - 7e0ec0529553d4a8e236cad41366f79eda5126b3 path + /tmp/got blob - 1595b111128cb8eaca43db33c9b1eab5a5ff4291 file + gotwebd/got_operations.c --- gotwebd/got_operations.c +++ gotwebd/got_operations.c @@ -184,6 +184,9 @@ got_get_repo_age(char **repo_age, struct request *c, c if (refname) break; + + got_object_close_commit(commit); + commit = NULL; } if (cmp_time != 0) { @@ -191,6 +194,8 @@ got_get_repo_age(char **repo_age, struct request *c, c error = gotweb_get_time_str(repo_age, committer_time, ref_tm); } done: + if (commit) + got_object_close_commit(commit); got_ref_list_free(&refs); return error; } @@ -923,6 +928,8 @@ done: got_ref_list_free(&refs); if (commit) got_object_commit_close(commit); + if (tree) + got_object_tree_close(commit); free(commit_id); free(tree_id); return error; blob - b09e447cd160ea6b4ec21e61c49d40abcc7a3532 file + lib/repository.c --- lib/repository.c +++ lib/repository.c @@ -1629,21 +1629,27 @@ match_packed_object(struct got_object_id **unique_id, struct got_repository *repo, const char *id_str_prefix, int obj_type) { const struct got_error *err = NULL; + char *objects_pack_dir = NULL; struct got_object_id_queue matched_ids; struct got_pathlist_entry *pe; struct stat sb; STAILQ_INIT(&matched_ids); - if (stat(got_repo_get_path_objects_pack(repo), &sb) == -1) { - if (errno != ENOENT) - return got_error_from_errno2("stat", - got_repo_get_path_objects_pack(repo)); + objects_pack_dir = got_repo_get_path_objects_pack(repo); + if (objects_pack_dir == NULL) + return got_error_from_errno("got_repo_get_path_objects_pack"); + + if (stat(objects_pack_dir, &sb) == -1) { + if (errno != ENOENT) { + err = got_error_from_errno2("stat", objects_pack_dir); + goto done; + } } else if (sb.st_mtime != repo->pack_path_mtime) { purge_packidx_paths(&repo->packidx_paths); err = got_repo_list_packidx(&repo->packidx_paths, repo); if (err) - return err; + goto done; } TAILQ_FOREACH(pe, &repo->packidx_paths, entry) { @@ -1692,6 +1698,7 @@ match_packed_object(struct got_object_id **unique_id, } } done: + free(objects_pack_dir); got_object_id_queue_free(&matched_ids); if (err) { free(*unique_id); @@ -1790,21 +1797,27 @@ got_repo_match_object_id_prefix(struct got_object_id * const char *id_str_prefix, int obj_type, struct got_repository *repo) { const struct got_error *err = NULL; - char *path_objects = got_repo_get_path_objects(repo); - char *object_dir = NULL; + char *path_objects = NULL, *object_dir = NULL; size_t len; int i; *id = NULL; + path_objects = got_repo_get_path_objects(repo); + if (path_objects == NULL) + return got_error_from_errno("got_repo_get_path_objects"); + len = strlen(id_str_prefix); - if (len > SHA1_DIGEST_STRING_LENGTH - 1) - return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR); + if (len > SHA1_DIGEST_STRING_LENGTH - 1) { + err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR); + goto done; + } for (i = 0; i < len; i++) { if (isxdigit((unsigned char)id_str_prefix[i])) continue; - return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR); + err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR); + goto done; } if (len >= 2) { @@ -1840,6 +1853,7 @@ got_repo_match_object_id_prefix(struct got_object_id * goto done; } done: + free(path_objects); free(object_dir); if (err) { free(*id);