summaryrefslogtreecommitdiffstats
path: root/refspec.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2024-11-12 09:36:10 +0100
committerJunio C Hamano <gitster@pobox.com>2024-11-12 10:16:48 +0100
commitd36af33081cac89a840f4a4cbc4af1ba4aa40392 (patch)
treedc435a8d45bc7f8bdbcc74ef72e7f2c84fee9bec /refspec.c
parentfetch: adjust refspec->raw_nr when filtering prefetch refspecs (diff)
downloadgit-d36af33081cac89a840f4a4cbc4af1ba4aa40392.tar.xz
git-d36af33081cac89a840f4a4cbc4af1ba4aa40392.zip
refspec: drop separate raw_nr count
A refspec struct contains zero or more refspec_item structs, along with matching "raw" strings. The items and raw strings are kept in separate arrays, but those arrays will always have the same length (because we write them only via refspec_append_nodup(), which grows both). This can lead to bugs when manipulating the array, since the arrays and lengths must be modified in lockstep. For example, the bug fixed in the previous commit, which forgot to decrement raw_nr. So let's get rid of "raw_nr" and have only "nr", making this kind of bug impossible (and also making it clear that the two are always matched, something that existing code already assumed but was not guaranteed by the interface). Even though we'd expect "alloc" and "raw_alloc" to likewise move in lockstep, we still need to keep separate counts there if we want to continue to use ALLOC_GROW() for both. Conceptually this would all be simpler if refspec_item just held onto its own raw string, and we had a single array. But there are callers which use refspec_item outside of "struct refspec" (and so don't hold on to a matching "raw" string at all), which we'd possibly need to adjust. So let's not worry about refactoring that for now, and just get rid of the redundant count variable. That is the first step on the road to combining them anyway. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refspec.c')
-rw-r--r--refspec.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/refspec.c b/refspec.c
index c3cf003443..8e8ee8542d 100644
--- a/refspec.c
+++ b/refspec.c
@@ -186,10 +186,12 @@ static void refspec_append_nodup(struct refspec *rs, char *refspec)
refspec_item_init_or_die(&item, refspec, rs->fetch);
ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
- rs->items[rs->nr++] = item;
+ rs->items[rs->nr] = item;
- ALLOC_GROW(rs->raw, rs->raw_nr + 1, rs->raw_alloc);
- rs->raw[rs->raw_nr++] = refspec;
+ ALLOC_GROW(rs->raw, rs->nr + 1, rs->raw_alloc);
+ rs->raw[rs->nr] = refspec;
+
+ rs->nr++;
}
void refspec_append(struct refspec *rs, const char *refspec)
@@ -217,18 +219,17 @@ void refspec_clear(struct refspec *rs)
{
int i;
- for (i = 0; i < rs->nr; i++)
+ for (i = 0; i < rs->nr; i++) {
refspec_item_clear(&rs->items[i]);
+ free(rs->raw[i]);
+ }
FREE_AND_NULL(rs->items);
rs->alloc = 0;
rs->nr = 0;
- for (i = 0; i < rs->raw_nr; i++)
- free(rs->raw[i]);
FREE_AND_NULL(rs->raw);
rs->raw_alloc = 0;
- rs->raw_nr = 0;
rs->fetch = 0;
}