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

[PATCH 2/2] always check for NULL return from packet_read_line()

From
Jon Simons <jon@jonsimons.org>
Date
Feb 8, 2018, 18:47 UTC
Message-ID
<1518115670-2646-3-git-send-email-jon@jonsimons.org>
In-Reply-To
<1518115670-2646-1-git-send-email-jon@jonsimons.org>

The packet_read_line() function will die if it sees any protocol or socket errors. But it will return NULL for a flush packet; some callers which are not expecting this may dereference NULL if they get an unexpected flush. This would involve the other side breaking protocol, but we should flag the error rather than segfault.

Signed-off-by: Jon Simons <jon@jonsimons.org>
---
 remote-curl.c | 2 ++
 send-pack.c   | 2 ++
 2 files changed, 4 insertions(+)
diff --git a/remote-curl.c b/remote-curl.c
index 0053b09..9903077 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -339,6 +339,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
 		 * pkt-line matches our request.
 		 */
 		line = packet_read_line_buf(&last->buf, &last->len, NULL);
+		if (!line)
+			die("invalid server response; expected service, got flush packet");
 
 		strbuf_reset(&exp);
 		strbuf_addf(&exp, "# service=%s", service);
diff --git a/send-pack.c b/send-pack.c
index 11d6f3d..d37b265 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -147,6 +147,8 @@ static int pack_objects(int fd, struct ref *refs, struct oid_array *extra, struc
 static int receive_unpack_status(int in)
 {
 	const char *line = packet_read_line(in, NULL);
+	if (!line)
+		return error(_("unexpected flush packet while reading remote unpack status"));
 	if (!skip_prefix(line, "unpack ", &line))
 		return error(_("unable to parse remote unpack status: %s"), line);
 	if (strcmp(line, "ok"))
-- 
2.1.4
Previous: Jon SimonsNext: Jeff King
Message 3 of 4 in “Fix NULL checks for some packet_read_line call sites”
  1. 0/2 Fix NULL checks for some packet_read_line call sitesJon Simons, Feb 8, 2018
  2. 1/2 correct error messages for NULL packet_read_line()Jon Simons, Feb 8, 2018
  3. 2/2 always check for NULL return from packet_read_line()Jon Simons, Feb 8, 2018
  4. Jeff KingFeb 8, 2018

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.