fetch-pack: make unexpected peek result non-fatal
PATCH 1/1 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.
--- fetch-pack.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
fetch-pack.c +11 -8
@@ -1370,17 +1370,20 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,static int process_section_header (struct packet_reader *reader,const char *section,int peek) { -int ret; - -if (packet_reader_peek (reader) !=PACKET_READ_NORMAL ) -die (_ ("error reading section header '%s'" ), section); +int ret =0 ; - ret = !strcmp (reader->line, section); +if (packet_reader_peek (reader) ==PACKET_READ_NORMAL && + !strcmp (reader->line, section)) + ret =1 ;if (!peek) { -if (!ret) -die (_ ("expected '%s', received '%s'" ), - section, reader->line); +if (!ret) { +if (reader->line) +die (_ ("expected '%s', received '%s'" ), + section, reader->line); +else +die (_ ("expected '%s'" ), section); + }packet_reader_read (reader); }
Signature · 1 line
2.36.0.550.gb090851708-goog
Quoted 13 lines · Jonathan Tan
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()(seeprocess_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.
Quoted 4 lines
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->linemay now be NULL later in the function, so update the error message printing code accordingly.
[not a suggestion to change anything; just making sure I got the above correct]
We used to die unless READ_NORMAL was returned, so the code after packet_reader_peek() can rely on reader->line to be populated. Now that code must handle cases where an earlier _peek() gave something other than READ_NORMAL and in such an error case reader->line points at NULL. So any code that assumed reader->line was populated needs to be updated.
Makes sense.
Quoted 37 lines
Signed-off-by Jonathan Tan <[email protected]>--- fetch-pack.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)fetch-pack.c +11
-8@@ -1370,17 +1370,20 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,static int process_section_header (struct packet_reader *reader,const char *section,int peek) { -int ret; - -if (packet_reader_peek (reader) !=PACKET_READ_NORMAL ) -die (_ ("error reading section header '%s'" ), section); +int ret =0 ; - ret = !strcmp (reader->line, section); +if (packet_reader_peek (reader) ==PACKET_READ_NORMAL && + !strcmp (reader->line, section)) + ret =1 ;if (!peek) { -if (!ret) -die (_ ("expected '%s', received '%s'" ), - section, reader->line); +if (!ret) { +if (reader->line) +die (_ ("expected '%s', received '%s'" ), + section, reader->line); +else +die (_ ("expected '%s'" ), section); + }packet_reader_read (reader); }
Quoted 8 lines · Junio C Hamano
We used to die unless
READ_NORMALwas returned, so the code afterpacket_reader_peek()can rely onreader->lineto be populated. Now that code must handle cases where an earlier_peek()gave something other thanREAD_NORMALand in such an error casereader->linepoints at NULL. So any code that assumedreader->linewas populated needs to be updated.Makes sense.
Thanks for taking a look. Yes, that's correct.