From patchwork Thu Aug 8 13:05:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13757434 Received: from fout7-smtp.messagingengine.com (fout7-smtp.messagingengine.com [103.168.172.150]) (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 9CC56EEA9 for ; Thu, 8 Aug 2024 13:05:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.150 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723122313; cv=none; b=n3RjvEDA0EUMumV9hCd14ntYWd1H+KgnVBQ31wrS+ROJSg4/2TUNgMlqLnkrepoI+fKIlZkUOzUBoI1UIz1mqiLfydJXcUC6fLuRLk+v4qfNpk4DBTraN06cByfqFpVugmNsDXMKhUpvUeuoufntfrdYMqX6dVvDpW2qV6FIUao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723122313; c=relaxed/simple; bh=oDLchzGe7Ee4MU7C6VSnikCZtYxYgD+7hYI1cRC7Xlg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=md9Yb78oU6JFfITMSf325DMb8BPh1Bwl5YfVeNVOsO+mLW0eh52sGRre8vdUFDpjMsWYg1n/zf27kRi4TP9bwBFnde8+olBXtKyKACWCy0fr8yFTlNpBVNT0lbiobwCESdLjTac6pkBIagtFB/OlqtpzfVWpByqMWjvrx6eM1DQ= 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=LF5imQVY; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=QzSNHVYh; arc=none smtp.client-ip=103.168.172.150 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="LF5imQVY"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="QzSNHVYh" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfout.nyi.internal (Postfix) with ESMTP id C814F138FC70; Thu, 8 Aug 2024 09:05:10 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Thu, 08 Aug 2024 09:05:10 -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=1723122310; x=1723208710; bh=8+nj5Y/mx9 6wWp0Ob8myx6K1tynKti+ImBJhz0/x9Ag=; b=LF5imQVYFr04KXmUwtKOv6bLCO xaJSJ6J4SUbAuENZ+c+xOZ7ODSO5/N8+TAWdaqrJNlq3m0lj/cEoG3T/c9uIBjsw cVfE9l3bmNwjT3XLOTRpduvrh3VQImYCfKJMqz8vJVOW0GlO8WZ+N/8Ox15QFcGr n0tkwNTEZzdqTp56O4j+GkhT8eF3LXTbijXmPBOaQMRWqDHONmuJqXFmTgvGh/rF 2+VbKUb+msyijpC/hWIGorO46NVkiuaHanqHkq8sSTp1WWLx1Osr+RRQ0uqCPyM/ UgjraXqMACi0MjIiWgcxlzUUDc33/a8bp5INlhytbmh54MQPm5FEQ/DaIQNQ== 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=1723122310; x=1723208710; bh=8+nj5Y/mx96wWp0Ob8myx6K1tynK ti+ImBJhz0/x9Ag=; b=QzSNHVYh2Fqfprv0/YBtvJjaA+dXuqFlq8AYniNwzZF5 AfHFX0EGi2V9mXgeEfusKzwEXPB0rjpkr5KFlWySR98xw2zhMkfSJbGTguYQF5tC vRtXai4jxqcQ9jzIW6mJ9fwczDjqRfUCyM0mvhLf2Y4RZZFrj5RuSyRkOVgOD9Mb aYz45M/eq3I2DX2ebYA6nJrLe2zv6YI/MyGHUIEv71f1yOrax/dzp5CdZKTKCr3W zcH/JHeyB2efErtt1LByOj0E/jHNYT864+QOXIfyjb1Lbs0vWNHBeB8oZueXh/VK yPapWC2wmEXQXRrlUfIkYF4poJcmJ+diQ0srjaLQhw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrledvgdeitdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecu hfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqe enucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeeh gfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedunecurfgrrhgrmhepmhgrih hlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopeefpdhmohguvgepshhm thhpohhuthdprhgtphhtthhopehjrghmvghssehjrghmvghslhhiuhdrihhopdhrtghpth htohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehphhhilhhl ihhprdifohhougduvdefsehgmhgrihhlrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 8 Aug 2024 09:05:09 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id a16f6a9b (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 8 Aug 2024 13:05:02 +0000 (UTC) Date: Thu, 8 Aug 2024 15:05:07 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: James Liu , Phillip Wood Subject: [PATCH v2 08/22] config: fix leaking comment character config Message-ID: <8fbd72a1002d1a285847c62b5524041927a7b4d4.1723121979.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: When the comment line character has been specified multiple times in the configuration, then `git_default_core_config()` will cause a memory leak because it unconditionally copies the string into `comment_line_str` without free'ing the previous value. In fact, it can't easily free the value in the first place because it may contain a string constant. Refactor the code such that we track allocated comment character strings via a separate non-constant variable `comment_line_str_allocated`. Adapt sites that set `comment_line_str` to set both and free the old value that was stored in `comment_line_str_allocated`. This memory leak is being hit in t3404. As there are still other memory leaks in that file we cannot yet mark it as passing with leak checking enabled. Signed-off-by: Patrick Steinhardt --- builtin/commit.c | 7 +++++-- config.c | 4 +++- environment.c | 1 + environment.h | 1 + 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 66427ba82d..025b1c4686 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -684,7 +684,9 @@ static void adjust_comment_line_char(const struct strbuf *sb) const char *p; if (!memchr(sb->buf, candidates[0], sb->len)) { - comment_line_str = xstrfmt("%c", candidates[0]); + free(comment_line_str_allocated); + comment_line_str = comment_line_str_allocated = + xstrfmt("%c", candidates[0]); return; } @@ -705,7 +707,8 @@ static void adjust_comment_line_char(const struct strbuf *sb) if (!*p) die(_("unable to select a comment character that is not used\n" "in the current commit message")); - comment_line_str = xstrfmt("%c", *p); + free(comment_line_str_allocated); + comment_line_str = comment_line_str_allocated = xstrfmt("%c", *p); } static void prepare_amend_commit(struct commit *commit, struct strbuf *sb, diff --git a/config.c b/config.c index 6421894614..cb78b652ee 100644 --- a/config.c +++ b/config.c @@ -1596,7 +1596,9 @@ static int git_default_core_config(const char *var, const char *value, else if (value[0]) { if (strchr(value, '\n')) return error(_("%s cannot contain newline"), var); - comment_line_str = xstrdup(value); + free(comment_line_str_allocated); + comment_line_str = comment_line_str_allocated = + xstrdup(value); auto_comment_line_char = 0; } else return error(_("%s must have at least one character"), var); diff --git a/environment.c b/environment.c index 5cea2c9f54..1a95798d5f 100644 --- a/environment.c +++ b/environment.c @@ -114,6 +114,7 @@ int protect_ntfs = PROTECT_NTFS_DEFAULT; * that is subject to stripspace. */ const char *comment_line_str = "#"; +char *comment_line_str_allocated; int auto_comment_line_char; /* Parallel index stat data preload? */ diff --git a/environment.h b/environment.h index e9f01d4d11..0e0906f125 100644 --- a/environment.h +++ b/environment.h @@ -9,6 +9,7 @@ struct strvec; * that is subject to stripspace. */ extern const char *comment_line_str; +extern char *comment_line_str_allocated; extern int auto_comment_line_char; /*