@@ -224,7 +224,7 @@ func (r *Result) Strings() ([]string, error) {
224
224
return nil , fmt .Errorf ("read array length: %w" , err )
225
225
}
226
226
227
- if ln < 0 {
227
+ if ln <= 0 {
228
228
return nil , nil
229
229
}
230
230
@@ -309,18 +309,27 @@ func (r *Result) writeTo(w io.Writer) (int64, replyType, error) {
309
309
}
310
310
311
311
isNewArray := typ == '*'
312
+ var newArraySize int
312
313
if isNewArray {
314
+ var err error
313
315
// New array
314
- n , err : = strconv .Atoi (string (s ))
316
+ newArraySize , err = strconv .Atoi (string (s ))
315
317
if err != nil {
316
318
return 0 , typ , fmt .Errorf ("invalid array length %q" , s )
317
319
}
318
- r .arrayStack = append (r .arrayStack , n )
320
+ if newArraySize > 0 {
321
+ r .arrayStack = append (r .arrayStack , newArraySize )
322
+ }
319
323
}
320
324
321
325
var n int
322
326
n , r .err = w .Write (s )
323
327
if ! isNewArray {
328
+ // On new arrays, we don't want to advance any state
329
+ // as we've just modified the array stack.
330
+ incrRead ()
331
+ } else if newArraySize == 0 {
332
+ // Nil array, so we want to advance pipeline.
324
333
incrRead ()
325
334
}
326
335
return int64 (n ), typ , r .err
@@ -397,10 +406,11 @@ func (r *Result) ArrayLength() (int, error) {
397
406
}
398
407
399
408
// -1 is a nil array.
400
- if gotN < 0 {
409
+ if gotN <= 0 {
401
410
return gotN , nil
402
411
}
403
412
413
+ // Sanity check that we've populated the array stack correctly.
404
414
if r .arrayStack [len (r .arrayStack )- 1 ] != gotN {
405
415
// This should be impossible.
406
416
return 0 , fmt .Errorf ("array stack mismatch (expected %d, got %d)" , r .arrayStack [len (r .arrayStack )- 1 ], gotN )
@@ -432,16 +442,21 @@ func (r *Result) Next() bool {
432
442
r .mu .Lock ()
433
443
defer r .mu .Unlock ()
434
444
435
- return r .next ()
445
+ return r .hasMore ()
436
446
}
437
447
438
- // next returns true if there are more results to read.
439
- func (r * Result ) next () bool {
448
+ // hasMore returns true if there are more results to read.
449
+ func (r * Result ) hasMore () bool {
440
450
if r .err != nil {
441
451
return false
442
452
}
443
453
444
- return r .pipeline .at < r .pipeline .end || len (r .arrayStack ) > 0
454
+ var arrays int
455
+ for _ , n := range r .arrayStack {
456
+ arrays += n
457
+ }
458
+
459
+ return r .pipeline .at < r .pipeline .end || arrays > 0
445
460
}
446
461
447
462
// Close releases all resources associated with the result.
@@ -458,7 +473,7 @@ func (r *Result) Close() error {
458
473
}
459
474
460
475
func (r * Result ) close () error {
461
- for r .next () {
476
+ for r .hasMore () {
462
477
// Read the result into discard so that the connection can be reused.
463
478
_ , _ , err := r .writeTo (io .Discard )
464
479
if errors .Is (err , errClosed ) {
0 commit comments