path: root/pkt-line.c
diff options
authorJeff King <>2019-04-13 05:54:02 (GMT)
committerJunio C Hamano <>2019-04-15 05:00:51 (GMT)
commit533ddba47e8cc36a6ec2864f482a8e540465eb64 (patch)
treee356d9c18e8b8bb878c128acc332993988fa39dd /pkt-line.c
parent014ade748420b074a06dbb7f5fb974b5e6184f43 (diff)
pkt-line: prepare buffer before handling ERR packets
Since 2d103c31c2 (pack-protocol.txt: accept error packets in any context, 2018-12-29), the pktline code will detect an ERR packet and die automatically, saving the caller from dealing with it. But we do so too early in the function, before we have terminated the buffer with a NUL. As a result, passing the ERR message to die() may result in us printing random cruft from a previous packet. This doesn't trigger memory tools like ASan because we reuse the same buffer over and over (so the contents are valid and initialized; they're just stale). We can see demonstrate this by tightening the regex we use to match the error message in t5516; without this patch, git-fetch will accidentally print the capabilities from the (much longer) initial packet we received. By moving the ERR code later in the function we get a few other benefits, too: - we'll now chomp any newline sent by the other side (which is what we want, since die() will add its own newline) - we'll now mention the ERR packet with GIT_TRACE_PACKET Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'pkt-line.c')
1 files changed, 5 insertions, 4 deletions
diff --git a/pkt-line.c b/pkt-line.c
index ffd7220..c9ed780 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -350,16 +350,17 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
- if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
- starts_with(buffer, "ERR "))
- die(_("remote error: %s"), buffer + 4);
if ((options & PACKET_READ_CHOMP_NEWLINE) &&
len && buffer[len-1] == '\n')
buffer[len] = 0;
packet_trace(buffer, len, 0);
+ if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
+ starts_with(buffer, "ERR "))
+ die(_("remote error: %s"), buffer + 4);
*pktlen = len;