Commit Diff


commit - 7db2b0ddc68d7d2761c2c1b93fa9543431efde97
commit + 3d8e0c5ede1a5654397b63a9f483d875543527d5
blob - a7ca5b927daf30815b370c13f3ec304c395a1051
blob + ec8a575ccccc0191c9140e4914ca6dd642338b9c
--- lib/reference.c
+++ lib/reference.c
@@ -853,15 +853,8 @@ got_reflist_insert(struct got_reflist_entry **newp,
 	int cmp;
 
 	*newp = NULL;
-
-	new = malloc(sizeof(*new));
-	if (new == NULL)
-		return got_error_from_errno("malloc");
-	new->ref = ref;
-	*newp = new;
 
-	if (cmp_cb != got_ref_cmp_by_name &&
-	    (new->ref->flags & GOT_REF_IS_PACKED)) {
+	if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
 		/*
 		 * If we are not sorting elements by name then we must still
 		 * detect collisions between a packed ref and an on-disk ref
@@ -869,18 +862,20 @@ got_reflist_insert(struct got_reflist_entry **newp,
 		 * already present on the list before packed refs get added.
 		 */
 		TAILQ_FOREACH(re, refs, entry) {
-			err = got_ref_cmp_by_name(NULL, &cmp,
-			    re->ref, new->ref);
+			err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
 			if (err)
 				return err;
-			if (cmp == 0) {
-				free(new);
-				*newp = NULL;
+			if (cmp == 0)
 				return NULL;
-			}
 		}
 	}
 
+	new = malloc(sizeof(*new));
+	if (new == NULL)
+		return got_error_from_errno("malloc");
+	new->ref = ref;
+	*newp = new;
+
 	/*
 	 * We must de-duplicate entries on insert because packed-refs may
 	 * contain redundant entries. On-disk refs take precedence.