From patchwork Tue Jun 7 15:50:03 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: 12872078 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 2EC1DC433EF for ; Tue, 7 Jun 2022 15:51:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236619AbiFGPvM (ORCPT ); Tue, 7 Jun 2022 11:51:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230036AbiFGPvI (ORCPT ); Tue, 7 Jun 2022 11:51:08 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B8FDF33A5 for ; Tue, 7 Jun 2022 08:51:06 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id o37-20020a05600c512500b0039c4ba4c64dso3977450wms.2 for ; Tue, 07 Jun 2022 08:51:06 -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=Yp+pxPsPCKspCvMR+GHXTKwM7XJ17BbdiKbig5kf474=; b=IgoQAHl1iAD98FKHx4duNhpVNocrtdZ3jOzQxwInh9gNuEckZXzb2Pl94UPn4drxOz 2jBDTPuExTYz00N82QRBP64UeX1D23L1yIWaL/GeazBukUrT8vhoa8ZGACSL/uJo7ESQ x8p3oMUVuGpQa2YNiEitNjCYbfNuryadqnjqCzJG4r9282GLJIJOwXGjefeQ80PwkfL4 aTOSHHNRbbGrSD2bkGHeI/4RUQXuTCbzXZ9ouNuruPlXMtnT3oNKJSeoXBXhv5SL+n3l DNEgaWCMF6zByGRthpA0O4QVShq65cUjIrdKGIRuYkvv0I5+q9qBlhKUq2aRoWI+wA1J NR1g== 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=Yp+pxPsPCKspCvMR+GHXTKwM7XJ17BbdiKbig5kf474=; b=yLjtaXYXfEaXxlynlhGWHWGBAsdAL3IBdWA53cr8WU0icOnyW7k5ZdSwPu5YtD3dbp yRWtojiHU64EpH9nJhEeLEeCmBmQWMK/1ceJOVhyFdNZY7lLYEzwMihjHnY3Hr6M4HUT lzUiVtD8Y3n7N//KDLFo5eldhvTSoFky2yG5cvp9TGM7XNpjoSq0Mf0cp67XWLqH/byr 4BbGF5wsYZTksHpx1UhAXE9sqhTBqLdgia06RNDbR3ivnjahRdZincvaCf72X88RJefg HPA1abtk0rlqRUZ0gCppAMSUlc9krPYYP41Dqwemj9sPz6+y1Ch6/FqydvseXQCf95kD 8XHg== X-Gm-Message-State: AOAM531HZjfb0JX5anzZN26fuuilrRQ+veMsHXlUzX1fkI3mSQqLLfaX ocIaeEvwmggdttt5/Ann+cQGtbY5m77ovw== X-Google-Smtp-Source: ABdhPJxyFi8w/ru3xkBE2SMFYLRtbFypcsrCVUTZSUkA5uUgCUY6Fl4U1xQ+wX6C7xCPLjQlZg2hWw== X-Received: by 2002:a7b:cc12:0:b0:37c:1ae:100a with SMTP id f18-20020a7bcc12000000b0037c01ae100amr57683658wmh.54.1654617064936; Tue, 07 Jun 2022 08:51:04 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id s6-20020adfbc06000000b0020fe4c5e94csm18790056wrg.19.2022.06.07.08.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 08:51:03 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7AgQmphcm1hc29u?= Subject: [PATCH 1/3] remote.c: remove braces from one-statement "for"-loops Date: Tue, 7 Jun 2022 17:50:03 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1178.g0c3594a0ba5 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Remove braces that don't follow the CodingGuidelines from code added in fd3cb0501e1 (remote: move static variables into per-repository struct, 2021-11-17). A subsequent commit will edit code adjacent to this. Signed-off-by: Ævar Arnfjörð Bjarmason --- remote.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/remote.c b/remote.c index e98148ac227..3e75db7bb4f 100644 --- a/remote.c +++ b/remote.c @@ -145,14 +145,12 @@ static void remote_clear(struct remote *remote) free((char *)remote->name); free((char *)remote->foreign_vcs); - for (i = 0; i < remote->url_nr; i++) { + for (i = 0; i < remote->url_nr; i++) free((char *)remote->url[i]); - } FREE_AND_NULL(remote->pushurl); - for (i = 0; i < remote->pushurl_nr; i++) { + for (i = 0; i < remote->pushurl_nr; i++) free((char *)remote->pushurl[i]); - } FREE_AND_NULL(remote->pushurl); free((char *)remote->receivepack); free((char *)remote->uploadpack); @@ -2720,9 +2718,8 @@ void remote_state_clear(struct remote_state *remote_state) { int i; - for (i = 0; i < remote_state->remotes_nr; i++) { + for (i = 0; i < remote_state->remotes_nr; i++) remote_clear(remote_state->remotes[i]); - } FREE_AND_NULL(remote_state->remotes); remote_state->remotes_alloc = 0; remote_state->remotes_nr = 0; From patchwork Tue Jun 7 15:50:04 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: 12872079 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 B634BC43334 for ; Tue, 7 Jun 2022 15:51:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344317AbiFGPvO (ORCPT ); Tue, 7 Jun 2022 11:51:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236559AbiFGPvI (ORCPT ); Tue, 7 Jun 2022 11:51:08 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96F2EF33A9 for ; Tue, 7 Jun 2022 08:51:07 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id u8so20428364wrm.13 for ; Tue, 07 Jun 2022 08:51:07 -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=+4Ahy/6m/84ucWfcFHTC+Vfzg0oC8QUvy5LQLFp/Ous=; b=qGdU17D3WMdHkOc27fGB7COuaH8pCpk3iZJNTLy05DCwGLlmuOZnjrIRFK8ye/TmjN 8neO6noPnSBQHxC+4z2WnZneMcf3KG5mfbxZP05dRHuchnz6e3QkrjUucwClhn5oDp7u m/67GWynVKfR+qjCPK34SksNbI3yKO7TwuIpF0VlE+nHgl/ufzajxszDfCWFSr5lq52v E4r4ACA+9h54sLiovAvfuc1AeYc8jOf/3gb7fWxuGmCcrG1tM/xLYWtWqhuQCsnVWa5y L3DFIozZKp7d71HAqG+Ej64mKWO+lRF1wTIKHY9R0S4mTzMtAF6LvR4/l63rrAzD5YdM 4TKA== 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=+4Ahy/6m/84ucWfcFHTC+Vfzg0oC8QUvy5LQLFp/Ous=; b=0Gq2NNSTVwwfuSmucxXHYI1w0nB0e+yy2QjNQC1bief+e57mzn5CBNNpC5FmRFvaBy VFv0cZOVS3p/eI8IHBekEmC/v0M+toYTtsPbIjPYsY/obdJnwANZ254gkps7VZSHcQ5w CtRrQTv9OX+FG31qj+vODVL4PvTG5bZciPau11nXj/iWHD0bQxV/5KsUZ1r0iFMTDdBK Igih36kjPFATE1QXRjQ1cgulZsKTGkq4UbM8ZiXe0018fuBlTdwyUIh5VYj0oNXLn36s 7bXljaSERBuwrWHyX9YaIbqtbFH7+C8SHEIbAKvcJzn8yNR5v2zAZ5vhRCWE8dx9Ts05 dqkw== X-Gm-Message-State: AOAM532+qVUUUlNpFX0SvICfcACPLjh2lIU1ReBhGiYeqrSU0mWJMQUp iuXCGpC+GXOZX9KwDbjaJ+AouDqMmPQBJw== X-Google-Smtp-Source: ABdhPJx8TfbXWLGBw6aWVqPfet1RbECmySEPcPkUO4J2j3+VrYg/W7J/WAcva2Xl/bKRp0RV64+X5w== X-Received: by 2002:a5d:6d0d:0:b0:218:45b1:ef1f with SMTP id e13-20020a5d6d0d000000b0021845b1ef1fmr9359557wrq.558.1654617065869; Tue, 07 Jun 2022 08:51:05 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id s6-20020adfbc06000000b0020fe4c5e94csm18790056wrg.19.2022.06.07.08.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 08:51:05 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7AgQmphcm1hc29u?= Subject: [PATCH 2/3] remote.c: don't dereference NULL in freeing loop Date: Tue, 7 Jun 2022 17:50:04 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1178.g0c3594a0ba5 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a bug in fd3cb0501e1 (remote: move static variables into per-repository struct, 2021-11-17) where we'd free(remote->pushurl[i]) after having NULL'd out remote->pushurl. itself. We free "remote->pushurl" in the next "for"-loop, so doing this appears to have been a copy/paste error. Before this change GCC 12's -fanalyzer would correctly note that we'd dereference NULL in this case, this change fixes that: remote.c: In function ‘remote_clear’: remote.c:153:17: error: dereference of NULL ‘*remote.pushurl’ [CWE-476] [-Werror=analyzer-null-dereference] 153 | free((char *)remote->pushurl[i]); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [...] Signed-off-by: Ævar Arnfjörð Bjarmason --- remote.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remote.c b/remote.c index 3e75db7bb4f..0b243b090d9 100644 --- a/remote.c +++ b/remote.c @@ -147,7 +147,7 @@ static void remote_clear(struct remote *remote) for (i = 0; i < remote->url_nr; i++) free((char *)remote->url[i]); - FREE_AND_NULL(remote->pushurl); + FREE_AND_NULL(remote->url); for (i = 0; i < remote->pushurl_nr; i++) free((char *)remote->pushurl[i]); From patchwork Tue Jun 7 15:50:05 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: 12872080 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 C5345C43334 for ; Tue, 7 Jun 2022 15:51:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344323AbiFGPvQ (ORCPT ); Tue, 7 Jun 2022 11:51:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245690AbiFGPvK (ORCPT ); Tue, 7 Jun 2022 11:51:10 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A82F6EAD31 for ; Tue, 7 Jun 2022 08:51:08 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id d14so15766722wra.10 for ; Tue, 07 Jun 2022 08:51:08 -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=z8V4twYBhbhTkrwQrLdadzwTvSaU8JMoplmgQEXcCAE=; b=ZCGqo2g3Ex4MNiIYw2w6wkmYCjLa9YDCRJSdDB5vAReNvZV3TmO6F4J5wFq2ATvKkj LfYUrqeDzt9vzk3zsqXOXxPh+V/LJMCmhaKktX3ZVGiWkff0DizqLvSTfpmkAPji6orB I1s7VI6Fs6Igxs0MjaFX3H5OH9LjT+YQrOGMaPHGLMX4gfj1tvEEnKIiNCVdpy3as7+J NZOLnOsF9p9yKA9ktTXh1t2MPnjwiv2tKIQYiPRBmfMzpPQfS+uTysAZrUbxAdNcI+p6 rl+jIgVKb7QgOBpVz57VAFi60ycPKn5hP7XK9ELHflqsLTQhf6MuvqQIJNQhuTgsfYkJ 71Zw== 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=z8V4twYBhbhTkrwQrLdadzwTvSaU8JMoplmgQEXcCAE=; b=OnAjp462K8JsJoDENTZlat2XNE+Cny1p2Avmb+y8gQYKCqktf3hFUem/0sJT0XYn3R /ooT25aLsbnNL5jz2yfttP8j8qTRQ6ePo1466HtSFq9hPK3lqB3GheVRzTGjKdnfFlYd 98JOyjWeMJ447gsSTcXq47pLg6U00ZxxyIkh6Iznfw3/+4JGgwHJjYtVhds/uTnQIaLe 8sCDGrzJmJ1E48JvYmPBm8EfneZGuk+XJaBZo7b4nPYGSlzSjNpCHXIkAQuQXig4uG+j lPXZOOFqmBPuYgXnX/kXOtn2Qc2DPbDwIQMEygQowFun0ws8hkAvRcJuYTrL48Au513H TOHg== X-Gm-Message-State: AOAM533lzoqjLrTbzIzFPJNbDvprKm7qp1Ju7103pjbb+ju0LlfTnt73 RMdHOxO3qdoXIlxOkM85JistqP9NtaKWxQ== X-Google-Smtp-Source: ABdhPJw1Wb2dJQd4I3Td1YmUrD43Invuv3gaWiiJ+xFUbRZYWFRZhVjWEROtGTk9EZcGVIUKUnmDgw== X-Received: by 2002:adf:df8e:0:b0:210:2e5c:695d with SMTP id z14-20020adfdf8e000000b002102e5c695dmr28823373wrl.423.1654617066882; Tue, 07 Jun 2022 08:51:06 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id s6-20020adfbc06000000b0020fe4c5e94csm18790056wrg.19.2022.06.07.08.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 08:51:06 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Glen Choo , Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7AgQmphcm1hc29u?= Subject: [PATCH 3/3] remote API: don't buggily FREE_AND_NULL(), free() instead Date: Tue, 7 Jun 2022 17:50:05 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1178.g0c3594a0ba5 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the buggy "remote_clear()" function to stop pretending to to be able to zero out a "struct remote". Setting "url" and "pushurl" to NULL results in an invalid state unless the corresponding "url_nr" and "pushurl_nr" are also set to zero. In this case however we do not use the "struct remote", so the FREE_AND_NULL() pattern added in fd3cb0501e1 (remote: move static variables into per-repository struct, 2021-11-17) can be replaced with free(). The API was also odd in that remote_state_new() would xmalloc() for us, but the user had to free() it themselves, let's instead change the behavior to have the destructor free() what we malloc() in the constructer. In this case this appears to have been done for consistency with repo_clear(), let's instead have repo_clear() handle the NULL-ing of its "remote_state", and not attempt to reset the structure in remote.c Signed-off-by: Ævar Arnfjörð Bjarmason --- remote.c | 14 +++++++------- remote.h | 10 +++++++++- repository.c | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/remote.c b/remote.c index 0b243b090d9..c6ce04dacb7 100644 --- a/remote.c +++ b/remote.c @@ -147,15 +147,15 @@ static void remote_clear(struct remote *remote) for (i = 0; i < remote->url_nr; i++) free((char *)remote->url[i]); - FREE_AND_NULL(remote->url); + free(remote->url); for (i = 0; i < remote->pushurl_nr; i++) free((char *)remote->pushurl[i]); - FREE_AND_NULL(remote->pushurl); + free(remote->pushurl); free((char *)remote->receivepack); free((char *)remote->uploadpack); - FREE_AND_NULL(remote->http_proxy); - FREE_AND_NULL(remote->http_proxy_authmethod); + free(remote->http_proxy); + free(remote->http_proxy_authmethod); } static void add_merge(struct branch *branch, const char *name) @@ -2720,12 +2720,12 @@ void remote_state_clear(struct remote_state *remote_state) for (i = 0; i < remote_state->remotes_nr; i++) remote_clear(remote_state->remotes[i]); - FREE_AND_NULL(remote_state->remotes); - remote_state->remotes_alloc = 0; - remote_state->remotes_nr = 0; + free(remote_state->remotes); hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent); hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent); + + free(remote_state); } /* diff --git a/remote.h b/remote.h index dd4402436f1..d91b2b29373 100644 --- a/remote.h +++ b/remote.h @@ -54,9 +54,17 @@ struct remote_state { int initialized; }; -void remote_state_clear(struct remote_state *remote_state); +/** + * xmalloc() a "struct remote_state" and initialize it. The resulting + * data should be free'd with remote_state_clear(). + */ struct remote_state *remote_state_new(void); +/** + * free() the structure returned by remote_state_new(). + */ +void remote_state_clear(struct remote_state *remote_state); + struct remote { struct hashmap_entry ent; diff --git a/repository.c b/repository.c index 5d166b692c8..0a6df6937e4 100644 --- a/repository.c +++ b/repository.c @@ -292,7 +292,7 @@ void repo_clear(struct repository *repo) if (repo->remote_state) { remote_state_clear(repo->remote_state); - FREE_AND_NULL(repo->remote_state); + repo->remote_state = NULL; } repo_clear_path_cache(&repo->cached_paths);