git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 09/12] pkt-line: make packet_reader.line non-const

From
Jeff King <peff@peff.net>
Date
Mar 31, 2026, 23:51 UTC
Message-ID
<20260331235136.GI2328529@coredump.intra.peff.net>
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
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 22 of 50 in “fixing the remainder of the C23 strchr warnings”
  1. 0/12 fixing the remainder of the C23 strchr warningsJeff King, Mar 31, 2026
  2. 01/12 convert: add const to fix strchr() warningsJeff King, Mar 31, 2026
  3. 02/12 http: add const to fix strchr() warningsJeff King, Mar 31, 2026
  4. 03/12 transport-helper: drop const to fix strchr() warningsJeff King, Mar 31, 2026
  5. Patrick SteinhardtApr 1, 2026
  6. 04/12 pager: explicitly cast away strchr() constnessJeff King, Mar 31, 2026
  7. Junio C HamanoApr 1, 2026
  8. Jeff KingApr 2, 2026
  9. 05/12 run-command: explicitly cast away constness when assigning to voidJeff King, Mar 31, 2026
  10. 06/12 find_last_dir_sep(): convert inline function to macroJeff King, Mar 31, 2026
  11. 07/12 pseudo-merge: fix disk reads from find_pseudo_merge()Jeff King, Mar 31, 2026
  12. Jeff KingMar 31, 2026
  13. Junio C HamanoApr 1, 2026
  14. Taylor BlauApr 2, 2026
  15. 08/12 skip_prefix(): check const match between in and out paramsJeff King, Mar 31, 2026
  16. Phillip WoodApr 1, 2026
  17. Phillip WoodApr 1, 2026
  18. Jeff KingApr 1, 2026
  19. Junio C HamanoApr 1, 2026
  20. Phillip WoodApr 2, 2026
  21. Patrick SteinhardtApr 1, 2026
  22. 09/12 pkt-line: make packet_reader.line non-constJeff King, Mar 31, 2026
  23. Junio C HamanoApr 1, 2026
  24. Jeff KingApr 2, 2026
  25. 10/12 range-diff: drop const to fix strstr() warningsJeff King, Mar 31, 2026
  26. 11/12 http: drop const to fix strstr() warningJeff King, Mar 31, 2026
  27. 12/12 refs/files-backend: drop const to fix strchr() warningJeff King, Mar 31, 2026
  28. Patrick SteinhardtApr 1, 2026
  29. Junio C HamanoApr 1, 2026
  30. Jeff KingApr 2, 2026
  31. 0/12 fixing the remainder of the C23 strchr warningsJeff King, Apr 2, 2026
  32. 01/12 convert: add const to fix strchr() warningsJeff King, Apr 2, 2026
  33. 02/12 http: add const to fix strchr() warningsJeff King, Apr 2, 2026
  34. 03/12 transport-helper: drop const to fix strchr() warningsJeff King, Apr 2, 2026
  35. 04/12 pager: explicitly cast away strchr() constnessJeff King, Apr 2, 2026
  36. 05/12 run-command: explicitly cast away constness when assigning to voidJeff King, Apr 2, 2026
  37. 06/12 find_last_dir_sep(): convert inline function to macroJeff King, Apr 2, 2026
  38. 07/12 pseudo-merge: fix disk reads from find_pseudo_merge()Jeff King, Apr 2, 2026
  39. 08/12 skip_prefix(): check const match between in and out paramsJeff King, Apr 2, 2026
  40. Junio C HamanoApr 2, 2026
  41. Jeff KingApr 2, 2026
  42. Junio C HamanoApr 2, 2026
  43. Junio C HamanoApr 2, 2026
  44. Toon ClaesApr 3, 2026
  45. 13/12 git-compat-util: fix CONST_OUTPARAM typo and indentationJeff King, Apr 4, 2026
  46. 09/12 pkt-line: make packet_reader.line non-constJeff King, Apr 2, 2026
  47. 10/12 range-diff: drop const to fix strstr() warningsJeff King, Apr 2, 2026
  48. 11/12 http: drop const to fix strstr() warningJeff King, Apr 2, 2026
  49. 12/12 refs/files-backend: drop const to fix strchr() warningJeff King, Apr 2, 2026
  50. Toon ClaesApr 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.