Re: [PATCH 16/20] strvec: add functions to replace and remove strings
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- May 23, 2024, 17:09 UTC
- Message-ID
- <CAPig+cTKrUL1AzY4Y+M130AvH5S=kSTdwo542T_GdFC=6JUu=Q@mail.gmail.com>
- In-Reply-To
- <c43c93db3bdc0ea5283a6d5c71d37d6fe949dd8c.1716465556.git.ps@pks.im>
On Thu, May 23, 2024 at 8:27 AM Patrick Steinhardt <ps@pks.im> wrote:
Show 18 quoted lines
> 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@pks.im>
> ---
> 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?