Skip to content

Commit b9c8e7f

Browse files
mhaggergitster
authored andcommitted
prefix_ref_iterator: don't trim too much
The `trim` parameter can be set independently of `prefix`. So if some caller were to set `trim` to be greater than `strlen(prefix)`, we could end up pointing the `refname` field of the iterator past the NUL of the actual reference name string. That can't happen currently, because `trim` is always set either to zero or to `strlen(prefix)`. But even the latter could lead to confusion, if a refname is exactly equal to the prefix, because then we would set the outgoing `refname` to the empty string. And we're about to decouple the `prefix` and `trim` arguments even more, so let's be cautious here. Report a bug if ever asked to trim a reference whose name is not longer than `trim`. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 04aea8d commit b9c8e7f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

refs/iterator.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,23 @@ static int prefix_ref_iterator_advance(struct ref_iterator *ref_iterator)
292292
if (!starts_with(iter->iter0->refname, iter->prefix))
293293
continue;
294294

295-
iter->base.refname = iter->iter0->refname + iter->trim;
295+
if (iter->trim) {
296+
/*
297+
* It is nonsense to trim off characters that
298+
* you haven't already checked for via a
299+
* prefix check, whether via this
300+
* `prefix_ref_iterator` or upstream in
301+
* `iter0`). So if there wouldn't be at least
302+
* one character left in the refname after
303+
* trimming, report it as a bug:
304+
*/
305+
if (strlen(iter->iter0->refname) <= iter->trim)
306+
die("BUG: attempt to trim too many characters");
307+
iter->base.refname = iter->iter0->refname + iter->trim;
308+
} else {
309+
iter->base.refname = iter->iter0->refname;
310+
}
311+
296312
iter->base.oid = iter->iter0->oid;
297313
iter->base.flags = iter->iter0->flags;
298314
return ITER_OK;

0 commit comments

Comments
 (0)