From patchwork Fri Jul 26 12:16:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13742735 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0664140E34 for ; Fri, 26 Jul 2024 12:16:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721996211; cv=none; b=oIvrrXpBB8VwgaRVUkybCz96AKXesDTbkyd0pXMvU4be8Ihe10feWM7an+F3CelLeZH5znu/6kY2vdJBT2I2XOmlTAV621p2zBL2fkJoBVHQKVCpYgoy37p9DvBUqvO6vyIFU/S2j61ORZREfokeBo04j9K+/LbdMhCdgnpBma4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721996211; c=relaxed/simple; bh=GSUGuR4biMcDrRRdCcZud78q1/tkig2F/k1lHDkN1Hc=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mQGlmkw1IIT4xjpHBccwLNeYNGzQBSkRKr8LyxsyHsuNmuQ28pd0S2Pxd8wBr3SyI+R+WhqgIlLns2KtocLZ1nUNWAuRbGR+y5cvINLDbwDBumuZoEcneU9s0chWRTlI3OS1vP+r79NLXKwmo1xrHmRkyOvO2VOE6Vy1n/rqfOM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=cjpSPIlT; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Guf6PASS; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="cjpSPIlT"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Guf6PASS" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfout.nyi.internal (Postfix) with ESMTP id 5B22E1380502 for ; Fri, 26 Jul 2024 08:16:48 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Fri, 26 Jul 2024 08:16:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1721996208; x=1722082608; bh=nXEeyZmaW1 pOVsP5vw1xvpi4WPMR64kx+0BZA4V+zcU=; b=cjpSPIlTJDShl94OdMvfcM726M XCzRwfQ3vmh4tz8X+lb2VHVOVe78ZaWOUMpUPgC3wMprWk1CaZDmMxQAR84Cwtow IhltdTVPzcvtjRCOsuqDQ/fQptzze1yYhvi9UygTTWb1QqD6oKuq6GQPW27hcglM Vj3qgeEh8XJZgNsTEU7ExbmjM/Pr4V6HLywwDo7ilvQs8109TqOgZZXDz2mx6iB3 9hOMOD3m/YwYjbR2F3FS8X7cSEOJjZIz0zmEc3Yd19LMdFogoXAYtbpkssUEzrUg g5k1nk9hwDyrMC8XifQnfxmSH/J99lm9tBskHjpn3J2cUl5GSDlySPA/EMjA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1721996208; x=1722082608; bh=nXEeyZmaW1pOVsP5vw1xvpi4WPMR 64kx+0BZA4V+zcU=; b=Guf6PASSpZsqkFR/6hyqdIH7zjhCe/TXSuJIxebkGEN9 UcZ8ErGLqY2w3jJ+FpxZHLamslbreVf11UgQvqdQTHUWSCRnCluNul0/r2IOhCbt 0RBD7XjyWT41w31QxzWKO0dazwSHP+zl44UquqZbkiejK6UpLSPw76h1q7pKoFjP sDP1ppjaulxtUIEIjTezokIuOK1efAzBDtCrybFz4B5b0nh4XLdit+7OYaxpB6ur mWTh87pB+5TqEKvsmKZC2PSxTtHBFfdyPeVBUAmSfeZlZXBKMHlYrFXvT+akvMpk hc0BVI7FnlrtR4a6b9UzM+VzqzS841y/KqQXOP+a1g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrieehgdehudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffukfhfgggtuggjsehgtderre dttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhk shdrihhmqeenucggtffrrghtthgvrhhnpeehgefhtdefueffheekgfffudelffejtdfhvd ejkedthfehvdelgfetgfdvtedthfenucevlhhushhtvghrufhiiigvpedunecurfgrrhgr mhepmhgrihhlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Fri, 26 Jul 2024 08:16:47 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 809716d7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 26 Jul 2024 12:15:26 +0000 (UTC) Date: Fri, 26 Jul 2024 14:16:27 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 12/23] builtin/rev-parse: fix memory leak with `--parseopt` Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `--parseopt` mode allows shell scripts to have the same option parsing mode as we have in C builtins. It soaks up a set of option descriptions via stdin and massages them into proper `struct option`s that we can then use to parse a set of arguments. We only partially free those options when done though, creating a memory leak. Interestingly, we only end up free'ing the first option's help, which is of course wrong. Fix this by freeing all option's help fields as well as their `argh` fielids to plug this memory leak. Signed-off-by: Patrick Steinhardt --- builtin/rev-parse.c | 5 ++++- t/t1502-rev-parse-parseopt.sh | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 2e64f5bda7..5845d3f59b 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -553,7 +553,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) strbuf_release(&sb); strvec_clear(&longnames); strvec_clear(&usage); - free((char *) opts->help); + for (size_t i = 0; i < opts_nr; i++) { + free((char *) opts[i].help); + free((char *) opts[i].argh); + } free(opts); return 0; } diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh index b754b9fd74..5eaa6428c4 100755 --- a/t/t1502-rev-parse-parseopt.sh +++ b/t/t1502-rev-parse-parseopt.sh @@ -1,6 +1,8 @@ #!/bin/sh test_description='test git rev-parse --parseopt' + +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh check_invalid_long_option () {