From patchwork Fri Aug 7 07:05:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 11704967 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 12697175A for ; Fri, 7 Aug 2020 07:06:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BF64D20866 for ; Fri, 7 Aug 2020 07:06:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="raqF7Iuj"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="I11BhHk1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726401AbgHGHGE (ORCPT ); Fri, 7 Aug 2020 03:06:04 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:37891 "EHLO out1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725805AbgHGHGE (ORCPT ); Fri, 7 Aug 2020 03:06:04 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id B6AAC5C01E4 for ; Fri, 7 Aug 2020 03:06:02 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 07 Aug 2020 03:06:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:subject:message-id:mime-version:content-type; s=fm1; bh=388xvlQIGgD2MtKKcv7albsz5H75FohzfK/5xpsbSHk=; b=raqF7Iuj5LDh IOxBLFJu9A90jtCvdoF6ufSDZRtonm+Tpo2v37/3JXA5WazbTXaf06gRbnhUVB7+ z68z8aZmUa5v2cqLmtAPYV+FedwyJLkM1555YUmbyZpsbM9kQIwvqkOj/ClkhnO8 /8jLQWQf9OKj9DCUuGWKAcqswuGBqHq+D/3LYzDLPbRWfqGIxvG2SPeyMAEbySSi 6McLkuEoP3pVdgvd8jdwiZyJjp+XV61eXSjT/x2XSBgSlpCGvcQLe1R6rgeZnPnp yaQiF8nqBtWwdnTGkOElkKlRhtL/gAblTLY6IlY/FR6nfs4oQ6jIQ0uRrGnHpzqN OJOTcMD80Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=388xvlQIGgD2MtKKcv7albsz5H75F ohzfK/5xpsbSHk=; b=I11BhHk1mzTB6lBJs7M05GLSWIqYyPF+/1ZkkvPBPCZ0z k1PmH8wIe5WHyLxr3NYmi6Pkw9udqyLOnNe/aC1RkFOxFNNT5b006vmBwsfC3vce sWaCtP+xkI+UcyyqmyZ6YV+fHp93hvQfVHb4zNLQOIMTPJ69bLaAqJheEJ1yyu36 vz2Z/HEmHbR+PhsZR1rMXt+pkHV7rB/19WlFGpUcSt0+Z+DjkYTAuGq5fPvgCttZ dU2fhRTfQF5aILQPgAZCwVBy/d/yogWMTozVLbnDIQ/4OUg97TiIJbjWaAXiWlir akvnskawDay89mcdh8MItcWOiX2v7LCVzEJehNgqA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrkedugdduudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkgggtugesghdtreertd dtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhs rdhimheqnecuggftrfgrthhtvghrnhepjeeifedvueelfffgjeduffdvgefhiefgjefgvd dvfeduvefffeevfffhgfekieffnecukfhppeejjedrudekfedrvdeirddugedtnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkhhsrd himh X-ME-Proxy: Received: from vm-mail.pks.im (x4db71a8c.dyn.telefonica.de [77.183.26.140]) by mail.messagingengine.com (Postfix) with ESMTPA id CE82130600A3 for ; Fri, 7 Aug 2020 03:06:01 -0400 (EDT) Received: from localhost (tanuki [10.192.0.23]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id ac177629 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 7 Aug 2020 07:05:59 +0000 (UTC) Date: Fri, 7 Aug 2020 09:05:58 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH] refs: fix interleaving hook calls with reference-transaction hook Message-ID: <63fb363375b515b903ed1269d10124b727c1d1cc.1596783732.git.ps@pks.im> MIME-Version: 1.0 Content-Disposition: inline Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In order to not repeatedly search for the reference-transaction hook in case it's getting called multiple times, we use a caching mechanism to only call `find_hook()` once. What was missed though is that the return value of `find_hook()` actually comes from a static strbuf, which means it will get overwritten when calling `find_hook()` again. As a result, we may call the wrong hook with parameters of the reference-transaction hook. This scenario was spotted in the wild when executing a git-push(1) with multiple references, where there are interleaving calls to both the update and the reference-transaction hook. While initial calls to the reference-transaction hook work as expected, it will stop working after the next invocation of the update hook. The result is that we now start calling the update hook with parameters and stdin of the reference-transaction hook. This commit fixes the issue by storing a copy of `find_hook()`'s return value in the cache. --- refs.c | 2 +- t/t1416-ref-transaction-hooks.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/refs.c b/refs.c index 2dd851fe81..17e515b288 100644 --- a/refs.c +++ b/refs.c @@ -2044,7 +2044,7 @@ static int run_transaction_hook(struct ref_transaction *transaction, if (hook == &hook_not_found) return ret; if (!hook) - hook = find_hook("reference-transaction"); + hook = xstrdup_or_null(find_hook("reference-transaction")); if (!hook) { hook = &hook_not_found; return ret; diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index da58d867a5..d4d19194bf 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -106,4 +106,30 @@ test_expect_success 'hook gets all queued updates in aborted state' ' test_cmp expect actual ' +test_expect_success 'interleaving hook calls succeed' ' + test_when_finished "rm -r target-repo.git" && + + git init --bare target-repo.git && + + write_script target-repo.git/hooks/reference-transaction <<-\EOF && + echo $0 "$@" >>actual + EOF + + write_script target-repo.git/hooks/update <<-\EOF && + echo $0 "$@" >>actual + EOF + + cat >expect <<-EOF && + hooks/update refs/tags/PRE 0000000000000000000000000000000000000000 63ac8e7bcdb882293465435909f54a96de17d4f7 + hooks/reference-transaction prepared + hooks/reference-transaction committed + hooks/update refs/tags/POST 0000000000000000000000000000000000000000 99d53161c3a0a903b6561b9f6c0c665b3a476401 + hooks/reference-transaction prepared + hooks/reference-transaction committed + EOF + + git push ./target-repo.git PRE POST && + test_cmp expect target-repo.git/actual +' + test_done