From patchwork Mon Jun 3 09:39:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13683478 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 A8B74824A4 for ; Mon, 3 Jun 2024 09:39:43 +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=1717407585; cv=none; b=ZuWkC/qbcBVt1cH51+X/VfVM3pH1meGcPRo4SzyAz6SSmeI7O/nx8e9qerYx+5NSd5gerFTwpU9iSCZln4N7Ljn6qAo3WRzC6LnD7Gm5J1eizuiGRRJUhlg2BnAbPJZyPnqXmQd20vt1YbdjMERLTdSYPYfPgxUKN1PZieairzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717407585; c=relaxed/simple; bh=ELgg0ATW7II/mIC5XeaiDtZolt5ogbofcLpdXpo1XYk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=l7RYDkjBfQqizSl4THuJq7ufduaVZlV+aqwRAFoe1gc7oDta90Z3AJSuv9arEJhU/WEn7cyykoL5mpeIomt/bnRJrTDL+SPRizfLx+3kvpdvtSmMtz/mInA+hKg+GweniTpplWA8lgACnfrUOODSd53XuXoNDEJcewtLmzHDZ08= 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=eID7er26; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=kbHhJvdJ; 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="eID7er26"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="kbHhJvdJ" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.west.internal (Postfix) with ESMTP id D4C141C0008D; Mon, 3 Jun 2024 05:39:42 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Mon, 03 Jun 2024 05:39: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=1717407582; x=1717493982; bh=Ut7iIkqFzV fPGJvR99bdDyV37gp+WfELv7CJaAJbMXI=; b=eID7er26uOeLX72keet/xODXw0 eCT9ODFHPvZvD8PoPcTYgyCuBsv0u80nb6e9Btr0713o88L1mfV5pjGxMHRpHxVw 1S1ZR9gCj6++xTZLnOM0UApF1dtDrTK/OB2yi+ZFdft1n0aA/1d+W/j4305v8jq6 3Q/YMvI7PzjMWKC9XZ3IzLFlAkhKc3s+GFt65/TtzPS1T6MptB83eLeC2IhwhQhv kvjGlavSwQNwe9JUhF/80L7c714+myV414SOudCB5MVC1uaUB6qB+/dBQo/I+JYz R9yowocvhg2KklbKrcqjhs03TEY5t64+3VNgvWX6m3fTdTvyfxsyZzlKQETA== 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=1717407582; x=1717493982; bh=Ut7iIkqFzVfPGJvR99bdDyV37gp+ WfELv7CJaAJbMXI=; b=kbHhJvdJBhfw0t9HOyD4IuzH1dBphCnBWnwF3pGavJb/ udGzNBluS2fLhPtT6KLTnLRos0c1yssfvi2rM42OZBf7IKsxN+HO/v6NVmZ4AkfZ C//r2ily0VUX1IUNP6+Zn8FELBDtC5YAv5eoQiMKk+RZWOXfTb/0+18wv9s5z5nQ h34wBHqb5PUY7PfAHSOBHrjdDDp1t098MCg3YTDC4w2amctykFwfCmkYO3BCEbfX eaa9NOZi3bEda/GFYoX88g0mmO5J246MTERB9My2MdJSu+paq39m6SxjmRxZNSWh HJ8Hp9FXHQ3ODhU2Ss5nYHNOsVLDHYuCZzCY41waNg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdelvddgudelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehgtd erredttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegke eluddvfeefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 3 Jun 2024 05:39:41 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 413c301b (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 3 Jun 2024 09:39:16 +0000 (UTC) Date: Mon, 3 Jun 2024 11:39:38 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Jeff King , Junio C Hamano Subject: [PATCH v3 09/27] line-log: always allocate the output prefix Message-ID: <03dbdd235b880e65012018a5f6418d23d02f697f.1717402403.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 bd3e663c24..67c80b39a0 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;