Skip to content

Commit 6dd1c5f

Browse files
authored
fix: Memcached decrement() gives the wrong sign on a missing key (#10512)
* fix: MemcachedHandler::decrement() wrong sign on missing key MemcachedHandler::decrement() passed $offset as Memcached::decrement()'s initial_value, which is used as-is (not decremented from) when a key doesn't exist yet. That made a fresh key end up at +$offset instead of the -$offset every other cache handler (File, Redis, Predis) produces. Memcached counters are unsigned, so they can't hold a negative initial value the way the other handlers effectively can. Use 0 instead (also Memcached::decrement()'s own default), so a fresh key at least stops going the wrong direction. Updates the existing MemcachedHandlerTest::testDecrement() expectation and adds a changelog entry. * fix: address review feedback on Memcached decrement() fix * fix: correct expected type in MemcachedHandlerTest::testDecrement
1 parent b8866f2 commit 6dd1c5f

4 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎system/Cache/Handlers/MemcachedHandler.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ public function decrement(string $key, int $offset = 1): false|int
180180

181181
$key = static::validateKey($key, $this->prefix);
182182

183-
// FIXME: third parameter isn't other handler actions.
184-
185-
return $this->memcached->decrement($key, $offset, $offset, 60);
183+
// Memcached counters are unsigned and saturate at 0, so a missing
184+
// key can only ever be initialized to 0, never a negative value.
185+
// Use Memcached::decrement()'s own default of 0 as the initial
186+
// value instead of $offset, so a new key no longer starts positive.
187+
return $this->memcached->decrement($key, $offset, 0, 60);
186188
}
187189

188190
public function clean(): bool

‎tests/system/Cache/Handlers/MemcachedHandlerTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ public function testDecrement(): void
151151

152152
$this->assertSame(9, $memcachedHandler->decrement(self::$key1, 1));
153153
$this->assertFalse($memcachedHandler->decrement(self::$key2, 1));
154-
$this->assertSame(1, $memcachedHandler->decrement(self::$key3, 1));
154+
// A key that doesn't exist yet starts at 0, not at the offset
155+
// (Memcached counters are unsigned, so it can't start negative).
156+
$this->assertSame(0, $memcachedHandler->decrement(self::$key3, 5));
157+
// Memcached stores counter values as decimal strings on the wire, so
158+
// a plain get() on a counter key returns a string, not an int.
159+
$this->assertSame('0', $memcachedHandler->get(self::$key3));
155160
}
156161

157162
public function testClean(): void

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Bugs Fixed
4545
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.
4646
- **Honeypot:** Fixed a bug where bot detection returned an HTTP 500 response instead of 403 (Forbidden).
4747
- **Logger:** Fixed a bug where interpolating a log message with array or non-stringable context values could raise PHP warnings or errors.
48+
- **Cache:** Fixed ``MemcachedHandler::decrement()`` initializing a non-existent counter to the positive offset. Missing counters are now initialized to ``0``, reflecting Memcached's unsigned, saturating counter semantics.
4849

4950
See the repo's
5051
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

‎user_guide_src/source/libraries/caching.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ Class Reference
212212

213213
.. literalinclude:: caching/008.php
214214

215+
.. important:: Memcached counters are unsigned and saturate at ``0`` instead of
216+
going negative. Decrementing a key that does not yet exist initializes it
217+
to ``0`` (rather than ``-$offset``, as the File, Redis, and Predis handlers
218+
do), and decrementing an existing counter below ``0`` clamps it at ``0``.
219+
215220
.. php:method:: clean()
216221
217222
:returns: ``true`` on success, ``false`` on failure

0 commit comments

Comments
 (0)