always check for NULL return from packet_read_line()

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> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jon Simons committed Feb 8, 2018 at 13:47 UTC bb1356dc643e1488ccc1924ab674f6cbbe615f74
2 files changed +4
remote-curl.c
+2
@@ -339,6 +339,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
339 * pkt-line matches our request.
340 */
341 line = packet_read_line_buf(&last->buf, &last->len, NULL);
342 + if (!line)
343 + die("invalid server response; expected service, got flush packet");
344
345 strbuf_reset(&exp);
346 strbuf_addf(&exp, "# service=%s", service);
send-pack.c
+2
@@ -137,6 +137,8 @@ static int pack_objects(int fd, struct ref *refs, struct oid_array *extra, struc
137 static int receive_unpack_status(int in)
138 {
139 const char *line = packet_read_line(in, NULL);
140 + if (!line)
141 + return error(_("unexpected flush packet while reading remote unpack status"));
142 if (!skip_prefix(line, "unpack ", &line))
143 return error(_("unable to parse remote unpack status: %s"), line);
144 if (strcmp(line, "ok"))