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

Re: [PATCH] pkt-line: don't check string length in packet_length()

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 5, 2023, 05:56 UTC
Message-ID
<xmqqttui3nqn.fsf@gitster.g>
In-Reply-To
<89d58db7-6a01-b3fa-54f0-19d5a3819eb3@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 5 quoted lines
> hex2chr() takes care not to run over the end of a short string.
> 101736a14c (pkt-line: extern packet_length(), 2020-05-19) turned the
> input parameter of packet_length() from a string pointer into an array
> of known length, making string length checks unnecessary.  Get rid of
> them by using hexval() directly.

I am puzzled about the part of the above description on "making string length checks unnecessary". The two callers we currently have both do pass char[4], but the compiler would not stop us from passing a pointer to a memory region of an unknown size; if we butcher one of the current callers

diff --git c/pkt-line.c w/pkt-line.c
index 6e022029ca..e1c49baefd 100644
--- c/pkt-line.c
+++ w/pkt-line.c
@@ -421,7 +421,7 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
 		return PACKET_READ_EOF;
 	}
 
-	len = packet_length(linelen);
+	len = packet_length(buffer);
 
 	if (len < 0) {
 		if (options & PACKET_READ_GENTLE_ON_READ_ERROR)

where "buffer" is just a random piece of memory passed to the caller
and there is no such guarantee like "it at least is 4 bytes long",
we would just slurp garbage and run past the end of the buffer.

> The resulting branchless code is simpler and it becomes easier to see
> that the function mirrors set_packet_header().

I do like the resulting code, but I feel a bit uneasy to sell this
change as "the code becomes more streamlined without losing safety".
It looks more like "this change is safe for our two callers; those
adding more callers in the future are better be very careful", no?
Previous: René ScharfeNext: René Scharfe
Message 2 of 8 in “pkt-line: don't check string length in packet_length()”
  1. pkt-line: don't check string length in packet_length()René Scharfe, Jul 1, 2023
  2. Junio C HamanoJul 5, 2023
  3. René ScharfeJul 5, 2023
  4. Junio C HamanoJul 5, 2023
  5. René ScharfeJul 5, 2023
  6. Junio C HamanoJul 5, 2023
  7. René ScharfeJul 6, 2023
  8. pkt-line: add size parameter to packet_length()René Scharfe, Jul 7, 2023

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.