summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoris Cro <kappaloris@gmail.com>2019-11-04 16:36:06 +0100
committerLoris Cro <kappaloris@gmail.com>2019-11-04 16:36:06 +0100
commitb12d2f65d660b4139b322af90b6ef60ed267210b (patch)
tree1742511b99888427eac8d03d7404daa3180b0449
parent3f14bfd809e1aba63ec3ffc852ca7b169f993aff (diff)
downloadredis-b12d2f65d660b4139b322af90b6ef60ed267210b.tar.gz
fix unreported overflow in autogerenared stream IDs
-rw-r--r--src/t_stream.c23
-rw-r--r--tests/unit/type/stream.tcl6
2 files changed, 19 insertions, 10 deletions
diff --git a/src/t_stream.c b/src/t_stream.c
index ea9a620f1..58b59f521 100644
--- a/src/t_stream.c
+++ b/src/t_stream.c
@@ -173,9 +173,19 @@ int streamCompareID(streamID *a, streamID *b) {
* C_ERR if an ID was given via 'use_id', but adding it failed since the
* current top ID is greater or equal. */
int streamAppendItem(stream *s, robj **argv, int64_t numfields, streamID *added_id, streamID *use_id) {
- /* If an ID was given, check that it's greater than the last entry ID
- * or return an error. */
- if (use_id && streamCompareID(use_id,&s->last_id) <= 0) return C_ERR;
+
+ /* Generate the new entry ID. */
+ streamID id;
+ if (use_id)
+ id = *use_id;
+ else
+ streamNextID(&s->last_id,&id);
+
+ /* Check that the new ID is greater than the last entry ID
+ * or return an error. Automatically generated IDs might
+ * overflow (and wrap-around) when incrementing the sequence
+ part. */
+ if (streamCompareID(&id,&s->last_id) <= 0) return C_ERR;
/* Add the new entry. */
raxIterator ri;
@@ -192,13 +202,6 @@ int streamAppendItem(stream *s, robj **argv, int64_t numfields, streamID *added_
}
raxStop(&ri);
- /* Generate the new entry ID. */
- streamID id;
- if (use_id)
- id = *use_id;
- else
- streamNextID(&s->last_id,&id);
-
/* We have to add the key into the radix tree in lexicographic order,
* to do so we consider the ID as a single 128 bit number written in
* big endian, so that the most significant bytes are the first ones. */
diff --git a/tests/unit/type/stream.tcl b/tests/unit/type/stream.tcl
index a7415ae8d..aa9c5f3a9 100644
--- a/tests/unit/type/stream.tcl
+++ b/tests/unit/type/stream.tcl
@@ -79,6 +79,12 @@ start_server {
assert {[streamCompareID $id2 $id3] == -1}
}
+ test {XADD IDs correctly report an error when overflowing} {
+ r DEL mystream
+ r xadd mystream 18446744073709551615-18446744073709551615 a b
+ assert_error ERR* {r xadd mystream * c d}
+ }
+
test {XADD with MAXLEN option} {
r DEL mystream
for {set j 0} {$j < 1000} {incr j} {