fetch-pack: make unexpected peek result non-fatal

When a Git server responds to a fetch request, it may send optional sections before the packfile section. To handle this, the Git client calls packet_reader_peek() (see process_section_header()) in order to see what's next without consuming the line. However, as implemented, Git errors out whenever what's peeked is not an ordinary line. This is not only unexpected (here, we only need to know whether the upcoming line is the section header we want) but causes errors to include the name of a section header that is irrelevant to the cause of the error. For example, at $DAYJOB, we have seen "fatal: error reading section header 'shallow-info'" error messages when none of the repositories involved are shallow. Therefore, fix this so that the peek returns 1 if the upcoming line is the wanted section header and nothing else. Because of this change, reader->line may now be NULL later in the function, so update the error message printing code accordingly. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed May 16, 2022 at 04:02 UTC 7709acf7beca41be2e382eb0e6c13b5003a20c1a
1 file changed +11 -8
fetch-pack.c
+11 -8
@@ -1357,17 +1357,20 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
1357 static int process_section_header(struct packet_reader *reader,
1358 const char *section, int peek)
1359 {
1360 - int ret;
1361 -
1362 - if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
1363 - die(_("error reading section header '%s'"), section);
1360 + int ret = 0;
1361
1365 - ret = !strcmp(reader->line, section);
1362 + if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1363 + !strcmp(reader->line, section))
1364 + ret = 1;
1365
1366 if (!peek) {
1368 - if (!ret)
1369 - die(_("expected '%s', received '%s'"),
1370 - section, reader->line);
1367 + if (!ret) {
1368 + if (reader->line)
1369 + die(_("expected '%s', received '%s'"),
1370 + section, reader->line);
1371 + else
1372 + die(_("expected '%s'"), section);
1373 + }
1374 packet_reader_read(reader);
1375 }
1376