Skip to content

Commit 533ddba

Browse files
peffgitster
authored andcommitted
pkt-line: prepare buffer before handling ERR packets
Since 2d103c3 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 014ade7 commit 533ddba

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

pkt-line.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,17 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
350350
return PACKET_READ_EOF;
351351
}
352352

353-
if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
354-
starts_with(buffer, "ERR "))
355-
die(_("remote error: %s"), buffer + 4);
356-
357353
if ((options & PACKET_READ_CHOMP_NEWLINE) &&
358354
len && buffer[len-1] == '\n')
359355
len--;
360356

361357
buffer[len] = 0;
362358
packet_trace(buffer, len, 0);
359+
360+
if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
361+
starts_with(buffer, "ERR "))
362+
die(_("remote error: %s"), buffer + 4);
363+
363364
*pktlen = len;
364365
return PACKET_READ_NORMAL;
365366
}

t/t5516-fetch-push.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ do
12411241
git fetch ../testrepo/.git $SHA1_2 &&
12421242
git cat-file commit $SHA1_2 &&
12431243
test_must_fail git fetch ../testrepo/.git $SHA1_3 2>err &&
1244-
test_i18ngrep "remote error:.*not our ref" err
1244+
test_i18ngrep "remote error:.*not our ref.*$SHA1_3\$" err
12451245
)
12461246
'
12471247
done

0 commit comments

Comments
 (0)