From patchwork Thu Jul 21 06:39:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12924787 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D655C43334 for ; Thu, 21 Jul 2022 06:39:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231936AbiGUGj6 (ORCPT ); Thu, 21 Jul 2022 02:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbiGUGj5 (ORCPT ); Thu, 21 Jul 2022 02:39:57 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19A9D5247D for ; Wed, 20 Jul 2022 23:39:56 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id a5so799151wrx.12 for ; Wed, 20 Jul 2022 23:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0CjKbQSxjSLU4H4FdKciqb1gw8pQEvvBpj4at/MBM2o=; b=KDqGzfXpNgX+klxJSwGpQqnUsSldkgsrQ1F1GiIEHXw/jAGnHtgCMVp/6tuiiAvP5i 7kaMgRfSatvTFKOY9qjLZ5s/lFM3zwd6MP+iB3G6GunK3NSYYWx8Ms5zJTHrA36GYbJ0 E87Ds7X9KL9Ya1516gaEcs2qzBaQqjBjfXri07XQt+QOaqhpuGBmiuv1Sg07HpGYVDSq 25YPla0CwL6hEqG7YkjW6L0Pd7GMomarcLPHiuq6WsSTpzmTwPZxLMgmtYR8vbxvODvH ShEZaiqc8iIl/Xh4F7BRTqEXpjpKpRTzNYWaOKhFrXR3VuUIgkzHaIFHoqJPMdBACDCO fsTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0CjKbQSxjSLU4H4FdKciqb1gw8pQEvvBpj4at/MBM2o=; b=JISfHL27W+xYlbh4WLx0FQEFFaKAAd+Eyr8Ko9BKybiRO48o2l0O1Xf42XH9zTXPnV 5h2wdX4r3jMh55exxs+gkQz1Szocd2MIvJWYW0eER67Ml10jDvGyn3accdwuydkTJZgp SF7cMnkL9rUbUXxdLWhiaTyxqXIebb4hv4E4P/WoA4fBCnvsntrHdFmxjyz0xNrhOTwR 1oVjOdb0MgkmhCxPNjCkp6Ffuv4FLKGZq1F/fwG0LohVgF1P0U/+MiK+WbxJnyk4GYTA dzXHIUYff/IDw8IwbsaLrKvISTlxoYoIS8pjzSr+aBdI63nfMHxBeCJMM5OWY9QpQ63d jkmQ== X-Gm-Message-State: AJIora/GIr3GO3Q5KfowoM2cxOaE5Q6Vy+7lP+W5dtKpfP2VaP2Ly3Rj aI79Its5857RdJBc7ZK39N25mhI/y9O2Mg== X-Google-Smtp-Source: AGRyM1vzMqUvitq97s7Ow1jRi7xYX8GZhRY03MOy3resOwuD4SMuDDzBrf/wKjHedsWMvO+riPI48w== X-Received: by 2002:adf:dd82:0:b0:21d:62e3:a51 with SMTP id x2-20020adfdd82000000b0021d62e30a51mr32789437wrl.324.1658385594241; Wed, 20 Jul 2022 23:39:54 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id r67-20020a1c2b46000000b003a308e9a192sm4641137wmr.30.2022.07.20.23.39.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 23:39:52 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 1/2] string_list API users + cocci: use string_list_init_dup() Date: Thu, 21 Jul 2022 08:39:44 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1095.g64a1e8362fd In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 --- 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); } From patchwork Thu Jul 21 06:39:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12924788 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 727ADC433EF for ; Thu, 21 Jul 2022 06:40:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232139AbiGUGkF (ORCPT ); Thu, 21 Jul 2022 02:40:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbiGUGj7 (ORCPT ); Thu, 21 Jul 2022 02:39:59 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 559CF3AE56 for ; Wed, 20 Jul 2022 23:39:58 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id h9so867367wrm.0 for ; Wed, 20 Jul 2022 23:39:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nx8I+O/QnBUB1b1kH/84sWVXnNb+k3EGj5r3DnOoWdc=; b=Wj3tUZajKEm5HJFDaAaH6wng8n6/6kuO2CPerbcMoa5wYKXatfYC0VMetRtTYa7qpv g0jVtwPTBOvk4ZmD6bU++WsQ1qM3n3tQcAbi7NgUXqGXOWGRN7V04LwPwsiDtnYQ4cKF glN9O1pkhVwjAM8vvSbmWBDaFxaU+YQDt06DmsmR47/q9fmvXL8aLS5/9cOiVpd205oH ausV6VT+5vJv/c6TiaMpHGUeKlQr886WkXbpCy1br7Oxgdk3TCRemTjXFM5BY4hjKGO/ WRTKv8hGFkNxqqnThX3Tz6GpIeERJS3TnhFYQzJvFtN9PAvLFoeQTn5sWW6rWrJiva5r O+Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nx8I+O/QnBUB1b1kH/84sWVXnNb+k3EGj5r3DnOoWdc=; b=qZOmjIvp802zgcEaOs5yPNLbZKn0ugbuDdNtaKUcEhU8VptRXhyDjZpS01Un6jrlVT 6BhNBjPfjQ0fcDNqoz6KYb3hl6PBfVmFoa4xntV6usrdMGYIKbFTpYyccN1IXpLsPvq8 GIHto7KaJV7dq08PkEQvKBBA73tv6UJoa2mxdrcH5VgR5ixOKry0yjC2UYfoaeiYwZSU gs6pSF4oAj6axbAZMhe9LWM16X+du9+vYK7ZyKptOgQVOhU/7p3y48oZ6ovUJRFnr3pU d4frgOagIVP6VEq95mD9s9pde//1j909hI8XO6HdyKVZlveWPR8n4Xsm/g0AbVQLnHKL 6K0g== X-Gm-Message-State: AJIora+v8o5C0I/6pfmjJwXAc3Rp0P3sCclMkeB1WS2klCPVVXkHwspf 0147auMUygZiR9JpHtw2UU2Y0Bf+swDcbA== X-Google-Smtp-Source: AGRyM1uIGy7gUJfFIqoAm3e0JHywf0bH5/r4DNgecyVbRVemWfBcOQFvVfAdOfDvUZZDfF3GJTilNQ== X-Received: by 2002:a05:6000:901:b0:21a:a576:23cc with SMTP id bz1-20020a056000090100b0021aa57623ccmr31882742wrb.489.1658385596588; Wed, 20 Jul 2022 23:39:56 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id r67-20020a1c2b46000000b003a308e9a192sm4641137wmr.30.2022.07.20.23.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 23:39:54 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH 2/2] string-list API users: manually use string_list_init_*() Date: Thu, 21 Jul 2022 08:39:45 +0200 Message-Id: X-Mailer: git-send-email 2.37.1.1095.g64a1e8362fd In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Convert various code that didn't use string_list_init_*() to do so, in cases where the only thing being allocated was the string list we can change from CALLOC_ARRAY() to ALLOC_ARRAY(), the string_list_init_*() function will zero out the memory. This covers cases that weren't matched by tho coccinelle rule in the preceding commit, which is conservative enough to care about the type of what we're modifying. Signed-off-by: Ævar Arnfjörð Bjarmason --- notes-utils.c | 4 ++-- reflog-walk.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/notes-utils.c b/notes-utils.c index d7d18e30f5a..73559d24ec8 100644 --- a/notes-utils.c +++ b/notes-utils.c @@ -129,8 +129,8 @@ struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) c->cmd = cmd; c->enabled = 1; c->combine = combine_notes_concatenate; - CALLOC_ARRAY(c->refs, 1); - c->refs->strdup_strings = 1; + ALLOC_ARRAY(c->refs, 1); + string_list_init_dup(c->refs); c->refs_from_env = 0; c->mode_from_env = 0; if (rewrite_mode_env) { diff --git a/reflog-walk.c b/reflog-walk.c index 7aa6595a51f..2b17408f9a4 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -120,7 +120,7 @@ struct reflog_walk_info { void init_reflog_walk(struct reflog_walk_info **info) { CALLOC_ARRAY(*info, 1); - (*info)->complete_reflogs.strdup_strings = 1; + string_list_init_dup(&((*info)->complete_reflogs)); } void reflog_walk_info_release(struct reflog_walk_info *info)