From patchwork Fri May 24 10:03:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13672989 Received: from fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (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 2FC7583CCC for ; Fri, 24 May 2024 10:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.153 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716545033; cv=none; b=NdoWkPyk9ibJHkOEYvdyKMi1e4VP4NGX55PL2tYWE0jhVkqlqSML2eJ2/4341lRMYugDdG10rVtp52uCg2nJzOMoIbbKcgTsFIN6/tSQveR53qbcfvenU56fxNe9wtujJwYfP419k7jDi/4ceUXROLPEf1KZGdJvIXBm+U3CPQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716545033; c=relaxed/simple; bh=8tLZkJDudCljQJVhwme5ixZcwqXiZuCg9ZbeLz2hx0c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=prj3zx5Lnt8g2NaA29XsiFDP/waWEblgqKyVdEAHsOGN7/RMw6hHSJ4NiGFfhNHWVSWR94a7Kfts+RRsXHazPACyap+W45O14yaiHgbqwPHINNsKVLnsmLmMyMi7wZVTU/ZFK+NyPtLfoEcIl9DA1eTh4WT7T815Hs1JOSw8080= 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=L+tzJ9wr; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Vs7TGSEm; arc=none smtp.client-ip=103.168.172.153 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="L+tzJ9wr"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Vs7TGSEm" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 59E4711400C2; Fri, 24 May 2024 06:03:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Fri, 24 May 2024 06:03:51 -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=fm1; t=1716545031; x=1716631431; bh=+xatpJPETN vmonEK9zl9VQW/uwzRSdrrRd57nKF/zuM=; b=L+tzJ9wrVp6UcayWBM3h+1ksrQ qp8s0ai94emR71vJneDiC8YMsHzzuWJ//RI3RgvPm9Bh3Th11rsvqoizFbM1EiL+ wq3deJ0r0oBXcumd7uVNvSBq2mW0oNJ4JEbJZnClb175saw1rYKWYY7RjdFy59bU bpOtSnNU6c6VRYbCmkLtS6nXZ0efuGw1EYWN7BXZG+WFby+c9rW7Hsi+SEjeiqA8 v/dODYriyq7LBhMeyjW8NKqDfG1Y5rGcVyr+mVdKNxm3GgQpTkoskBeTiRuEJi0p H5ZgH6q+N9v9D4PrTRi0L4dCiKJXoKlKiLjcUFERTwkPCgQMrwon1hRX9K9g== 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= fm1; t=1716545031; x=1716631431; bh=+xatpJPETNvmonEK9zl9VQW/uwzR SdrrRd57nKF/zuM=; b=Vs7TGSEm2o8rBCkGDFb1G7+BpKsOJI3DiuDzRLMIFfMQ NiElJjAXnLsQB605EaGQtd1nEBlcEpUNbAd1GtNwlTVM8HSVI+MnyHVNj0oC6osn wR4HyASTGQYInmyTO0b2glihtBwD6oz9Ws0CAaOEN/qOitaTCkLm9uziOT+yRrll TcvvWDiWwwjqNcJytlo5in0ou9QF6B3nt38ipLLVlivAUwak0uR+JMosfcTCkjBx TSyr6hqKDPrVGmmb4b8C1/svcNKqc9SGVoIADfGpUAJ4uHDMhIvfpSIS3PiiBjDO usOScayJPqIWRfoiTqzktclPxCw1n4lpIbhZYY4Kvg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeikedgvdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 24 May 2024 06:03:50 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id d356ff31 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 24 May 2024 10:03:47 +0000 (UTC) Date: Fri, 24 May 2024 12:03:48 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Eric Sunshine , Junio C Hamano Subject: [PATCH v2 08/21] diff: refactor code to clarify memory ownership of prefixes 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 source and destination prefixes are tracked in a `const char *` array, but may at times contain allocated strings. The result is that those strings may be leaking because we never free them. Refactor the code to always store allocated strings in those variables, freeing them as required. This requires us to handle the default values a bit different compared to before. But given that there is only a single callsite where we use the variables to `struct diff_options` it's easy to handle the defaults there. Signed-off-by: Patrick Steinhardt --- diff.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/diff.c b/diff.c index 902df9286a..679ef472f4 100644 --- a/diff.c +++ b/diff.c @@ -62,8 +62,8 @@ static char *diff_order_file_cfg; int diff_auto_refresh_index = 1; static int diff_mnemonic_prefix; static int diff_no_prefix; -static const char *diff_src_prefix = "a/"; -static const char *diff_dst_prefix = "b/"; +static char *diff_src_prefix; +static char *diff_dst_prefix; static int diff_relative; static int diff_stat_name_width; static int diff_stat_graph_width; @@ -411,10 +411,12 @@ int git_diff_ui_config(const char *var, const char *value, return 0; } if (!strcmp(var, "diff.srcprefix")) { - return git_config_string(&diff_src_prefix, var, value); + FREE_AND_NULL(diff_src_prefix); + return git_config_string((const char **) &diff_src_prefix, var, value); } if (!strcmp(var, "diff.dstprefix")) { - return git_config_string(&diff_dst_prefix, var, value); + FREE_AND_NULL(diff_dst_prefix); + return git_config_string((const char **) &diff_dst_prefix, var, value); } if (!strcmp(var, "diff.relative")) { diff_relative = git_config_bool(var, value); @@ -3433,8 +3435,8 @@ void diff_set_noprefix(struct diff_options *options) void diff_set_default_prefix(struct diff_options *options) { - options->a_prefix = diff_src_prefix; - options->b_prefix = diff_dst_prefix; + options->a_prefix = diff_src_prefix ? diff_src_prefix : "a/"; + options->b_prefix = diff_dst_prefix ? diff_dst_prefix : "b/"; } struct userdiff_driver *get_textconv(struct repository *r, @@ -5371,8 +5373,8 @@ static int diff_opt_default_prefix(const struct option *opt, BUG_ON_OPT_NEG(unset); BUG_ON_OPT_ARG(optarg); - diff_src_prefix = "a/"; - diff_dst_prefix = "b/"; + FREE_AND_NULL(diff_src_prefix); + FREE_AND_NULL(diff_dst_prefix); diff_set_default_prefix(options); return 0; }