Skip to content

ByteUtils.getBytes(ByteBuffer) does not respect buffer position for heap buffers #2204

Closed

Description

@hban

Noticed after upgrading from 2.5.5 to 2.6.0. If RedisElementWriter returns ByteBuffer that is not completely filled (limit() < capacity()), Spring Data will still use the entire buffer (including trailing junk bytes), instead of using only the filled part. This happens for ZSet values at least, I haven't checked other scenarios.

Reproducer:

import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.RedisElementReader;
import org.springframework.data.redis.serializer.RedisElementWriter;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.nio.ByteBuffer;

public class Foo {
    public static void main(String[] args) {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration);
        connectionFactory.afterPropertiesSet();

        RedisElementReader<Integer> reader = buffer -> { throw new UnsupportedOperationException(); };
        RedisElementWriter<Integer> writer = element -> {
            // Buffer is intentionally larger than necessary.
            ByteBuffer buffer = ByteBuffer.allocate(16);
            buffer.putInt(element);
            buffer.flip();
            System.out.printf("Serialized value (%d) has %d bytes%n", element, buffer.remaining());
            return buffer;
        };

        RedisSerializationContext<Integer, Integer> serializationContext = RedisSerializationContext
                .<Integer, Integer>newSerializationContext()
                .key(reader, writer)
                .value(reader, writer)
                .hashKey(reader, writer)
                .hashValue(reader, writer)
                .build();

        ReactiveRedisTemplate<Integer, Integer> intTemplate =
                new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
        intTemplate.opsForZSet().add(20, 21, 22.0).block();

        ReactiveRedisTemplate<byte[], byte[]> byteTemplate =
                new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.byteArray());
        byteTemplate.opsForZSet()
                .scan(new byte[] { 0, 0, 0, 20 })
                .doOnNext(tuple -> {
                    // This prints "4 bytes" on 2.5.5, but "16 bytes" on 2.6.0.
                    System.out.printf("Deserialized value has %d bytes%n", tuple.getValue().length);
                })
                .then()
                .block();
    }
}

Activity

  1. self-assigned this
    on Nov 22, 2021
  2. changed the title [-]ZSet value serialization includes junk bytes [/-] [+]`ByteUtils.getBytes(ByteBuffer)` does not respect buffer position for heap buffers[/+] on Dec 14, 2021
  3. added
    type: regressionA regression from a previous release
    and removed on Dec 14, 2021
  4. added this to the 2.6.1 (2021.1.1) milestone on Dec 14, 2021
  5. mp911de commented on Dec 14, 2021

    @mp911de
    Member

    Thanks for reporting the issue. We introduced a regression with Spring Data 2.6 affecting heap buffers.

  6. added 3 commits that reference this issue on Dec 14, 2021
    5a49235
    2414545
    6ed3b51
  7. added a commit that references this issue on Dec 15, 2021
    40ecc9c
  8. added a commit that references this issue on Apr 26, 2024
    3ca2191
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions