Skip to content

Commit caf3ca7

Browse files
committed
Merge branch 'jk/sideband-more-error-checking'
The code to detect premature EOF in the sideband demultiplexer has been cleaned up. * jk/sideband-more-error-checking: sideband: diagnose more sideband anomalies
2 parents 6a44c9c + af22a63 commit caf3ca7

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

pkt-line.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,11 @@ int recv_sideband(const char *me, int in_stream, int out)
461461
enum sideband_type sideband_type;
462462

463463
while (1) {
464-
len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX,
465-
0);
466-
if (!demultiplex_sideband(me, buf, len, 0, &scratch,
464+
int status = packet_read_with_status(in_stream, NULL, NULL,
465+
buf, LARGE_PACKET_MAX,
466+
&len,
467+
PACKET_READ_GENTLE_ON_EOF);
468+
if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
467469
&sideband_type))
468470
continue;
469471
switch (sideband_type) {
@@ -520,9 +522,9 @@ enum packet_read_status packet_reader_read(struct packet_reader *reader)
520522
reader->options);
521523
if (!reader->use_sideband)
522524
break;
523-
if (demultiplex_sideband(reader->me, reader->buffer,
524-
reader->pktlen, 1, &scratch,
525-
&sideband_type))
525+
if (demultiplex_sideband(reader->me, reader->status,
526+
reader->buffer, reader->pktlen, 1,
527+
&scratch, &sideband_type))
526528
break;
527529
}
528530

sideband.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "config.h"
44
#include "sideband.h"
55
#include "help.h"
6+
#include "pkt-line.h"
67

78
struct keyword_entry {
89
/*
@@ -114,7 +115,8 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
114115
#define ANSI_SUFFIX "\033[K"
115116
#define DUMB_SUFFIX " "
116117

117-
int demultiplex_sideband(const char *me, char *buf, int len,
118+
int demultiplex_sideband(const char *me, int status,
119+
char *buf, int len,
118120
int die_on_error,
119121
struct strbuf *scratch,
120122
enum sideband_type *sideband_type)
@@ -130,17 +132,30 @@ int demultiplex_sideband(const char *me, char *buf, int len,
130132
suffix = DUMB_SUFFIX;
131133
}
132134

133-
if (len == 0) {
134-
*sideband_type = SIDEBAND_FLUSH;
135-
goto cleanup;
136-
}
137-
if (len < 1) {
135+
if (status == PACKET_READ_EOF) {
138136
strbuf_addf(scratch,
139-
"%s%s: protocol error: no band designator",
137+
"%s%s: unexpected disconnect while reading sideband packet",
140138
scratch->len ? "\n" : "", me);
141139
*sideband_type = SIDEBAND_PROTOCOL_ERROR;
142140
goto cleanup;
143141
}
142+
143+
if (len < 0)
144+
BUG("negative length on non-eof packet read");
145+
146+
if (len == 0) {
147+
if (status == PACKET_READ_NORMAL) {
148+
strbuf_addf(scratch,
149+
"%s%s: protocol error: missing sideband designator",
150+
scratch->len ? "\n" : "", me);
151+
*sideband_type = SIDEBAND_PROTOCOL_ERROR;
152+
} else {
153+
/* covers flush, delim, etc */
154+
*sideband_type = SIDEBAND_FLUSH;
155+
}
156+
goto cleanup;
157+
}
158+
144159
band = buf[0] & 0xff;
145160
buf[len] = '\0';
146161
len--;

sideband.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ enum sideband_type {
1818
*
1919
* scratch must be a struct strbuf allocated by the caller. It is used to store
2020
* progress messages split across multiple packets.
21+
*
22+
* The "status" parameter is a pkt-line response as returned by
23+
* packet_read_with_status() (e.g., PACKET_READ_NORMAL).
2124
*/
22-
int demultiplex_sideband(const char *me, char *buf, int len,
25+
int demultiplex_sideband(const char *me, int status,
26+
char *buf, int len,
2327
int die_on_error,
2428
struct strbuf *scratch,
2529
enum sideband_type *sideband_type);

t/t0070-fundamental.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ test_expect_success 'incomplete sideband messages are reassembled' '
4040
grep "Hello, world" err
4141
'
4242

43+
test_expect_success 'eof on sideband message is reported' '
44+
printf 1234 >input &&
45+
test-tool pkt-line receive-sideband <input 2>err &&
46+
test_i18ngrep "unexpected disconnect" err
47+
'
48+
49+
test_expect_success 'missing sideband designator is reported' '
50+
printf 0004 >input &&
51+
test-tool pkt-line receive-sideband <input 2>err &&
52+
test_i18ngrep "missing sideband" err
53+
'
54+
4355
test_done

0 commit comments

Comments
 (0)