reflog_expire_cfg: NUL-terminate pattern field

You can tweak the reflog expiration for a particular subset of refs by configuring gc.foo.reflogexpire. We keep a linked list of reflog_expire_cfg structs, each of which holds the pattern and a "len" field for the length of the pattern. The pattern itself is _not_ NUL-terminated. However, we feed the pattern directly to wildmatch(), which expects a NUL-terminated string, meaning it may keep reading random junk after our struct. We can fix this by allocating an extra byte for the NUL (which is already zero because we use xcalloc). Let's also drop the misleading "len" field, which is no longer necessary. The existing use of "len" can be converted to use strncmp(). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 19, 2016 at 06:21 UTC c3a700fba1693f68643f24c4f2e6d4cdd240babf
1 file changed +3 -5
builtin/reflog.c
+3 -5
@@ -396,7 +396,6 @@ static struct reflog_expire_cfg {
396 struct reflog_expire_cfg *next;
397 unsigned long expire_total;
398 unsigned long expire_unreachable;
399 - size_t len;
399 char pattern[FLEX_ARRAY];
400 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
401
@@ -408,13 +407,12 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
407 reflog_expire_cfg_tail = &reflog_expire_cfg;
408
409 for (ent = reflog_expire_cfg; ent; ent = ent->next)
411 - if (ent->len == len &&
412 - !memcmp(ent->pattern, pattern, len))
410 + if (!strncmp(ent->pattern, pattern, len) &&
411 + ent->pattern[len] == '\0')
412 return ent;
413
415 - ent = xcalloc(1, (sizeof(*ent) + len));
414 + ent = xcalloc(1, sizeof(*ent) + len + 1);
415 memcpy(ent->pattern, pattern, len);
417 - ent->len = len;
416 *reflog_expire_cfg_tail = ent;
417 reflog_expire_cfg_tail = &(ent->next);
418 return ent;