Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommandHandler's validateWrite method has a calculated overflow problem #2150

Closed
Yanam opened this issue Jul 12, 2022 · 2 comments
Closed

CommandHandler's validateWrite method has a calculated overflow problem #2150

Yanam opened this issue Jul 12, 2022 · 2 comments
Labels
type: bug A general bug
Milestone

Comments

@Yanam
Copy link
Contributor

Yanam commented Jul 12, 2022

Bug Report

Current Behavior

When the RequestQueueSize of ClientOptions is set to the range Integer.MAX_VALUE to Integer.MAX_VALUE-4, the execution of any redis command will throw an exception

Stack trace
Caused by: io.lettuce.core.RedisException: Internal stack size exceeded: 2147483643. Commands are not accepted until the stack size drops.
	at io.lettuce.core.protocol.CommandHandler.validateWrite(CommandHandler.java:526)
	at io.lettuce.core.protocol.CommandHandler.addToStack(CommandHandler.java:496)
	at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:417)
	at io.lettuce.core.protocol.CommandHandler.write(CommandHandler.java:387)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:717)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:764)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:790)
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:758)
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:808)
	at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1025)
	at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:306)
	at io.lettuce.core.RedisHandshake.dispatch(RedisHandshake.java:221)

Input Code

Input Code
    RedisClient redisClient = RedisClient.create("redis://127.0.0.1:6379/");
    ClientOptions clientOptions = ClientOptions.builder().requestQueueSize(Integer.MAX_VALUE-4).build();
    redisClient.setOptions(clientOptions);
    StatefulRedisConnection<String, String> connection = redisClient.connect();

    RedisCommands<String, String> syncCommands = connection.sync();
    String res = syncCommands.get("test");

Expected behavior/code

Possible Solution

public class CommandHandler extends ChannelDuplexHandler implements HasQueuedCommands {

...

  private void validateWrite(int commands) {

        if (usesBoundedQueues()) {
            
            // int allowedRequestQueueSize = clientOptions.getRequestQueueSize() + maxMaintenanceCommands;

            // fixes integer calculation overflow problem
            int allowedRequestQueueSize =  clientOptions.getRequestQueueSize() >= Integer.MAX_VALUE-maxMaintenanceCommands?  Integer.MAX_VALUE : clientOptions.getRequestQueueSize()+maxMaintenanceCommands;
            if (stack.size() + commands > allowedRequestQueueSize)

                throw new RedisException("Internal stack size exceeded: " + clientOptions.getRequestQueueSize()
                        + ". Commands are not accepted until the stack size drops.");
        }
    }

...
}

Additional context

None

@mp911de
Copy link
Collaborator

mp911de commented Jul 12, 2022

Do you want to submit a pull request?

@Yanam
Copy link
Contributor Author

Yanam commented Jul 12, 2022

Do you want to submit a pull request?

ok,I can submit a pull request

Yanam added a commit to Yanam/lettuce-core that referenced this issue Jul 12, 2022
@mp911de mp911de added this to the 6.1.9.RELEASE milestone Jul 12, 2022
@mp911de mp911de added the type: bug A general bug label Jul 12, 2022
mp911de pushed a commit that referenced this issue Jul 12, 2022
mp911de added a commit that referenced this issue Jul 12, 2022
Assume maintenance command count is decremented from request queue size
mp911de pushed a commit that referenced this issue Jul 12, 2022
mp911de added a commit that referenced this issue Jul 12, 2022
Assume maintenance command count is decremented from request queue size
@mp911de mp911de closed this as completed Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants