@@ -74,8 +74,7 @@ sub packet_bin_read {
74
74
my $bytes_read = read STDIN , $buffer , 4;
75
75
if ( $bytes_read == 0 ) {
76
76
# EOF - Git stopped talking to us!
77
- print $debug " STOP\n " ;
78
- exit ();
77
+ return ( -1, " " );
79
78
}
80
79
elsif ( $bytes_read != 4 ) {
81
80
die " invalid packet: '$buffer '" ;
@@ -99,12 +98,21 @@ sub packet_bin_read {
99
98
100
99
sub packet_txt_read {
101
100
my ( $res , $buf ) = packet_bin_read();
102
- unless ( $buf eq ' ' or $buf =~ s /\n $// ) {
101
+ unless ( $res == -1 or $ buf eq ' ' or $buf =~ s /\n $// ) {
103
102
die " A non-binary line MUST be terminated by an LF." ;
104
103
}
105
104
return ( $res , $buf );
106
105
}
107
106
107
+ sub packet_required_key_val_read {
108
+ my ( $key ) = @_ ;
109
+ my ( $res , $buf ) = packet_txt_read();
110
+ unless ( $res == -1 or ( $buf =~ s / ^$key=// and $buf ne ' ' ) ) {
111
+ die " bad $key : '$buf '" ;
112
+ }
113
+ return ( $res , $buf );
114
+ }
115
+
108
116
sub packet_bin_write {
109
117
my $buf = shift ;
110
118
print STDOUT sprintf ( " %04x" , length ($buf ) + 4 );
@@ -152,13 +160,18 @@ sub packet_flush {
152
160
$debug -> flush();
153
161
154
162
while (1) {
155
- my ( $command ) = packet_txt_read() =~ / ^command=(.+)$ / ;
163
+ my ( $res , $command ) = packet_required_key_val_read(" command" );
164
+ if ( $res == -1 ) {
165
+ print $debug " STOP\n " ;
166
+ exit ();
167
+ }
156
168
print $debug " IN: $command " ;
157
169
$debug -> flush();
158
170
159
171
if ( $command eq " list_available_blobs" ) {
160
172
# Flush
161
- packet_bin_read();
173
+ packet_compare_lists([1, " " ], packet_bin_read()) ||
174
+ die " bad list_available_blobs end" ;
162
175
163
176
foreach my $pathname ( sort keys %DELAY ) {
164
177
if ( $DELAY {$pathname }{" requested" } >= 1 ) {
@@ -184,14 +197,13 @@ sub packet_flush {
184
197
packet_flush();
185
198
}
186
199
else {
187
- my ( $pathname ) = packet_txt_read() =~ / ^pathname=(.+)$ / ;
200
+ my ( $res , $pathname ) = packet_required_key_val_read(" pathname" );
201
+ if ( $res == -1 ) {
202
+ die " unexpected EOF while expecting pathname" ;
203
+ }
188
204
print $debug " $pathname " ;
189
205
$debug -> flush();
190
206
191
- if ( $pathname eq " " ) {
192
- die " bad pathname '$pathname '" ;
193
- }
194
-
195
207
# Read until flush
196
208
my ( $done , $buffer ) = packet_txt_read();
197
209
while ( $buffer ne ' ' ) {
@@ -205,6 +217,9 @@ sub packet_flush {
205
217
206
218
( $done , $buffer ) = packet_txt_read();
207
219
}
220
+ if ( $done == -1 ) {
221
+ die " unexpected EOF after pathname '$pathname '" ;
222
+ }
208
223
209
224
my $input = " " ;
210
225
{
@@ -215,6 +230,9 @@ sub packet_flush {
215
230
( $done , $buffer ) = packet_bin_read();
216
231
$input .= $buffer ;
217
232
}
233
+ if ( $done == -1 ) {
234
+ die " unexpected EOF while reading input for '$pathname '" ;
235
+ }
218
236
print $debug " " . length ($input ) . " [OK] -- " ;
219
237
$debug -> flush();
220
238
}
0 commit comments