From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Elijah Newren" <newren@gmail.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 1/2] string_list API users + cocci: use string_list_init_dup()
Date: Thu, 21 Jul 2022 08:39:44 +0200 [thread overview]
Message-ID: <patch-1.2-c89758491e7-20220721T063543Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-0.2-00000000000-20220721T063543Z-avarab@gmail.com>
Add a coccinelle rule to detect a particular misuse of the "struct
string_list" API. We have the *_INIT macros, but this code assumed
that a zero'd out "struct string_list" with a "strdup_string" set
would be the same as string_list_init_dup().
That assumption happens to be right, but let's instead use the helper
functions introduced in 183113a5ca9 (string_list: Add STRING_LIST_INIT
macro and make use of it., 2010-07-04).
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
contrib/coccinelle/string_list.cocci | 8 ++++++++
contrib/coccinelle/tests/string_list.c | 7 +++++++
contrib/coccinelle/tests/string_list.res | 7 +++++++
refs.c | 4 ++--
resolve-undo.c | 8 ++++----
revision.c | 4 ++--
6 files changed, 30 insertions(+), 8 deletions(-)
create mode 100644 contrib/coccinelle/string_list.cocci
create mode 100644 contrib/coccinelle/tests/string_list.c
create mode 100644 contrib/coccinelle/tests/string_list.res
diff --git a/contrib/coccinelle/string_list.cocci b/contrib/coccinelle/string_list.cocci
new file mode 100644
index 00000000000..5d285d5732c
--- /dev/null
+++ b/contrib/coccinelle/string_list.cocci
@@ -0,0 +1,8 @@
+@@
+struct string_list *P;
+@@
+- CALLOC_ARRAY(P, 1);
++ ALLOC_ARRAY(P, 1);
+... when != P
+- (P)->strdup_strings = 1;
++ string_list_init_dup(P);
diff --git a/contrib/coccinelle/tests/string_list.c b/contrib/coccinelle/tests/string_list.c
new file mode 100644
index 00000000000..e77822b7682
--- /dev/null
+++ b/contrib/coccinelle/tests/string_list.c
@@ -0,0 +1,7 @@
+int init(void)
+{
+ struct string_list *list;
+
+ CALLOC_ARRAY(list, 1);
+ list->strdup_strings = 1;
+}
diff --git a/contrib/coccinelle/tests/string_list.res b/contrib/coccinelle/tests/string_list.res
new file mode 100644
index 00000000000..7e666f5bf48
--- /dev/null
+++ b/contrib/coccinelle/tests/string_list.res
@@ -0,0 +1,7 @@
+int init(void)
+{
+ struct string_list *list;
+
+ ALLOC_ARRAY(list, 1);
+ string_list_init_dup(list);
+}
diff --git a/refs.c b/refs.c
index 90bcb271687..83151a42b3a 100644
--- a/refs.c
+++ b/refs.c
@@ -1313,8 +1313,8 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti
while (len && ref[len - 1] == '/')
ref[--len] = '\0';
if (!hide_refs) {
- CALLOC_ARRAY(hide_refs, 1);
- hide_refs->strdup_strings = 1;
+ ALLOC_ARRAY(hide_refs, 1);
+ string_list_init_dup(hide_refs);
}
string_list_append(hide_refs, ref);
}
diff --git a/resolve-undo.c b/resolve-undo.c
index e81096e2d45..e66b8306fe0 100644
--- a/resolve-undo.c
+++ b/resolve-undo.c
@@ -15,8 +15,8 @@ void record_resolve_undo(struct index_state *istate, struct cache_entry *ce)
return;
if (!istate->resolve_undo) {
- CALLOC_ARRAY(resolve_undo, 1);
- resolve_undo->strdup_strings = 1;
+ ALLOC_ARRAY(resolve_undo, 1);
+ string_list_init_dup(resolve_undo);
istate->resolve_undo = resolve_undo;
}
resolve_undo = istate->resolve_undo;
@@ -57,8 +57,8 @@ struct string_list *resolve_undo_read(const char *data, unsigned long size)
int i;
const unsigned rawsz = the_hash_algo->rawsz;
- CALLOC_ARRAY(resolve_undo, 1);
- resolve_undo->strdup_strings = 1;
+ ALLOC_ARRAY(resolve_undo, 1);
+ string_list_init_dup(resolve_undo);
while (size) {
struct string_list_item *lost;
diff --git a/revision.c b/revision.c
index 0c6e26cd9c8..e44af92cacc 100644
--- a/revision.c
+++ b/revision.c
@@ -1578,8 +1578,8 @@ void clear_ref_exclusion(struct string_list **ref_excludes_p)
void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
{
if (!*ref_excludes_p) {
- CALLOC_ARRAY(*ref_excludes_p, 1);
- (*ref_excludes_p)->strdup_strings = 1;
+ ALLOC_ARRAY(*ref_excludes_p, 1);
+ string_list_init_dup(*ref_excludes_p);
}
string_list_append(*ref_excludes_p, exclude);
}
--
2.37.1.1095.g64a1e8362fd
next prev parent reply other threads:[~2022-07-21 6:39 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-21 6:39 [PATCH 0/2] string_list API users: use alloc + init, not calloc + strdup_strings Ævar Arnfjörð Bjarmason
2022-07-21 6:39 ` Ævar Arnfjörð Bjarmason [this message]
2022-07-21 6:39 ` [PATCH 2/2] string-list API users: manually use string_list_init_*() Ævar Arnfjörð Bjarmason
2022-07-21 7:48 ` Elijah Newren
2022-07-21 12:00 ` [PATCH v2 0/6] string-list API user: fix API use, some with coccinelle Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 1/6] string_list API users + cocci: use string_list_init_dup() Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 2/6] cocci: apply string_list.cocci with --disable-worth-trying-opt Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 3/6] reflog-walk.c: use string_list_init_dup() Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 4/6] cocci: add "string_list" rule to swap "DUP" <-> "NODUP" Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 5/6] string-list API users: don't tweak "strdup_strings" to free dupes Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 6/6] notes.c: make "struct string_list display_notes_refs" non-static Ævar Arnfjörð Bjarmason
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=patch-1.2-c89758491e7-20220721T063543Z-avarab@gmail.com \
--to=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=newren@gmail.com \
/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).