From patchwork Wed May 29 12:44:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13678795 Received: from wfout4-smtp.messagingengine.com (wfout4-smtp.messagingengine.com [64.147.123.147]) (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 436FC142E68 for ; Wed, 29 May 2024 12:44:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.147 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716986685; cv=none; b=uPKJFllnDbw76T4JkhrrNSo6zt15P2P7hWw1QO9IRzYxXiaDuTgsU7ICJM7DYf1ri2V8p70UvbMcX2ckDsnB0BQlKQMIWBE1JkRLk0u7GWwzPnqK3W50d5ZAUBGX9eGXrTODYs5sVeBY+jmMLHvpXsJ8FS5uxRvF3zUv/aFDXnY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716986685; c=relaxed/simple; bh=LWysdPrqul4FEP4uujm1303OdwEcW9zTO43kk4Bsma0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=s7BQilURQEMvHsDIGZsrf+jG3W4N9jpQqP7roEMrrSglMyQOxu2ORrEaWi8qNGHsZ6wB+GolYyGOQ89LABFYjkQUbiGI8UOCMuYOeSpt93bajDBDK6HuYJS6a0ZlSqxzTWzsKAoBsNFSGhcO0V8fHLRoBt+ukcEgZFLXPmmPG+c= 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=TZrI0dQ/; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Cg+jmoqk; arc=none smtp.client-ip=64.147.123.147 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="TZrI0dQ/"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Cg+jmoqk" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id B0DFE1C0012A; Wed, 29 May 2024 08:44:43 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Wed, 29 May 2024 08:44:43 -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=1716986683; x=1717073083; bh=LQCUNOKyiW 3UeoGMH2D0OfhNBNI+svjMOYkeJnQEQuA=; b=TZrI0dQ/OzRuyyLF+az4Kt/r2o WoaAsjb2GeLZEA52tGQdjBystkoAUOxadrXE/jAI69uWffPFKp+IggQIoexvAEnf lGlYQG/JxGdHnTEqCCIHkK3KpNrNxoAORhBUtfWA+aVOjak71YycwZ7FC26QLT+R mhORacrWyTxNNBuecEplxDwK/y92OGEjwEHItAXmpHJF9USaFZIVdzSIeIYgMxv9 E2TGmsGEEoMGLWMCvF9MICLBn3zJzy98fyNh6M4LnTG4mokbypPONApzVrT5Da6f YAheFBpCDTjbT8NVzWSzDiTFg0E0gX7a3b95WguOSwGisvLg8gLnAtZ7xzaQ== 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=1716986683; x=1717073083; bh=LQCUNOKyiW3UeoGMH2D0OfhNBNI+ svjMOYkeJnQEQuA=; b=Cg+jmoqkcPGcfHHDZEQfeGDWO2l+DT1c91nxFv9yqPDp vC9LCt+ZvfHFlaXVhGZf+8fmZShB22sCuZoDL28Ha0diAWTWngxLB0DxOn24oYlM X6XuBTCCma7V0jhERe/0Gh/A2gidJkIGCWlaDBCs4Sc5nuTpsA1k+q7P0elL8cZT 6o6YldZgqCICd1cN/7I2r/nHlxaXs8XPlvKWrfOStkjbe9LIi/Eu8XaPGEa+UFmB 7tAFV3VwJAnKuQJdEPu6rDV6mCQw5JWeKMvZHH/SL00euwGA8Yrvg/FWTUUwbNu2 mz+tzgXfy9/otQRdL+eC++wVUuum68ktRxriWH8fPg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdekuddgheegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehgtd erredttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegke eluddvfeefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedunecurfgr rhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 29 May 2024 08:44:42 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 61639a25 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 29 May 2024 12:44:27 +0000 (UTC) Date: Wed, 29 May 2024 14:44:39 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH 08/19] line-log: always allocate the output prefix Message-ID: <699eeae92c0f58032fc76af68521d8cc60f12031.1716983704.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 returned string by `output_prefix()` is sometimes a string constant and sometimes an allocated string. This has been fine until now because we always leak the allocated strings, and thus we never tried to free the string constant. Fix the code to always return an allocated string and free the returned value at all callsites. Signed-off-by: Patrick Steinhardt --- line-log.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/line-log.c b/line-log.c index d9bf2c8120..9a298209d0 100644 --- a/line-log.c +++ b/line-log.c @@ -899,14 +899,12 @@ static void print_line(const char *prefix, char first, static char *output_prefix(struct diff_options *opt) { - char *prefix = ""; - if (opt->output_prefix) { struct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data); - prefix = sb->buf; + return sb->buf; + } else { + return xstrdup(""); } - - return prefix; } static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range) @@ -927,7 +925,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang const char *c_context = diff_get_color(opt->use_color, DIFF_CONTEXT); if (!pair || !diff) - return; + goto out; if (pair->one->oid_valid) fill_line_ends(rev->diffopt.repo, pair->one, &p_lines, &p_ends); @@ -1002,8 +1000,10 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang c_context, c_reset, opt->file); } +out: free(p_ends); free(t_ends); + free(prefix); } /* @@ -1012,7 +1012,11 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang */ static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range) { - fprintf(rev->diffopt.file, "%s\n", output_prefix(&rev->diffopt)); + char *prefix = output_prefix(&rev->diffopt); + + fprintf(rev->diffopt.file, "%s\n", prefix); + free(prefix); + while (range) { dump_diff_hacky_one(rev, range); range = range->next;