Re: [PATCH v4 11/12] refs: implement logic to migrate between ref storage formats

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jun 03, 2024 at 11:31:00AM +0200, Patrick Steinhardt wrote:

> +int repo_migrate_ref_storage_format(struct repository *repo,
> +				    enum ref_storage_format format,
> +				    unsigned int flags,
> +				    struct strbuf *errbuf)
> +{
> [...]
> +	new_gitdir = mkdtemp(xstrdup(buf.buf));
> +	if (!new_gitdir) {
> +		strbuf_addf(errbuf, "cannot create migration directory: %s",
> +			    strerror(errno));
> +		ret = -1;
> +		goto done;
> +	}

Coverity complains here of a leak of the xstrdup(). The return from
mkdtemp() should generally point to the same buffer we passed in, but if
it sees an error it will return NULL and the new heap buffer will be
lost.

Probably unlikely, but since you are on a leak-checking kick, I thought
I'd mention it. ;)

Since you have a writable strbuf already, maybe:

  new_gitdir = mkdtemp(buf.buf);
  if (!new_gitdir)
	...
  new_gitdir = strbuf_detach(&buf, NULL); /* same pointer, but now we own it */

Or since "buf" is not used for anything else, we could just leave it
attached to the strbuf. And probably give it a better name. Maybe:

diff --git a/refs.c b/refs.c
index 166b6f269e..9a6655abee 100644
--- a/refs.c
+++ b/refs.c
@@ -2726,10 +2726,9 @@ int repo_migrate_ref_storage_format(struct repository *repo,
 {
 	struct ref_store *old_refs = NULL, *new_refs = NULL;
 	struct ref_transaction *transaction = NULL;
-	struct strbuf buf = STRBUF_INIT;
+	struct strbuf new_gitdir = STRBUF_INIT;
 	struct migration_data data;
 	size_t reflog_count = 0;
-	char *new_gitdir = NULL;
 	int did_migrate_refs = 0;
 	int ret;
 
@@ -2787,16 +2786,15 @@ int repo_migrate_ref_storage_format(struct repository *repo,
 	 *
 	 *   6. Change the repository format to the new ref format.
 	 */
-	strbuf_addf(&buf, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX");
-	new_gitdir = mkdtemp(xstrdup(buf.buf));
-	if (!new_gitdir) {
+	strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX");
+	if (!mkdtemp(new_gitdir.buf)) {
 		strbuf_addf(errbuf, "cannot create migration directory: %s",
 			    strerror(errno));
 		ret = -1;
 		goto done;
 	}
 
-	new_refs = ref_store_init(repo, format, new_gitdir,
+	new_refs = ref_store_init(repo, format, new_gitdir.buf,
 				  REF_STORE_ALL_CAPS);
 	ret = ref_store_create_on_disk(new_refs, 0, errbuf);
 	if (ret < 0)
@@ -2841,7 +2839,7 @@ int repo_migrate_ref_storage_format(struct repository *repo,
 
 	if (flags & REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN) {
 		printf(_("Finished dry-run migration of refs, "
-			 "the result can be found at '%s'\n"), new_gitdir);
+			 "the result can be found at '%s'\n"), new_gitdir.buf);
 		ret = 0;
 		goto done;
 	}
@@ -2862,13 +2860,13 @@ int repo_migrate_ref_storage_format(struct repository *repo,
 	if (ret < 0)
 		goto done;
 
-	ret = move_files(new_gitdir, old_refs->gitdir, errbuf);
+	ret = move_files(new_gitdir.buf, old_refs->gitdir, errbuf);
 	if (ret < 0)
 		goto done;
 
-	if (rmdir(new_gitdir) < 0)
+	if (rmdir(new_gitdir.buf) < 0)
 		warning_errno(_("could not remove temporary migration directory '%s'"),
-			      new_gitdir);
+			      new_gitdir.buf);
 
 	/*
 	 * We have migrated the repository, so we now need to adjust the
@@ -2888,13 +2886,12 @@ int repo_migrate_ref_storage_format(struct repository *repo,
 	if (ret && did_migrate_refs) {
 		strbuf_complete(errbuf, '\n');
 		strbuf_addf(errbuf, _("migrated refs can be found at '%s'"),
-			    new_gitdir);
+			    new_gitdir.buf);
 	}
 
 	if (ret && new_refs)
 		ref_store_release(new_refs);
 	ref_transaction_free(transaction);
-	strbuf_release(&buf);
-	free(new_gitdir);
+	strbuf_release(&new_gitdir);
 	return ret;
 }




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux