🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #3091

[PATCH] for-each-ref: avoid segfault on --no-format

#3091
From: aeron-gh
Date: Fri, 13 Nov 2026 16:14
70 lines
13063 bytes
The "--format" option is declared with OPT_STRING(), so the
parse-options machinery also accepts "--no-format" and sets the
format string to NULL. The default format is assigned before the
command line is parsed, so nothing puts it back, and the NULL is
dereferenced in verify_ref_format():

    $ git for-each-ref --no-format
    Segmentation fault

"git refs list" shares the code and crashes the same way. The
problem dates back to c3428da87f (Make builtin-for-each-ref.c use
parse-opts., 2007-10-13).

"git branch" and "git tag" have the same option and fall back to
their default format when it is NULL. Do the same here, and assign
the default after parsing the command line when no format is left.
"--no-format" then cancels an earlier "--format" and gives the
default output.

Marking the option with PARSE_OPT_NONEG would also stop the crash,
but then "--no-format" would work for "git branch" and "git tag"
and be an error here.

Signed-off-by: aeron-gh <agab0323@gmail.com>
---
 builtin/for-each-ref.c  | 4 ++--
 t/for-each-ref-tests.sh | 6 ++++++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 4a2fc421db..4390f3f5e9 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -52,14 +52,14 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re
 		OPT_END(),
 	};
 
-	format.format = "%(objectname) %(objecttype)\t%(refname)";
-
 	repo_config(repo, git_default_config, NULL);
 
 	/* Set default (refname) sorting */
 	string_list_append(&sorting_options, "refname");
 
 	parse_options(argc, argv, prefix, opts, usage, 0);
+	if (!format.format)
+		format.format = "%(objectname) %(objecttype)\t%(refname)";
 	if (format.array_opts.max_count < 0) {
 		error("invalid --count argument: `%d'", format.array_opts.max_count);
 		usage_with_options(usage, opts);
diff --git a/t/for-each-ref-tests.sh b/t/for-each-ref-tests.sh
index 4991a82b91..3e0108a7db 100644
--- a/t/for-each-ref-tests.sh
+++ b/t/for-each-ref-tests.sh
@@ -285,6 +285,12 @@ test_expect_success 'Check invalid atoms names are errors' '
 	test_must_fail ${git_for_each_ref} --format="%(INVALID)" refs/heads
 '
 
+test_expect_success '--no-format restores the default format' '
+	${git_for_each_ref} refs/heads >expect &&
+	${git_for_each_ref} --format="%(refname)" --no-format refs/heads >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 	${git_for_each_ref} --format="%(authordate)" refs/heads &&
 	${git_for_each_ref} --format="%(authordate:default) %(authordate)" refs/heads &&
-- 
2.56.0
Technical Details
Message-ID: <20261113161413.15688-1-agab0323@gmail.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-75393-linux-kernel-git=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-75393-linux-kernel-git=dewy.ru
From: agab0323@gmail.com (aeron-gh)
Newsgroups: linux.kernel.git
Subject: [PATCH] for-each-ref: avoid segfault on --no-format
Date: Fri, 13 Nov 2026 21:44:10 +0530
Organization: Usenet Network
Message-ID: <20261113161413.15688-1-agab0323@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1437769"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:eoC8AssdjPUcUF1bBGTVpwdbnQA= sha256:kkWyjUFh5ortOy2H/RL1FOCQ8Q/o5b67GJfaS4+sdfw=
	sha1:NAJ8F9vhZbpdt5Wt6T7VHFX9rPQ= sha256:30+ts/L/aGu986qx1xUWQS01GeWsMmJrBT/N7KHy12I=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id E01A9114675; Sat, 14 Nov 2026 00:03:26 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.948
X-Spam-Status: No, score=-1.948 tagged_above=-9999 required=6.31
	tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id vGtrZqPALTQD;
	Sat, 14 Nov 2026 00:03:19 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-75393-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 3F345114656; Sat, 14 Nov 2026 00:03:10 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id B838A3247EF; Fri, 13 Nov 2026 16:24:46 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1794586750;
	b=JbbszN4HGp1YG+EQklVxBRfgw2NXtO3MHRo9rpReFjClQqFshtkq7EGOJdTV6hVPXlHb
	 w+31EjKErpCCz8ZlovlHz5Kfyn6gx4F5cnZNwHPfo/5hGrKZH1UHVP+XczPYk6K0KQ3Hh
	 KoVWWrduiAJvRxm8ngQlnOyoV4mYMKWdgK6IBsLSACZug7Jqr1P32IDmZ1gseZUwj6ZtI
	 lWIF+EPZjpSS1rcP8jfnoH06uiy7vfx1dJi6CVFXwy5mnwYyn1WzJ/X5lIMlEh5yJhH81
	 8V1JZTL9/cT5jusZL0M3AMOqRy6ln0WzJBjKIgkGNSBAWmDWHMJbrbyV1OKdkEe6MAA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794586750;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding;
	bh=EZY+9WsrKcd2iFF6RoyEx9weEs1VrBOmMp5k6S7lWNc=;
	b=NxzbR7PAAx9/erIz1g4Pg2VZiOy4QryQUcIFLmNN3xReVIAjFKanYce2/3GRfJEVAEmO
	 GXBtMRLTwLqfdrRZkKgO2f1yKBVJkFwmV2Tft1VS9sJvnUJqWd9Au+2JV/h7l5V6ZKy0m
	 swsWg4yK1pWZKxG/qAz09xSHOO3KUkNOjHlkYi261mb14gy43YsZ3EtqRyjImZLOLAWr2
	 vXFI5juH26LWlT+rE+ZTi9LMqKtFWPip72tcnb00VeIHfIPyrcdwOJXS/D1aZVeu2XK4Y
	 7YyiAMuGUlsv+ugfaoNQ+zDPlDnCNwd8qzF05quFJypj2HnUvERx98IMWWaJYAs8D0g==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rxf7DyHY;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B275D606C561;
	Fri, 13 Nov 2026 16:19:10 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rxf7DyHY
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.180
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794586749;
	b=JBbwEgqvKKc57Rog/k15JRFf6A/VAXUO1t6X3jj9tMKsphoSA2WZDjSPUYj+OBovl2RM
	 0o3MNau8ecpXdw94FUpCZYyRX/JjYMfMyWaAMusdnfb7yz+gj4jPzqD1igcnfA61lO4H8
	 n2z2l2pMZnWWMY3l66GmBxamAA3lJ0y9aWmnUTi2i2aFIMTq15hu9g1QDE293/k0Yc67X
	 s03j30bGR1GLZBUbTQ4xKOfrwMKlG7isWefU+NFxHI6ZryLH993s2cI5sA71ccUZmJXTN
	 pLa8T/0OhULE3oA6AyM7LbmP5K3G34q7cPoSwXCXl/qoXhTOOxYuK9BY7hDsSVi8V8g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794586749;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding;
	bh=EZY+9WsrKcd2iFF6RoyEx9weEs1VrBOmMp5k6S7lWNc=;
	b=1cXUQxnJuJxXrfk+G86QVHajb8WKz010WV712s14G/bEADq0Y5caFoU1bNG/3bPd7wia
	 Exiz5GXCgnaPnCEG/VJn+PO9PDYrme8kTnXmXKB70v3BndZJe1QUVSzUDWOiTKkTI51zH
	 k4svWdNSE8i+7fEatwOPxtVB2F+CdaWjUK5sNGUVcVOyAYpNLcF2bLGfaq88u7/+Pft9n
	 wZa1BsV3Jis2ocaEXZSCVXDRjy8MGqD2l/VU+I+H4mewDBzDgvfE6eEDwStioFh2eMRUG
	 v9D+JWYlkcfumw+9c/cqNnz3yyfw+5G6dcX2TBExdGd4/EebSAxppwcdsIrMdrvUIIw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rxf7DyHY;
	arc=none smtp.remote-ip=209.85.210.180
X-Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88CAE607CD0F
	for <git@vger.kernel.org>; Fri, 13 Nov 2026 16:19:09 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88CAE607CD0F
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-8b5c3288e6cso523515b3a.3
        for <git@vger.kernel.org>; Fri, 13 Nov 2026 08:19:09 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1794586749; x=1795191549; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:message-id:date:subject:cc
         :to:from:from:to:cc:subject:date:message-id:reply-to:content-type;
        bh=EZY+9WsrKcd2iFF6RoyEx9weEs1VrBOmMp5k6S7lWNc=;
        b=rxf7DyHYPca3vdi2ZAMaNveN3PhXx7jVY9GYxBddHPve7e6daLPmV7/R2cPrnD6jT6
         E3gYD3xyzt8Jpx/qjCqx8032hX4/wW4MX+3FqMqmdhhv/mMD4yADc4eEi1YbpToHFBUw
         DxsBcgoLD1FsOinVAh8xmQX4exCh8coYhL8gghFca9+yJxamQYGoJmCOx3Ipf0/GtWQK
         nuj6sIQ3vq7tPlznRwgO2B2BCt3z4OjvsfENe6VGuBSBKPviYs0N14Hy9oJISlq9Tso/
         aBS2+zwseUPrsU9De3DbZ6xvnTMbl5ZyAKEc1nHX0a95jJyev7yyRw9daS/73dJwoOvP
         RwxQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1794586749; x=1795191549;
        h=content-transfer-encoding:mime-version:message-id:date:subject:cc
         :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=EZY+9WsrKcd2iFF6RoyEx9weEs1VrBOmMp5k6S7lWNc=;
        b=zt3Wo/ct/tcxazpr+3JPw76Jk5MD8DGZssqQ4BRP0FfTleXB9BoU7zxoGvKjEkPykK
         yJjBfg7oY48mrEcwKvgB3C3xG9vcKz0oPp4iYTzC3z2ygjHWWV72v1pz0cbTkLeUmR8V
         +xjQ7JuE4rodKottkPeTuiU1lVdyS6PRROlNB3FvD/X/1ntPlu7Ei/X0GXJ3l/14koPj
         Z9E0Zhoy/gJ/ICdiuXCuJJjtY+7aZjfo9IvtQWEYVAOSVU5LYvlXHRCFVAdPGo2SwFRs
         rQRkpvyb0jsDjI68sj4e1ZsQEU0et/Eyqx+dI28OFmEIaGoj4uF7bGCZN4GL9S4tokGf
         b6SQ==
X-Gm-Message-State: APuU4Z/gOGFcyNIjulfWGvM3CxxGr0q7J81lGMBcOMAx/4ilpjO1zfdJ
	GFDODe1r7cEx/DqnEnTm64LF4F4OBefR+v4hrFh8LgsqV86s/pr8X0IDZDuMqwz7
X-Gm-Gg: AYBFou030MZUVTRKzCzL7lLTqUv0Avhv4/BlHEQbNXSBMqaxj/IFOJstDGfLlzeB7Xl
	y5R7xjM+TNhAfaFZE1i/8o7ScAftLjWwaEX3pdf/fs5/6V/DDto2sQ+qSgwi4GdaLakr6ZP47Ru
	bbYpCqzqMfrnzkq4d5FS8rlXvvufJ6EfrdcfUBU27c97gqsLoYmuxM7hbM8fkgBy0vMOLUG59hX
	RPU5nFRzyS4L/MnsJ4jATYnyUBh7rqlO4XGuZ/B2I5r9EwhSEf8sokVHbG0HkORDCQtcEPdx26B
	Dn7ZAiUQYdMn9jlC8OdppEN/3DTtkdH6QI97uHZ0S7sYjAmZl/4kTgs4FHb9fgMNlx2uMKZOeR0
	7PaFux1U+iJPhGmGgL9pOHdaZaw6TWZPWAWq7OgP13Zwz8zIr8YXt73mn0WICwiI6FPsL3YlV37
	JZEDbCh3TUGe0Ve2rp693q6ys75uukmPBxASKnoJOLa6e4MLZLfQISk2uYx3roePHcYGQZaD3Up
	TUAN+O5fq9XnbJ0bMaSChKy+z/q/04DgH7nIo6Gl4Qyk3VLaq3bcviajvr+o4jC+hmWQo6T0dm4
	mYcYGqZyzGk+JEXY6QEUo3eEOvbprjU=
X-Received: by 2002:a05:6a00:1746:b0:8a3:23c1:c0e3 with SMTP id d2e1a72fcca58-8beb84e28dfmr2871187b3a.59.1794586748757;
        Fri, 13 Nov 2026 08:19:08 -0800 (PST)
X-Received: from aeron-host ([2401:4900:883d:dce5:aeb5:d73b:40b4:bbdc])
        by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8becbad7de3sm1335932b3a.0.2026.11.13.08.19.06
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 13 Nov 2026 08:19:08 -0800 (PST)
X-Cc: aeron-gh <agab0323@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Usman Akinyemi <usmanakinyemi202@gmail.com>,
	Victoria Dye <vdye@github.com>,
	Meet Soni <meetsoni3017@gmail.com>
X-Mailer: git-send-email 2.56.0
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list