Skip to content

Commit 94edf8d

Browse files
committed
Support for 64-bit offsets
1 parent 3727cfc commit 94edf8d

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

php_memcached.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,6 @@ static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key,
21862186
{
21872187
zend_string *key, *server_key = NULL;
21882188
zend_long offset = 1;
2189-
uint32_t offset32 = 0;
21902189
uint64_t value = UINT64_MAX, initial = 0;
21912190
time_t expiry = 0;
21922191
memcached_return status;
@@ -2213,23 +2212,18 @@ static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key,
22132212
RETURN_FALSE;
22142213
}
22152214

2216-
if (offset > UINT_MAX) {
2217-
php_error_docref(NULL, E_WARNING, "offset cannot be larger than %d", UINT_MAX);
2218-
RETURN_FALSE;
2219-
}
2220-
22212215
if ((!by_key && n_args < 3) || (by_key && n_args < 4)) {
22222216
if (by_key) {
22232217
if (incr) {
2224-
status = memcached_increment_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, &value);
2218+
status = memcached_increment_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
22252219
} else {
2226-
status = memcached_decrement_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, &value);
2220+
status = memcached_decrement_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
22272221
}
22282222
} else {
22292223
if (incr) {
2230-
status = memcached_increment(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, &value);
2224+
status = memcached_increment_by_key(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
22312225
} else {
2232-
status = memcached_decrement(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, &value);
2226+
status = memcached_decrement_by_key(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), ZSTR_VAL(key), ZSTR_LEN(key), offset, &value);
22332227
}
22342228
}
22352229

@@ -2243,15 +2237,15 @@ static void php_memc_incdec_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key,
22432237
}
22442238
if (by_key) {
22452239
if (incr) {
2246-
status = memcached_increment_with_initial_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, initial, expiry, &value);
2240+
status = memcached_increment_with_initial_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), offset, initial, expiry, &value);
22472241
} else {
2248-
status = memcached_decrement_with_initial_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, initial, expiry, &value);
2242+
status = memcached_decrement_with_initial_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), offset, initial, expiry, &value);
22492243
}
22502244
} else {
22512245
if (incr) {
2252-
status = memcached_increment_with_initial(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, initial, expiry, &value);
2246+
status = memcached_increment_with_initial(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), offset, initial, expiry, &value);
22532247
} else {
2254-
status = memcached_decrement_with_initial(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), (unsigned int)offset, initial, expiry, &value);
2248+
status = memcached_decrement_with_initial(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), offset, initial, expiry, &value);
22552249
}
22562250
}
22572251
if (s_should_retry_write(intern, status) && retries-- > 0) {

tests/incrdecr.phpt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ $m->decrement('foo', 2);
2929
var_dump($m->get('foo'));
3030

3131
error_reporting(0);
32-
echo "Invalid offset\n";
32+
echo "Negative offset\n";
33+
3334
$m->increment('foo', -1);
3435
echo $php_errormsg, "\n";
3536
var_dump($m->get('foo'));
37+
3638
$m->decrement('foo', -1);
3739
echo $php_errormsg, "\n";
3840
var_dump($m->get('foo'));
41+
42+
echo "Enormous offset\n";
3943
$m->increment('foo', 4294967295);
4044
echo $php_errormsg, "\n";
4145
var_dump($m->get('foo'));
46+
4247
$m->decrement('foo', 4294967295);
4348
echo $php_errormsg, "\n";
4449
var_dump($m->get('foo'));
@@ -57,10 +62,11 @@ int(2)
5762
int(4)
5863
int(3)
5964
int(1)
60-
Invalid offset
61-
Memcached::increment(): offset cannot be negative
65+
Negative offset
66+
Memcached::increment(): offset cannot be a negative value
6267
int(1)
63-
Memcached::decrement(): offset cannot be negative
68+
Memcached::decrement(): offset cannot be a negative value
6469
int(1)
65-
Memcached::decrement(): offset cannot be larger than 2147483647
70+
Enormous offset
71+
int(4294967296)
6672
int(1)

tests/incrdecr_bykey.phpt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@ $m->decrementByKey('foo', 'foo', 2);
2626
var_dump($m->get('foo'));
2727

2828
error_reporting(0);
29-
echo "Invalid offset\n";
29+
echo "Negative offset\n";
30+
3031
$m->incrementByKey('foo', 'foo', -1);
3132
echo $php_errormsg, "\n";
3233
var_dump($m->get('foo'));
34+
3335
$m->decrementByKey('foo', 'foo', -1);
3436
echo $php_errormsg, "\n";
3537
var_dump($m->get('foo'));
3638

39+
echo "Enormous offset\n";
40+
$m->increment('foo', 4294967295);
41+
echo $php_errormsg, "\n";
42+
var_dump($m->get('foo'));
43+
44+
$m->decrement('foo', 4294967295);
45+
echo $php_errormsg, "\n";
46+
var_dump($m->get('foo'));
47+
3748
--EXPECT--
3849
Not there
3950
bool(false)
@@ -45,8 +56,11 @@ int(2)
4556
int(4)
4657
int(3)
4758
int(1)
48-
Invalid offset
49-
Memcached::incrementByKey(): offset cannot be negative
59+
Negative offset
60+
Memcached::incrementByKey(): offset cannot be a negative value
61+
int(1)
62+
Memcached::decrementByKey(): offset cannot be a negative value
5063
int(1)
51-
Memcached::decrementByKey(): offset cannot be negative
64+
Enormous offset
65+
int(4294967296)
5266
int(1)

0 commit comments

Comments
 (0)