All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Martin Ågren" <martin.agren@gmail.com>
To: git@vger.kernel.org
Cc: Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH v3 2/3] refs/files-backend: fix memory leak in lock_ref_for_update
Date: Tue, 29 Aug 2017 19:18:23 +0200	[thread overview]
Message-ID: <f136e83168453af7a0d1eba45c8ad47d01dd9a41.1504024261.git.martin.agren@gmail.com> (raw)
In-Reply-To: <CAN0heSqn59sFF3A-eQ593G+ZDWnO9pKM5F=sgiSQk+prUr-nSQ@mail.gmail.com>

After the previous patch, none of the functions we call hold on to
`referent.buf`, so we can safely release the string buffer before
returning.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
v3: identical to v2

 refs/files-backend.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index 73615d869..a2b3df21b 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2266,7 +2266,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 	struct strbuf referent = STRBUF_INIT;
 	int mustexist = (update->flags & REF_HAVE_OLD) &&
 		!is_null_oid(&update->old_oid);
-	int ret;
+	int ret = 0;
 	struct ref_lock *lock;
 
 	files_assert_main_repository(refs, "lock_ref_for_update");
@@ -2278,7 +2278,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 		ret = split_head_update(update, transaction, head_ref,
 					affected_refnames, err);
 		if (ret)
-			return ret;
+			goto out;
 	}
 
 	ret = lock_raw_ref(refs, update->refname, mustexist,
@@ -2292,7 +2292,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 		strbuf_addf(err, "cannot lock ref '%s': %s",
 			    original_update_refname(update), reason);
 		free(reason);
-		return ret;
+		goto out;
 	}
 
 	update->backend_data = lock;
@@ -2311,10 +2311,12 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 					strbuf_addf(err, "cannot lock ref '%s': "
 						    "error reading reference",
 						    original_update_refname(update));
-					return -1;
+					ret = -1;
+					goto out;
 				}
 			} else if (check_old_oid(update, &lock->old_oid, err)) {
-				return TRANSACTION_GENERIC_ERROR;
+				ret = TRANSACTION_GENERIC_ERROR;
+				goto out;
 			}
 		} else {
 			/*
@@ -2328,13 +2330,15 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 						  referent.buf, transaction,
 						  affected_refnames, err);
 			if (ret)
-				return ret;
+				goto out;
 		}
 	} else {
 		struct ref_update *parent_update;
 
-		if (check_old_oid(update, &lock->old_oid, err))
-			return TRANSACTION_GENERIC_ERROR;
+		if (check_old_oid(update, &lock->old_oid, err)) {
+			ret = TRANSACTION_GENERIC_ERROR;
+			goto out;
+		}
 
 		/*
 		 * If this update is happening indirectly because of a
@@ -2371,7 +2375,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 				    "cannot update ref '%s': %s",
 				    update->refname, write_err);
 			free(write_err);
-			return TRANSACTION_GENERIC_ERROR;
+			ret = TRANSACTION_GENERIC_ERROR;
+			goto out;
 		} else {
 			update->flags |= REF_NEEDS_COMMIT;
 		}
@@ -2385,10 +2390,14 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 		if (close_ref(lock)) {
 			strbuf_addf(err, "couldn't close '%s.lock'",
 				    update->refname);
-			return TRANSACTION_GENERIC_ERROR;
+			ret = TRANSACTION_GENERIC_ERROR;
+			goto out;
 		}
 	}
-	return 0;
+
+out:
+	strbuf_release(&referent);
+	return ret;
 }
 
 /*
-- 
2.14.1.151.g45c1275a3.dirty


  parent reply	other threads:[~2017-08-29 17:18 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-25 18:49 [PATCH 1/2] refs/files-backend: duplicate strings added to affected_refnames Martin Ågren
2017-08-25 18:49 ` [PATCH 2/2] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-08-25 21:00 ` [PATCH 1/2] refs/files-backend: duplicate strings added to affected_refnames Junio C Hamano
2017-08-26 10:16   ` Martin Ågren
2017-08-28  8:06     ` Michael Haggerty
2017-08-28 10:09       ` Martin Ågren
2017-08-28 20:32         ` [PATCH v2 1/2] refs/files-backend: add longer-scoped copy of string to list Martin Ågren
2017-08-29  8:33           ` Michael Haggerty
2017-08-28 20:32         ` [PATCH v2 2/2] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-08-29  8:39           ` Michael Haggerty
2017-08-29 10:41             ` Martin Ågren
2017-08-29 17:18               ` [PATCH v3 1/3] refs/files-backend: add longer-scoped copy of string to list Martin Ågren
2017-08-30  2:52                 ` Michael Haggerty
2017-08-30 18:02                   ` Martin Ågren
2017-09-05 10:02                     ` Junio C Hamano
2017-09-05 17:24                       ` Martin Ågren
2017-09-05 20:36                         ` Jeff King
2017-09-05 21:26                           ` Junio C Hamano
2017-09-06 18:12                             ` Martin Ågren
2017-09-06 19:52                               ` Junio C Hamano
2017-09-06 23:45                               ` Jeff King
2017-09-09  6:57                               ` [PATCH v4 0/4] Rerolling ma/split-symref-update-fix Martin Ågren
2017-09-09  6:57                                 ` [PATCH v4 1/4] refs/files-backend: add longer-scoped copy of string to list Martin Ågren
2017-09-09  6:57                                 ` [PATCH v4 2/4] refs/files-backend: fix memory leak in lock_ref_for_update Martin Ågren
2017-09-09  6:57                                 ` [PATCH v4 3/4] refs/files-backend: correct return value " Martin Ågren
2017-09-09  6:57                                 ` [PATCH v4 4/4] refs/files-backend: add `refname`, not "HEAD", to list Martin Ågren
2017-09-09 10:47                                 ` [PATCH v4 0/4] Rerolling ma/split-symref-update-fix Jeff King
2017-09-05  8:45                 ` [PATCH v3 1/3] refs/files-backend: add longer-scoped copy of string to list Jeff King
2017-09-05  9:03                   ` Michael Haggerty
2017-09-05  9:04                     ` Jeff King
2017-08-29 17:18               ` Martin Ågren [this message]
2017-09-05  8:47                 ` [PATCH v3 2/3] refs/files-backend: fix memory leak in lock_ref_for_update Jeff King
2017-09-05 17:28                   ` Martin Ågren
2017-08-29 17:18               ` [PATCH v3 3/3] refs/files-backend: correct return value " Martin Ågren

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f136e83168453af7a0d1eba45c8ad47d01dd9a41.1504024261.git.martin.agren@gmail.com \
    --to=martin.agren@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mhagger@alum.mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.