From patchwork Fri Dec 30 21:56:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13084556 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 C40EDC4332F for ; Fri, 30 Dec 2022 21:56:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230117AbiL3V4w (ORCPT ); Fri, 30 Dec 2022 16:56:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235440AbiL3V4v (ORCPT ); Fri, 30 Dec 2022 16:56:51 -0500 Received: from mout.web.de (mout.web.de [212.227.17.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 234141CFE1 for ; Fri, 30 Dec 2022 13:56:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1672437404; bh=LjngQSBkJCIJH4ACcFJIV5Q5pJVps0Baa0cunVQxd4c=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References:In-Reply-To; b=WWwyYMwYp92yRJz1KWN4n9BAb2nwgZQfL5NGT8twsBlYBWMTjjH+YBKkE6YUCiac+ ass34LHWi4R+XYNGi4TXkclOU0VF2ku8odcn4zR0SPBfOxyV8rYe7rBoP7JJi7NvNI 0auvMNfCmu4Jxg3RaM0FaNNJBBUtOexwGdE6s35IS3Tzd9CF09UEqzJe5/tuv1Met3 woaKvK6PGTlvKfGqscQeOqMOaJ2pJxDlFjE6BLYdSigvYkUV2sZVA7raUqxBZL+9uA OeB1yvUUfm9qPE2dd+QW2y3TO/BPiBqwA7VJeBCLVyBVuhXDNbuNSrDg1mRG2RPHDN d0Uc0idbT/iBg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.151.35]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1N0Icn-1owIWJ3eUB-00wtjj; Fri, 30 Dec 2022 22:56:44 +0100 Message-ID: Date: Fri, 30 Dec 2022 22:56:44 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: [PATCH 1/3] do full type check in COPY_ARRAY and MOVE_ARRAY Content-Language: en-US From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Junio C Hamano References: In-Reply-To: X-Provags-ID: V03:K1:tBPXt9OMWJcIBfd6m5fwwp6znEYSO/SjP9B9VzUMSeyUHb27YYE 2OEaHvCxofx1Kxexs2qfrhx8vOED6//InXL1S7PVM3hZUKjWryzx2m09xS46P8H/sQDWVa2 mCGYy3b1DQJivTHBUgpLnst3Vxc5bySKMr8hSU39UPrieTGvs9+P9QLXVXylHW2VsyFV5hv C/yC1TJLTRH8h9cyJQs3g== UI-OutboundReport: notjunk:1;M01:P0:nytalSmekHs=;R9fPlccbjZOsvaGssgG4jo05Xca g/4OkzLnB46z62fbVGYCvpTf3tHIMCTZmdBB/Q8jt5nOR5IXs8EwLDQJJAdQybhe+I/nuNMev lX0Oe+wMW4sXn2Ej3T15Iv4KRTff7rMspWuw4Qmb2p5cQN7reUHsQwXmrAaZFymZ+0GrzAQSf TJf0y/4XRHB21jEaO18NZ4AXUshTdNsMg6EU8905NebrPsbS8umzLn8rdOhZGZav9URwQCUjU 51Cnsg4FV6kalliDIRkunOohYKpV8hr5pU7xQ390soMTLa1naPv630IgPEUvLKrzblpkJLPom YjM2g7M4p3kd+4i36CHBjUtMaZBkSzVwApAcz60QkYuNIcEu9ykeaUqQ4jCLXAIaMn+wz2H/Y yD78ls1c6JV7A5xVT8akv1tnNBRP1eKC4xXJMOoLMFgaiVfovr86lzOLGCdfBsBakSDB4PN12 /X0PmYqtVewCZlRzUezcVqwObIJtEw3t2x5Y+UuTATpALyHH5A5wDcsOelFi262LNjRNxjzR/ QMwLG6G+A3WJZtHv4siosfRdztFvAXZjl1Su0AJGAw2gu1oWN0q6rKqEbOhAKVUdjRQvohF5G QdhpA5+L658WU/FNyZNFJRUx+tZHKZy9LUhijSVSV+0dfvqghbGewTxQOAsZf2wbiuVPDPeCl pSX7VLArXegg2cPVVHJXjQZ/p3OkEUcwBJ3vTkwDl9C2z2vYSRsShcd6gx9sNYZR1UAfChong 8kkBztLRxXF7Rey3CIcuxRSIfI8SiR4CDopg3jUvX/Yzma6moJ47u0NkX9lZZjyrpspVuoRaz 4yJM4wxazVoCWXhaIyJ9DP9KglIOY3svibO4sJthfItsr26K8nw2LQ0tWjcSgjBery4QYyK60 8OFXBi4YKvdAB/5tuCPLMqWBvqLh2NFto+wF4Mt3HYF/8wS5p8SynLEbQsCj+lfTfp+LbRxlO xmSFLg== Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Extend the element size comparison between source and destination with a full type check using an assignment. It is not actually evaluated and even optimized out, but compilers check the types before getting to that point, and report mismatches. The stricter check improves safety, as it catches attempts to copy between different types that happen to have the same size. The size check is still needed to avoid allowing copies from a array with a smaller element type to a bigger one, e.g. from a char array to an int array, which would be allowed by the assignment check alone. Signed-off-by: René Scharfe --- git-compat-util.h | 2 ++ 1 file changed, 2 insertions(+) -- 2.39.0 diff --git a/git-compat-util.h b/git-compat-util.h index 76e4b11131..8d04832988 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1094,6 +1094,7 @@ int xstrncmpz(const char *s, const char *t, size_t len); #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ + (0 ? (*(dst) = *(src), 0) : 0) + \ BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) static inline void copy_array(void *dst, const void *src, size_t n, size_t size) { @@ -1102,6 +1103,7 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size) } #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ + (0 ? (*(dst) = *(src), 0) : 0) + \ BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) static inline void move_array(void *dst, const void *src, size_t n, size_t size) { From patchwork Fri Dec 30 22:02:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13084557 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 428B2C4332F for ; Fri, 30 Dec 2022 22:02:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235571AbiL3WC0 (ORCPT ); Fri, 30 Dec 2022 17:02:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235440AbiL3WCY (ORCPT ); Fri, 30 Dec 2022 17:02:24 -0500 Received: from mout.web.de (mout.web.de [212.227.17.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C99BFAEE for ; Fri, 30 Dec 2022 14:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1672437740; bh=p5NLklPgxYdEN6F/t75APaVSodm8nwlQZ4jqnZt8NMk=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References:In-Reply-To; b=sKzlrv7a8gggWFb3udJ61aWCFsTi2AekUPZdIsVsn/Gq0jIw/FC6g+xlOeqbjUhN/ KyCtxo0dCa5lGwuzRZUA5Giaxnfwt6g2w83HVwYxFEDrwiL9glD8muwTxHx86qWIrR NdXTL8DZWoyrT9S+3z03EkB+ni5G+TCj+hbDzgJgewRCgDZ1EvO46Prf/1BNPoHq79 SC+I3LOQ9ACe6adh+hzBtOYLuzHIU7bhFMJC7VY8KFaTTzkvVR8utCXTnO9rPg+nPn qfCY+4D6VYUaEolnMsooaA8xdL6lly44UDLsy5JcCa31I+/3zzpy21BIriqtXLMxWj zpQMjXBHq1STQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.151.35]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1Mm9Zi-1oTYF949Am-00iLZl; Fri, 30 Dec 2022 23:02:20 +0100 Message-ID: <4dae3686-1183-0e70-a1c8-378b1cb91d23@web.de> Date: Fri, 30 Dec 2022 23:02:19 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: [PATCH 2/3] add DUP_ARRAY Content-Language: en-US From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Junio C Hamano References: In-Reply-To: X-Provags-ID: V03:K1:NDG/oacjsFAG8jZaClVp7Z/UiRZBL/jJWmymUeCcv7sTSK7JayI IFeQFrWiUffuMmdquYP3cFBwVfez63dsnvitCZHP3qtAGLetpB527tufE9OhTrGL6JMha5E /NeppFLAAYukazB7se8c/CeNugPJpzHOonZlXAVVBPdemp5j6jWfOsFdlKJdEuS6arN6ni/ Q4pLZXICzbeYkPFPNABfA== UI-OutboundReport: notjunk:1;M01:P0:p02tdqvZYus=;Q3GRbgNFMUFnlhjucyc9mIDSBO+ R+14Zhr4Ns+aqOeA68+ha0BpTt/XGw41azYcTwMhkU+nmAw0ubCdHqZqrk0gxLwKcgDW3Iyop WtSQFPlbAgEwWUS+90g6WxGyv7KAKe1v6RhTWtmVhoYHpgpFHq2VjEfdWRBy1vy6lk5fHixFz 5oXgSEf0/4J1CbV5TVlm+8PDRsDSXWmBKZUtCSs4IfCL6+r2aHE4blVahzpdl6XbfLjNPeiuc pzg4dOo8Vct2YVnbK8AveRWv799+COMmjJR6+yB5PAn8knTKAv1gZUGiDmJdQ2+zOAlhUKSoV llhEQrrH521CPFpmXou9h+9ESk8y0LfzJ4nR2au7b0lYl1L4kaLSFsZSuyg1RxWNMQAEqnPaq z2Dq25crUgfyljYl5U1UT71xYppkC6Fm2+/ndAPrWxsH8o4zuOK2CTHISR2XTweOjyj4z4pQz uXE+WlCSKGadeQfAxaArjG+0zo/Ib0ExlpSPe8VyifNqa9jUwqtitwjMv3teT0n0n7GfThDPC lehlj5sdDCiFR2t9N/UfzME9jH41QmNsuNF9APBnF39xZZ6y40VX3FAkYA0ao0j0+4t1/r685 74DQZfyRvsrt6jv1fjUm5rv/TcbaTHlZc3mQPQ+2NhPWEEMxLIe3tSff20wmSznwnWSeKPmR6 N5A+TjLMafSr0LUDWP1t9zYmCQqPk3XO1OfN3vBlwC+syo0sAnppd9RGBrJQVBtDhX+ynK/sk daJ2VA8OSDOOBA3jkipO8R4N1yQ0+JzETJupM4MqebipiEU8YvezoLbS9laieR4BCY0ZxhffK G95Xa+JilMtNragPBIYtOqtkKT/7Am+dH7qWAw8y3CVRs3Vvp91Wbny+LWgmOFA1tX8SHrKe7 BQevVNnUy4Rb0J+NP2ncy+6BE3wJ07NydCHh0zz53B2yhempiRr7Ekty8C9aojriAEfCui3zK EIwukg== Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a macro for allocating and populating a shallow copy of an array. It is intended to replace a sequence like this: ALLOC_ARRAY(dst, n); COPY_ARRAY(dst, src, n); With the less repetitive: DUP_ARRAY(dst, src, n); It checks whether the types of source and destination are compatible to ensure the copy can be used safely. An easier alternative would be to only consider the source and return a void pointer, that could be used like this: dst = ARRAY_DUP(src, n); That would be more versatile, as it could be used in declarations as well. Making it type-safe would require the use of typeof from C23, though, as far as I can see. So use the first variant, whose safety requires no compiler extensions or future features. Signed-off-by: René Scharfe --- git-compat-util.h | 5 +++++ 1 file changed, 5 insertions(+) -- 2.39.0 diff --git a/git-compat-util.h b/git-compat-util.h index 8d04832988..29f4f699b5 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1111,6 +1111,11 @@ static inline void move_array(void *dst, const void *src, size_t n, size_t size) memmove(dst, src, st_mult(size, n)); } +#define DUP_ARRAY(dst, src, n) do { \ + size_t dup_array_n_ = (n); \ + COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \ +} while (0) + /* * These functions help you allocate structs with flex arrays, and copy * the data directly into the array. For example, if you had: From patchwork Fri Dec 30 22:03:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13084558 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 198C8C4332F for ; Fri, 30 Dec 2022 22:03:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235440AbiL3WDz (ORCPT ); Fri, 30 Dec 2022 17:03:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbiL3WDx (ORCPT ); Fri, 30 Dec 2022 17:03:53 -0500 Received: from mout.web.de (mout.web.de [217.72.192.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EBCE1CFD6 for ; Fri, 30 Dec 2022 14:03:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1672437826; bh=LP2XrV3BS2X6qW0VoBU0te+OmGVIDTkXkHqlFTh3kQc=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References:In-Reply-To; b=tGqHn/eLukXvD+oWLTIVvAkG00dNABxXVYP7KcFozRYea7MSZwhk+r9zIaLM1R7m8 iYlkpBwM0ikEEe68d8uKNElKYh3SuWmEiVybjBNx1bikXWXDSFf/MSvCG9QPy+DLBO MmkcBaUVSID5tCGFr1mxXlt0AyNOgAItbgwTcsmsuPo4NVpkO7QjjkpBvQoKeSrxAB ayttyOo9T16Ak2Qehy5v2R1Do0PS0Q4jYnv2wrK36y0Gr0vNYhXhymc8gzbXvSutUi jBLImrXeV6Zgbr3Ajo/MyKiZqz0XcLA0GXdjHUaI57/AAhDj5I6ur3bx+t32Y59cwe 7080MUjoHa0dQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.151.35]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MzTPW-1oyGTi1FuC-00vWzy; Fri, 30 Dec 2022 23:03:46 +0100 Message-ID: <3f8f6ae4-dc70-d1e6-682c-17d078f2c546@web.de> Date: Fri, 30 Dec 2022 23:03:45 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: [PATCH 3/3] use DUP_ARRAY Content-Language: en-US From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Junio C Hamano References: In-Reply-To: X-Provags-ID: V03:K1:wXRR8ljhkltKyhZB+1UoR8xZ+4aD7pYOEG3IOAueDDGDAC9CCYl dXDoA4fZQJDU7FirQX0I2gupqIqOi4M6oqwYO7DStxgzIyAoWD0iH6C6Zx4o8sIyhAho33I X8lq8g2Khf2Cm5yt0pMv+lrij0MwGMYuWlYz0XjG/i3sWXjVTHi8Gc+GEDZk9/vhcPK36J/ 4a6d8iqAkLfk7O2EBO5Pw== UI-OutboundReport: notjunk:1;M01:P0:yNFxyBczBZo=;TbpwgBEA9VxhmbB/69YEwUYSByo SVs3/RRwAi2RJ+rHo187QoFAtvYeZ51H3m3178Nijo+Ech0fV/ft2/jfXV/SeWzKms2SVQ1Mt bBFCEyo6EsDvrEUbu9mPAPv2G/budCfNysIBaFaoEkAF03lQ8sqnSZHoZSK/cYbXpNbnPYmCD 80IF6pXfcyDbRlJtP4ZCEObpLEPUiDAEU9Y/M/FPWmlmqm/KpzzYIqZdE4EjjZyp0kvCmiGIn cTRX/Rf6HAww4uO2YU15Hd83fslKI6wlCGFW6O6qGQzraEyljIgOURLSpCRhwlNLSMs1nA4MD vx8KORBvfehGIbuw0l8BuBt+eMbx+bX0FwzpTUjYtj5gjL5uIqME/IURIGNqejwJvbi5CSGIS rS7miTLcA3ayFLchMAl5yiVK49fbdyz5y0leH8F5gVTC1z4r6xy1hyd3zgot+ZQFooos9AM4B hkaAckuZivjPEekOX/ZckIVeuGRdzXOxh+5hqHH4iy5WzXFZS1w34Ci6NjNZ3KJSmj8xFTGK+ aux9ZiE8jMJTJG9roV2eFybuC+aSCXzHd9f6Lnq+jHXydDyQjoh1Q5oLKRz7Eg450k7RlHXaw 84lP0MSIJhaEW0ubE/oWSOh4+U89XlSuL2asafqk/RsrU3WxqS/JYXtUD5tnHjwal6MVe3DBj 1TdQIgFB54CHDfL0xPCy7xlSsuI9eMNzrQKB29D1RLTVvelKNbraVuC6QKcRzHJ0ZxbXHZjOj H6gYYicY7rEIDbZloSBTcCLc6JEG2MpbRGp/TcsBewtifCAkSd6+9QPVYY6ucjyfUJxzu/BlU mBO4KrPeb8cGL8gCmx8pQdhvzoNI/lYzpXI1A+9td78xfFjZT2yVnBMEmXl0tHqR+nOcx2vA5 VGJuXj6Ykdfmln+B7n/l73I/ayTXZ/Y/eeil/aXYRjE1t6tOBNnvVIxScVJNbBnMiUDYlpvYL 3CfQrg== Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a semantic patch to replace ALLOC_ARRAY+COPY_ARRAY with DUP_ARRAY to reduce code duplication and apply its results. Signed-off-by: René Scharfe --- attr.c | 3 +-- builtin/am.c | 3 +-- commit-graph.c | 3 +-- commit-reach.c | 3 +-- compat/mingw.c | 3 +-- contrib/coccinelle/array.cocci | 7 +++++++ parse-options.c | 3 +-- pathspec.c | 6 ++---- 8 files changed, 15 insertions(+), 16 deletions(-) -- 2.39.0 diff --git a/attr.c b/attr.c index 42ad6de8c7..e4e304a826 100644 --- a/attr.c +++ b/attr.c @@ -599,8 +599,7 @@ struct attr_check *attr_check_dup(const struct attr_check *check) ret->nr = check->nr; ret->alloc = check->alloc; - ALLOC_ARRAY(ret->items, ret->nr); - COPY_ARRAY(ret->items, check->items, ret->nr); + DUP_ARRAY(ret->items, check->items, ret->nr); return ret; } diff --git a/builtin/am.c b/builtin/am.c index dddf1b9af0..eee06bbb6c 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1489,8 +1489,7 @@ static int run_apply(const struct am_state *state, const char *index_file) * apply_opts.v keeps referencing the allocated strings for * strvec_clear() to release. */ - ALLOC_ARRAY(apply_argv, apply_opts.nr); - COPY_ARRAY(apply_argv, apply_opts.v, apply_opts.nr); + DUP_ARRAY(apply_argv, apply_opts.v, apply_opts.nr); opts_left = apply_parse_options(apply_opts.nr, apply_argv, &apply_state, &force_apply, &options, diff --git a/commit-graph.c b/commit-graph.c index a7d8755932..c11b59f28b 100644 --- a/commit-graph.c +++ b/commit-graph.c @@ -1594,8 +1594,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx) _("Computing commit changed paths Bloom filters"), ctx->commits.nr); - ALLOC_ARRAY(sorted_commits, ctx->commits.nr); - COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); + DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); if (ctx->order_by_pack) QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp); diff --git a/commit-reach.c b/commit-reach.c index c226ee3da4..2e33c599a8 100644 --- a/commit-reach.c +++ b/commit-reach.c @@ -245,8 +245,7 @@ static int remove_redundant_with_gen(struct repository *r, * min_gen_pos points to the current position within 'array' * that is not yet known to be STALE. */ - ALLOC_ARRAY(sorted, cnt); - COPY_ARRAY(sorted, array, cnt); + DUP_ARRAY(sorted, array, cnt); QSORT(sorted, cnt, compare_commits_by_gen); min_generation = commit_graph_generation(sorted[0]); diff --git a/compat/mingw.c b/compat/mingw.c index d614f156df..ddc5dd0377 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1396,8 +1396,7 @@ static wchar_t *make_environment_block(char **deltaenv) p += s; } - ALLOC_ARRAY(result, size); - COPY_ARRAY(result, wenv, size); + DUP_ARRAY(result, wenv, size); FreeEnvironmentStringsW(wenv); return result; } diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci index aa75937950..27a3b479c9 100644 --- a/contrib/coccinelle/array.cocci +++ b/contrib/coccinelle/array.cocci @@ -94,3 +94,10 @@ expression n != 1; @@ - ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) ) + CALLOC_ARRAY(ptr, n) + +@@ +expression dst, src, n; +@@ +-ALLOC_ARRAY(dst, n); +-COPY_ARRAY(dst, src, n); ++DUP_ARRAY(dst, src, n); diff --git a/parse-options.c b/parse-options.c index a1ec932f0f..fd4743293f 100644 --- a/parse-options.c +++ b/parse-options.c @@ -702,8 +702,7 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx, if (!nr_aliases) return NULL; - ALLOC_ARRAY(newopt, nr + 1); - COPY_ARRAY(newopt, options, nr + 1); + DUP_ARRAY(newopt, options, nr + 1); /* each alias has two string pointers and NULL */ CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1)); diff --git a/pathspec.c b/pathspec.c index 46e77a85fe..e038481dc4 100644 --- a/pathspec.c +++ b/pathspec.c @@ -681,8 +681,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src) int i, j; *dst = *src; - ALLOC_ARRAY(dst->items, dst->nr); - COPY_ARRAY(dst->items, src->items, dst->nr); + DUP_ARRAY(dst->items, src->items, dst->nr); for (i = 0; i < dst->nr; i++) { struct pathspec_item *d = &dst->items[i]; @@ -691,8 +690,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src) d->match = xstrdup(s->match); d->original = xstrdup(s->original); - ALLOC_ARRAY(d->attr_match, d->attr_match_nr); - COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); + DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); for (j = 0; j < d->attr_match_nr; j++) { const char *value = s->attr_match[j].value; d->attr_match[j].value = xstrdup_or_null(value);