Git development
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Collin Funk <collin.funk1@gmail.com>, Michael J Gruber <git@grubix.eu>
Subject: [PATCH 09/12] pkt-line: make packet_reader.line non-const
Date: Tue, 31 Mar 2026 19:51:36 -0400	[thread overview]
Message-ID: <20260331235136.GI2328529@coredump.intra.peff.net> (raw)
In-Reply-To: <20260331233856.GA2327197@coredump.intra.peff.net>

The "line" member of a packet_reader struct is marked as const. This
kind of makes sense, because it's not its own allocated buffer that
should be freed, and we often use const to indicate that. But it is
always writable, because it points into the non-const "buffer" member.

And we rely on this writability in places like send-pack and
receive-pack, where we parse incoming packet contents by writing NULs
over delimiters. This has traditionally worked because we implicitly
cast away the constness with strchr() like:

  const char *head;
  char *p;

  head = reader->line;
  p = strchr(head, ' ');

Since C23 libc provides a generic strchr() to detect this implicit
const removal, this now generate a compiler warning on some platforms
(like recent glibc).

We can fix it by marking "line" as non-const, as well as a few
intermediate variables (like "head" in the above example). Note that by
itself, switching to a non-const variable would cause problems with this
line in send-pack.c:

  if (!skip_prefix(reader->line, "unpack ", &reader->line))

But due to our skip_prefix() magic introduced in the previous commit,
this compiles fine (both the in and out-parameters are non-const, so we
know it is safe).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/receive-pack.c | 7 ++++---
 pkt-line.h             | 2 +-
 send-pack.c            | 7 ++++---
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index e34edff406..a6af16c4e7 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1025,8 +1025,8 @@ static int read_proc_receive_report(struct packet_reader *reader,
 
 	for (;;) {
 		struct object_id old_oid, new_oid;
-		const char *head;
-		const char *refname;
+		char *head;
+		char *refname;
 		char *p;
 		enum packet_read_status status;
 
@@ -1050,7 +1050,8 @@ static int read_proc_receive_report(struct packet_reader *reader,
 		}
 		*p++ = '\0';
 		if (!strcmp(head, "option")) {
-			const char *key, *val;
+			char *key;
+			const char *val;
 
 			if (!hint || !(report || new_report)) {
 				if (!once++)
diff --git a/pkt-line.h b/pkt-line.h
index 3b33cc64f3..e6cf85e34e 100644
--- a/pkt-line.h
+++ b/pkt-line.h
@@ -184,7 +184,7 @@ struct packet_reader {
 	int pktlen;
 
 	/* the last line read */
-	const char *line;
+	char *line;
 
 	/* indicates if a line has been peeked */
 	int line_peeked;
diff --git a/send-pack.c b/send-pack.c
index 07ecfae4de..b4361d5610 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -175,8 +175,8 @@ static int receive_status(struct repository *r,
 	ret = receive_unpack_status(reader);
 	while (1) {
 		struct object_id old_oid, new_oid;
-		const char *head;
-		const char *refname;
+		char *head;
+		char *refname;
 		char *p;
 		if (packet_reader_read(reader) != PACKET_READ_NORMAL)
 			break;
@@ -190,7 +190,8 @@ static int receive_status(struct repository *r,
 		*p++ = '\0';
 
 		if (!strcmp(head, "option")) {
-			const char *key, *val;
+			char *key;
+			const char *val;
 
 			if (!hint || !(report || new_report)) {
 				if (!once++)
-- 
2.53.0.1136.gd760fbd4a0


  parent reply	other threads:[~2026-03-31 23:51 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-31 23:38 [PATCH 0/12] fixing the remainder of the C23 strchr warnings Jeff King
2026-03-31 23:41 ` [PATCH 01/12] convert: add const to fix strchr() warnings Jeff King
2026-03-31 23:41 ` [PATCH 02/12] http: " Jeff King
2026-03-31 23:41 ` [PATCH 03/12] transport-helper: drop " Jeff King
2026-04-01 13:46   ` Patrick Steinhardt
2026-03-31 23:42 ` [PATCH 04/12] pager: explicitly cast away strchr() constness Jeff King
2026-04-01 20:50   ` Junio C Hamano
2026-04-02  3:54     ` Jeff King
2026-03-31 23:42 ` [PATCH 05/12] run-command: explicitly cast away constness when assigning to void Jeff King
2026-03-31 23:44 ` [PATCH 06/12] find_last_dir_sep(): convert inline function to macro Jeff King
2026-03-31 23:46 ` [PATCH 07/12] pseudo-merge: fix disk reads from find_pseudo_merge() Jeff King
2026-03-31 23:56   ` Jeff King
2026-04-01 21:40     ` Junio C Hamano
2026-04-02 23:51     ` Taylor Blau
2026-03-31 23:50 ` [PATCH 08/12] skip_prefix(): check const match between in and out params Jeff King
2026-04-01 13:17   ` Phillip Wood
2026-04-01 14:04     ` Phillip Wood
2026-04-01 19:24       ` Jeff King
2026-04-01 22:13         ` Junio C Hamano
2026-04-02 15:05         ` Phillip Wood
2026-04-01 13:46   ` Patrick Steinhardt
2026-03-31 23:51 ` Jeff King [this message]
2026-04-01 22:18   ` [PATCH 09/12] pkt-line: make packet_reader.line non-const Junio C Hamano
2026-04-02  3:55     ` Jeff King
2026-03-31 23:52 ` [PATCH 10/12] range-diff: drop const to fix strstr() warnings Jeff King
2026-03-31 23:52 ` [PATCH 11/12] http: drop const to fix strstr() warning Jeff King
2026-03-31 23:53 ` [PATCH 12/12] refs/files-backend: drop const to fix strchr() warning Jeff King
2026-04-01 13:46   ` Patrick Steinhardt
2026-04-01 22:22     ` Junio C Hamano
2026-04-02  3:56       ` Jeff King
2026-04-02  4:14 ` [PATCH v2 0/12] fixing the remainder of the C23 strchr warnings Jeff King
2026-04-02  4:14   ` [PATCH v2 01/12] convert: add const to fix strchr() warnings Jeff King
2026-04-02  4:14   ` [PATCH v2 02/12] http: " Jeff King
2026-04-02  4:14   ` [PATCH v2 03/12] transport-helper: drop " Jeff King
2026-04-02  4:14   ` [PATCH v2 04/12] pager: explicitly cast away strchr() constness Jeff King
2026-04-02  4:15   ` [PATCH v2 05/12] run-command: explicitly cast away constness when assigning to void Jeff King
2026-04-02  4:15   ` [PATCH v2 06/12] find_last_dir_sep(): convert inline function to macro Jeff King
2026-04-02  4:15   ` [PATCH v2 07/12] pseudo-merge: fix disk reads from find_pseudo_merge() Jeff King
2026-04-02  4:15   ` [PATCH v2 08/12] skip_prefix(): check const match between in and out params Jeff King
2026-04-02  5:11     ` Junio C Hamano
2026-04-02  6:01       ` Jeff King
2026-04-02 15:50         ` Junio C Hamano
2026-04-02 15:41       ` Junio C Hamano
2026-04-03 11:13     ` Toon Claes
2026-04-04  5:42       ` [PATCH v2 13/12] git-compat-util: fix CONST_OUTPARAM typo and indentation Jeff King
2026-04-02  4:15   ` [PATCH v2 09/12] pkt-line: make packet_reader.line non-const Jeff King
2026-04-02  4:15   ` [PATCH v2 10/12] range-diff: drop const to fix strstr() warnings Jeff King
2026-04-02  4:15   ` [PATCH v2 11/12] http: drop const to fix strstr() warning Jeff King
2026-04-02  4:15   ` [PATCH v2 12/12] refs/files-backend: drop const to fix strchr() warning Jeff King
2026-04-03 11:14   ` [PATCH v2 0/12] fixing the remainder of the C23 strchr warnings Toon Claes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260331235136.GI2328529@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=collin.funk1@gmail.com \
    --cc=git@grubix.eu \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox