commit 6f711b103fc86abf1cc94d145c48c5a927f01527 from: Omar Polo date: Sat Sep 03 13:05:15 2022 UTC plug mem leak in got_repo_pack_fds_open pack_fds_tmp is not always free'd. However, while here, simplify it to not use a temporary array. ok stsp@ commit - b94206d0acc1c55bad1233c35f959fa7c4af297b commit + 6f711b103fc86abf1cc94d145c48c5a927f01527 blob - 9d9dfd0586d7bfc07846c61a82e2f1778968d95c blob + b09e447cd160ea6b4ec21e61c49d40abcc7a3532 --- lib/repository.c +++ lib/repository.c @@ -252,16 +252,11 @@ const struct got_error * got_repo_pack_fds_open(int **pack_fds) { const struct got_error *err = NULL; - int i, *pack_fds_tmp; + int i; - pack_fds_tmp = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(int)); - if (pack_fds_tmp == NULL) - return got_error_from_errno("calloc"); *pack_fds = calloc(GOT_PACK_NUM_TEMPFILES, sizeof(**pack_fds)); - if (*pack_fds == NULL) { - free(pack_fds_tmp); + if (*pack_fds == NULL) return got_error_from_errno("calloc"); - } /* * got_repo_pack_fds_close will try to close all of the @@ -270,18 +265,19 @@ got_repo_pack_fds_open(int **pack_fds) * we do not initialize to -1 here. */ for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) - pack_fds_tmp[i] = -1; + (*pack_fds)[i] = -1; for (i = 0; i < GOT_PACK_NUM_TEMPFILES; i++) { - pack_fds_tmp[i] = got_opentempfd(); - if (pack_fds_tmp[i] == -1) { + (*pack_fds)[i] = got_opentempfd(); + if ((*pack_fds)[i] == -1) { err = got_error_from_errno("got_opentempfd"); - got_repo_pack_fds_close(pack_fds_tmp); + got_repo_pack_fds_close(*pack_fds); + *pack_fds = NULL; return err; } } - memcpy(*pack_fds, pack_fds_tmp, GOT_PACK_NUM_TEMPFILES * sizeof(int)); - return err; + + return NULL; } const struct got_error *