On Thu, May 23, 2024 at 8:27 AM Patrick Steinhardt <ps@xxxxxx> wrote: > Add two functions that allow to replace and remove strings contained in > the strvec. This will be used by a subsequent commit that refactors > git-mv(1). > [...] > Signed-off-by: Patrick Steinhardt <ps@xxxxxx> > --- > diff --git a/strvec.c b/strvec.c > @@ -56,6 +56,26 @@ void strvec_pushv(struct strvec *array, const char **items) > +const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement) > +{ > + char *to_free; > + if (idx >= array->nr) > + BUG("index outside of array boundary"); > + to_free = (char *) array->v[idx]; > + array->v[idx] = xstrdup(replacement); > + free(to_free); > + return array->v[idx]; > +} The reason you delay calling free() until after xstrdup() is to protect against the case when `replacement` is a substring of the string already stored at `v[idx]`, correct?