From 9dc79c039a16674458a39c8bdfbcfe049f3fae77 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 17 Nov 2017 16:02:11 +0100 Subject: [PATCH] Streams: fix reverse iterator discarding of items out of range. --- src/t_stream.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/t_stream.c b/src/t_stream.c index f64824c9..efb01ef6 100644 --- a/src/t_stream.c +++ b/src/t_stream.c @@ -597,11 +597,18 @@ int streamIteratorGetID(streamIterator *si, streamID *id, int64_t *numfields) { } } - /* If we do not emit, we have to discard. */ - int to_discard = (flags & STREAM_ITEM_FLAG_SAMEFIELDS) ? - *numfields : *numfields*2; - for (int64_t i = 0; i < to_discard; i++) - si->lp_ele = lpNext(si->lp,si->lp_ele); + /* If we do not emit, we have to discard if we are going + * forward, or seek the previous entry if we are going + * backward. */ + if (!si->rev) { + int to_discard = (flags & STREAM_ITEM_FLAG_SAMEFIELDS) ? + *numfields : *numfields*2; + for (int64_t i = 0; i < to_discard; i++) + si->lp_ele = lpNext(si->lp,si->lp_ele); + } else { + int prev_times = 4; /* flag + id ms/seq diff + numfields. */ + while(prev_times--) si->lp_ele = lpPrev(si->lp,si->lp_ele); + } } /* End of listpack reached. Try the next/prev radix tree node. */