#4804·valkey

[BUG] XADD/XTRIM MAXLEN of 2^32 or more wraps and trims the stream on 32-bit builds

Author: xdk-amzCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbug

Describe the bug

On 32-bit builds, a MAXLEN threshold of 2^32 or more wraps modulo 2^32. XTRIM s MAXLEN 4294967296 trims to 0 entries and deletes the whole stream. MAXLEN 4294967297 trims to 1 entry. XADD with MAXLEN has the same problem.

The parser accepts any long long >= 0. streamTrim() then narrows it to size_t (32 bits on these builds):

c
size_t maxlen = args->maxlen;

A 32-bit server cannot hold 2^32 entries, so a threshold this large must never trim.

To reproduce

On a 32-bit build (make 32bit):

DEL s
XADD s 1-0 f v
XADD s 2-0 f v
XTRIM s MAXLEN 4294967296
XLEN s

XTRIM returns 2 and XLEN returns 0.

Test for tests/unit/type/stream.tcl:

tcl
test {XTRIM and XADD with MAXLEN of 2^32 or more do not trim} {
    r DEL mystream
    r XADD mystream 1-0 f v
    r XADD mystream 2-0 f v
    assert_equal 0 [r XTRIM mystream MAXLEN 4294967296]
    assert_equal 0 [r XTRIM mystream MAXLEN 4294967297]
    r XADD mystream MAXLEN 4294967296 3-0 f v
    assert_equal 3 [r XLEN mystream]
}

With maxlen 32 bits wide, the test fails:

[err]: XTRIM and XADD with MAXLEN of 2^32 or more do not trim in tests/unit/type/stream.tcl
Expected '0' to be equal to '2' (context: type eval line 5 cmd {assert_equal 0 [r XTRIM mystream MAXLEN 4294967296]} proc ::test)

It passes on 64-bit builds.

Expected behavior

A MAXLEN of 2^32 or more trims nothing. XTRIM returns 0 and the stream keeps all its entries.

Additional information

Suggested fix: keep the threshold 64-bit wide.

c
uint64_t maxlen = (uint64_t)args->maxlen;

s->length is already uint64_t, so the comparisons need no other change.