Giter Club home page Giter Club logo

Comments (7)

mhagstrand avatar mhagstrand commented on August 24, 2024

This is the result of an overflow. The value of 4294967296 (2^32) is just overflowing to 0. Incrementing by 4294967297 will result in an increment by 1.

PHP-memcached is casting the offset value of the increment and decrement function to an unsigned int. This can be seen in the php_memcached.c file with the code below:

if (incr) {
     status = memcached_increment(m_obj->memc, key, key_len, (unsigned int)offset, &value);
} else {

Fixing this is NOT as simple as just removing the cast from php_memcached.c. The memcached_increment function in libmemcached has a parameter called offset which is a uint32_t. This can be seen in the libmemcached file libmemcached/auto.cc with the code below:

memcached_return_t memcached_increment(memcached_st *memc,
                                       const char *key, size_t key_length,
                                       uint32_t offset,
                                       uint64_t *value)
{
      return memcached_increment_by_key(memc, key, key_length, key, key_length, offset, value);
} 

You should probably file this issue with libmemcached. The function memcached_increment_by_key uses a uint64_t for the offset. It could be used by PHP-memcached instead but the API docs for libmemcached still list the offset parameter in memcached_increment_by_key as uint32_t. That can be seen here:
http://docs.libmemcached.org/memcached_auto.html

from php-memcached.

sodabrew avatar sodabrew commented on August 24, 2024

Confirmed this is still a bug in 3.0.0 with PHP 7.0 and PHP 7.1.

from php-memcached.

sodabrew avatar sodabrew commented on August 24, 2024

Ah, the value can be 64-bit, but the increment is limited to 32-bits per API call. The protocol does allow 64-bit increments, so it might only be an API issue in libmemcached: https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L274

from php-memcached.

mhagstrand avatar mhagstrand commented on August 24, 2024

The problem still exists in the libmemcached:
http://bazaar.launchpad.net/~tangent-trunk/libmemcached/1.2/view/head:/libmemcached/auto.cc#L149

It is possible to work around this problem by replacing memcached_increment with memcached_increment_by_key in the code below.
https://github.com/php-memcached-dev/php-memcached/blob/master/php_memcached.c#L2224

That would look something like this:

    if (by_key) {
      if (incr) {
        status = memcached_increment_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
      } else {
        status = memcached_decrement_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
      }
    } else {
      if (incr) {
        status = memcached_increment_by_key(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
      } else {
        status = memcached_decrement_by_key(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
      }
    }

from php-memcached.

sodabrew avatar sodabrew commented on August 24, 2024

The documentation is mis-matched to the actual code! Yes, memcached_increment_by_key accepts a 64-bit offset. This should be straightforward to fix in the master branch.

from php-memcached.

mhagstrand avatar mhagstrand commented on August 24, 2024

I can put together a PR if you want or you can take care of it if you want.

from php-memcached.

sodabrew avatar sodabrew commented on August 24, 2024

Thanks for the offer! I've got it going in #306

from php-memcached.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.