Skip to content

OPT_PREFIX + incrByFloat = Key Corruption #408

@nfrm

Description

@nfrm

There seems to be some strange corruption happening when using Redis::OPT_PREFIX combined with Redis::incrByFloat().

Test script:

<?php

    $redis = new redis;
    $redis->connect('127.0.0.1');
    $redis->select(10);
    $redis->flushDB();

    $redis->setOption(Redis::OPT_PREFIX, 'aaa:');

    echo "\nAll Keys:\n";
    foreach ($redis->keys("*") as $key) {
        echo "KEY: '{$key}'\n";
    }

    echo "\n\nIncrement by float\n";
    $redis->incrByFloat("foo", 0.1);

    echo "\n\nAll Keys:\n";
    foreach ($redis->keys("*") as $key) {
        echo "KEY: '{$key}'\n";
    }

    echo "\n";

Result:

All Keys:

Increment by float

All Keys:

If I pop into redis-cli and check what is there:

redis 127.0.0.1:6379[10]> keys *
1) "\x00\x00\x00\x00\x00\x00\x00"

That doesn't look like the correct key name... :(

This doesn't seem to happen with Redis::incr() however.

Example:

<?php

    $redis = new redis;
    $redis->connect('127.0.0.1');
    $redis->select(10);
    $redis->flushDB();

    $redis->setOption(Redis::OPT_PREFIX, 'aaa:');

    echo "\nAll Keys:\n";
    foreach ($redis->keys("*") as $key) {
        echo "{$key}\n";
    }

    echo "\n\nIncrement by int\n";
    $redis->incr("foo", 1);

    echo "\n\nAll Keys:\n";
    foreach ($redis->keys("*") as $key) {
        echo "{$key}\n";
    }

    echo "\n";

Results:

All Keys:

Increment by int

All Keys:
aaa:foo

Looking at this: https://github.com/nicolasff/phpredis/blob/master/redis.c#L1335 I think you might have a null pointer here.

You might want to scoot that line down below:

cmd_len = redis_cmd_format_static(&cmd, "INCRBYFLOAT", "sf", key, key_len, val);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions