From patchwork Thu Aug 1 10:40:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13750137 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 1EA8A170A37 for ; Thu, 1 Aug 2024 10:40:25 +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=1722508827; cv=none; b=f6CTp8K3LzJvhtDf9UzbbYAyWR3/lyRD6cLx3CUqMybsQIn5MxQ1BAQAkyjHDISCunWwJkh7UTkKEjExkjwgnnDSpj7b96Gyy5C1hZ5TnjFG17KJvRT6mJN6duRD0cr9PTrTpEQVwPlf2/bmFPMm7DXFI2e5E1dQLcqzJEoq2BM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722508827; c=relaxed/simple; bh=MsyvqIJImmYrk/N/gbvJ+kfwNtmF/pDzCtTaMyuhKIY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Rs+fSmQhtJ6RZobtdPAproXxDz9sxmKCVSuNJXDF6uMnGa60hfv/NpV0pz7O4lRPJabzTbd9MRgzAbr+ALFwfKkwVT8n9ccz+olFlUczGgfnWtBDX2IKEIzPcLr2K+OZR02mkrJbx7Fd0W/u3Q2J9TsJDzG6a36JFwwSyOcuDWY= 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=PK8OSMx2; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=NyU5wdOH; 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="PK8OSMx2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="NyU5wdOH" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 23CBE1388015; Thu, 1 Aug 2024 06:40:25 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 01 Aug 2024 06:40:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc: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=1722508825; x=1722595225; bh=Lz6+bQErON SkVnPLEu/e0DC6ehcFzq3A+MhfAMl1RQs=; b=PK8OSMx2ATHL8Jfgt5NubUFDt3 dJtL7QYz8oVTD8Qusy5qf8PrjjCTROY6achtNtPQD45uXFTe2CTmlpOknYCmfHkB fdRoRxbRIOJpUhRW/tLN/WM80Yp6veQiaC7JdguBavxZFk/FqTKYg+U7SFBjUHiW rB2w3XvukMqQynem+W6dubs8WWP3/kRP7rLK9ye9m4bsvM4pgyC0lL52Xqzh1SFB mexgDgakU9I1LM3tP+P8wGG0WqZ3no1Haq33ZWHhs8YEt3OCyQsoiqPGJcl23FSo QqGnIOK4LhUPOFSNT8SLZMZu0rV23odRiroK9zcwHKC7jCEpcY0jErYvktEQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1722508825; x=1722595225; bh=Lz6+bQErONSkVnPLEu/e0DC6ehcF zq3A+MhfAMl1RQs=; b=NyU5wdOH8czmHvH45IMxmo977kD+lw2IVBypHoGu+FnL nn4BUL1vMs2ZZ781+r9a8qSy7EvUOzrrx8ZyA76KozCZQBnoh2cizLetbFpG7Xw+ VRwMKnw6A2BduBeLX/0BNipvR2hcmgltR2pJREQyJSRuKuLoGbsXg58kgvRH8PqV /d2sJNWZkr5Jye19szoHcI9vf+vbrWboMF5OWKt/8rY46A7aElPWANcx6aTcix9n 44bpdhseuNNJsAsOZJM/pu4EHWmwAVAil/teRbB7skLe39p38g5wOKWn0UmpHhB0 up4FNUNDzGKfIGmED7T6oOzj5Yx7jOux3oSkZIcHcQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrjeekgdefudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleffteen ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesph hkshdrihhmpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 1 Aug 2024 06:40:23 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 712c7e06 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 1 Aug 2024 10:38:52 +0000 (UTC) Date: Thu, 1 Aug 2024 12:40:21 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Karthik Nayak , Taylor Blau , Junio C Hamano , =?utf-8?b?UnViw6lu?= Justo Subject: [PATCH v2 13/24] builtin/rev-parse: fix memory leak with `--parseopt` Message-ID: <034c416d4693701cecc3201b9f8d9839fd99c1ab.1722499961.git.ps@pks.im> 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` fields 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 () {