Skip to content

Commit 40ccaed

Browse files
committed
message_pool: fix partial read with error
1 parent 126929f commit 40ccaed

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

connectctl/main.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,16 @@ func verifySend(opts docopt.Opts) {
252252
res, err := client.Do(req)
253253
if err != nil {
254254
panic(err)
255-
return
256255
}
257256
resBody, err := io.ReadAll(res.Body)
258257
if err != nil {
259258
panic(err)
260-
return
261259
}
262260

263261
result := map[string]any{}
264262
err = json.Unmarshal(resBody, &result)
265263
if err != nil {
266264
panic(err)
267-
return
268265
}
269266

270267
printResult(result)
@@ -306,19 +303,16 @@ func verifyNetwork(opts docopt.Opts) {
306303
res, err := client.Do(req)
307304
if err != nil {
308305
panic(err)
309-
return
310306
}
311307
resBody, err := io.ReadAll(res.Body)
312308
if err != nil {
313309
panic(err)
314-
return
315310
}
316311

317312
result := map[string]any{}
318313
err = json.Unmarshal(resBody, &result)
319314
if err != nil {
320315
panic(err)
321-
return
322316
}
323317

324318
printResult(result)
@@ -360,19 +354,16 @@ func loginNetwork(opts docopt.Opts) {
360354
res, err := client.Do(req)
361355
if err != nil {
362356
panic(err)
363-
return
364357
}
365358
resBody, err := io.ReadAll(res.Body)
366359
if err != nil {
367360
panic(err)
368-
return
369361
}
370362

371363
result := map[string]any{}
372364
err = json.Unmarshal(resBody, &result)
373365
if err != nil {
374366
panic(err)
375-
return
376367
}
377368

378369
printResult(result)
@@ -418,12 +409,10 @@ func clientId(opts docopt.Opts) {
418409
res, err := client.Do(req)
419410
if err != nil {
420411
panic(err)
421-
return
422412
}
423413
resBody, err := io.ReadAll(res.Body)
424414
if err != nil {
425415
panic(err)
426-
return
427416
}
428417

429418
fmt.Printf("response: %s\n", resBody)
@@ -432,7 +421,6 @@ func clientId(opts docopt.Opts) {
432421
err = json.Unmarshal(resBody, &result)
433422
if err != nil {
434423
panic(err)
435-
return
436424
}
437425

438426
printResult(result)

message_pool.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,19 @@ func MessagePoolReadAllWithTag(r io.Reader, tag uint8) ([]byte, error) {
287287
for j := 0; j < len(orderedMessagePools); j += 1 {
288288
for i < len(b) {
289289
n, err := r.Read(b[i:])
290-
if n == 0 {
291-
return b[:i], nil
290+
if n > 0 {
291+
i += n
292292
}
293293
if err != nil {
294+
if err == io.EOF {
295+
return b[:i], nil
296+
}
294297
MessagePoolReturn(b)
295298
return nil, err
296299
}
297-
i += n
300+
if n == 0 {
301+
return b[:i], nil
302+
}
298303
}
299304

300305
if len(orderedMessagePools) <= j+1 {
@@ -312,13 +317,21 @@ func MessagePoolReadAllWithTag(r io.Reader, tag uint8) ([]byte, error) {
312317
defer MessagePoolReturn(b)
313318
for {
314319
n, err := r.Read(b)
315-
if n == 0 {
316-
return out, nil
320+
if n > 0 {
321+
out = append(out, b[:n]...)
317322
}
318323
if err != nil {
324+
if err == io.EOF {
325+
return out, nil
326+
}
327+
// Preserve the historical contract that (non-EOF) errors yield a nil buffer
328+
// (callers do not expect to MessagePoolReturn on the error path).
329+
// We still consumed the bytes (preventing reader desync on streams).
319330
return nil, err
320331
}
321-
out = append(out, b[:n]...)
332+
if n == 0 {
333+
return out, nil
334+
}
322335
}
323336
}
324337

0 commit comments

Comments
 (0)