commit 41861f2b47879010d252aa5a6d8a7f8f734041f0 from: Stefan Sperling via: Thomas Adam date: Mon Sep 05 16:46:35 2022 UTC avoid a pointless malloc/free in got_reflist_insert() in my previous fix suggested by op@ commit - 21de8138fee88f812099d8b0b71d8280f4f6ba4e commit + 41861f2b47879010d252aa5a6d8a7f8f734041f0 blob - 3a699c9c2d5c9bc7d891ae0c0e65af894a4e653e blob + ceacd908686d8842b986df5c5ab20fbcf3acc81a --- 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.