From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>, Patrick Steinhardt <ps@pks.im>,
Junio C Hamano <gitster@pobox.com>,
karthik nayak <karthik.188@gmail.com>
Subject: [PATCH v3] strvec: `strvec_splice()` to a statically initialized vector
Date: Wed, 4 Dec 2024 23:44:25 +0100 [thread overview]
Message-ID: <3c7b3c26-7501-4797-8afa-c7f7e9c46558@gmail.com> (raw)
In-Reply-To: <5bea9f20-eb0d-409d-8f37-f20697d6ce14@gmail.com>
We use a singleton empty array to initialize a `struct strvec`;
similar to the empty string singleton we use to initialize a `struct
strbuf`.
Note that an empty strvec instance (with zero elements) does not
necessarily need to be an instance initialized with the singleton.
Let's refer to strvec instances initialized with the singleton as
"empty-singleton" instances.
As a side note, this is the current `strvec_pop()`:
void strvec_pop(struct strvec *array)
{
if (!array->nr)
return;
free((char *)array->v[array->nr - 1]);
array->v[array->nr - 1] = NULL;
array->nr--;
}
So, with `strvec_pop()` an instance can become empty but it does
not going to be the an "empty-singleton".
This "empty-singleton" circumstance requires us to be careful when
adding elements to instances. Specifically, when adding the first
element: when we detach the strvec instance from the singleton and
set the internal pointer in the instance to NULL. After this point we
apply `realloc()` on the pointer. We do this in
`strvec_push_nodup()`, for example.
The recently introduced `strvec_splice()` API is expected to be
normally used with non-empty strvec's. However, it can also end up
being used with "empty-singleton" strvec's:
struct strvec arr = STRVEC_INIT;
int a = 0, b = 0;
... no modification to arr, a or b ...
const char *rep[] = { "foo" };
strvec_splice(&arr, a, b, rep, ARRAY_SIZE(rep));
So, we'll try to add elements to an "empty-singleton" strvec instance.
Avoid misapplying `realloc()` to the singleton in `strvec_splice()` by
adding a special case for strvec's initialized with the singleton.
Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
This iteration fixes a problem we saw when running with SANITIZE=leak.
Although it wasn't a leak.
We need to end the array because `realloc(NULL)` is not going to give
us that { NULL }. I know it's something I considered at some point
because I thought about a change like `CALLOC_GROW()`. Perhaps
another time.
strvec.c | 11 +++++++----
t/unit-tests/strvec.c | 10 ++++++++++
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/strvec.c b/strvec.c
index d1cf4e2496..62283fcef2 100644
--- a/strvec.c
+++ b/strvec.c
@@ -61,16 +61,19 @@ void strvec_splice(struct strvec *array, size_t idx, size_t len,
{
if (idx + len > array->nr)
BUG("range outside of array boundary");
- if (replacement_len > len)
+ if (replacement_len > len) {
+ if (array->v == empty_strvec)
+ array->v = NULL;
ALLOC_GROW(array->v, array->nr + (replacement_len - len) + 1,
array->alloc);
+ array->v[array->nr + (replacement_len - len) + 1] = NULL;
+ }
for (size_t i = 0; i < len; i++)
free((char *)array->v[idx + i]);
- if (replacement_len != len) {
+ if ((replacement_len != len) && array->nr)
memmove(array->v + idx + replacement_len, array->v + idx + len,
(array->nr - idx - len + 1) * sizeof(char *));
- array->nr += (replacement_len - len);
- }
+ array->nr += replacement_len - len;
for (size_t i = 0; i < replacement_len; i++)
array->v[idx + i] = xstrdup(replacement[i]);
}
diff --git a/t/unit-tests/strvec.c b/t/unit-tests/strvec.c
index 855b602337..e66b7bbfae 100644
--- a/t/unit-tests/strvec.c
+++ b/t/unit-tests/strvec.c
@@ -88,6 +88,16 @@ void test_strvec__pushv(void)
strvec_clear(&vec);
}
+void test_strvec__splice_just_initialized_strvec(void)
+{
+ struct strvec vec = STRVEC_INIT;
+ const char *replacement[] = { "foo" };
+
+ strvec_splice(&vec, 0, 0, replacement, ARRAY_SIZE(replacement));
+ check_strvec(&vec, "foo", NULL);
+ strvec_clear(&vec);
+}
+
void test_strvec__splice_with_same_size_replacement(void)
{
struct strvec vec = STRVEC_INIT;
Interdiff against v2:
diff --git a/strvec.c b/strvec.c
index 087c020f5b..62283fcef2 100644
--- a/strvec.c
+++ b/strvec.c
@@ -66,6 +66,7 @@ void strvec_splice(struct strvec *array, size_t idx, size_t len,
array->v = NULL;
ALLOC_GROW(array->v, array->nr + (replacement_len - len) + 1,
array->alloc);
+ array->v[array->nr + (replacement_len - len) + 1] = NULL;
}
for (size_t i = 0; i < len; i++)
free((char *)array->v[idx + i]);
--
2.47.0.281.g735430a4cf
prev parent reply other threads:[~2024-12-04 22:44 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-29 17:23 [PATCH] strvec: `strvec_splice()` to a statically initialized vector Rubén Justo
2024-12-02 1:49 ` Junio C Hamano
2024-12-02 22:01 ` Rubén Justo
2024-12-02 12:54 ` Patrick Steinhardt
2024-12-03 19:47 ` [PATCH v2] " Rubén Justo
2024-12-04 0:09 ` Junio C Hamano
2024-12-04 1:08 ` Rubén Justo
2024-12-04 7:41 ` Junio C Hamano
2024-12-04 8:46 ` Rubén Justo
2024-12-04 8:50 ` Rubén Justo
2024-12-04 10:15 ` Junio C Hamano
2024-12-09 1:32 ` Junio C Hamano
2024-12-09 1:35 ` Junio C Hamano
2024-12-09 1:56 ` Junio C Hamano
2024-12-09 2:15 ` Jeff King
2024-12-09 7:33 ` Junio C Hamano
2024-12-09 22:42 ` Rubén Justo
2024-12-04 11:26 ` karthik nayak
2024-12-04 22:22 ` Rubén Justo
2024-12-06 11:33 ` karthik nayak
2024-12-04 22:44 ` Rubén Justo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3c7b3c26-7501-4797-8afa-c7f7e9c46558@gmail.com \
--to=rjusto@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=karthik.188@gmail.com \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).