Skip to content

Commit

Permalink
Handle a blank stat value (#388)
Browse files Browse the repository at this point in the history
A stat with a blank value makes the .stats() call fail with index error.

```
memcache_connection.stats()
File "/lib/python3.6/site-packages/pymemcache/client/base.py", line 741, in stats
result = self._fetch_cmd(b'stats', args, False)
File "/lib/python3.6/site-packages/pymemcache/client/base.py", line 921, in _fetch_cmd
result[key_value[1]] = key_value[2]
IndexError: list index out of range
```

The line in question we saw that caused this:

```
b'STAT ep_initfile '
```

This fixes things to no longer blow up.   I chose to use a blank binary string instead of None because I suspect other code might make assumptions about the data being binary strings due to this bug.

Co-authored-by: reecepeg <[email protected]>
  • Loading branch information
2 people authored and jparise committed Apr 1, 2022
1 parent 83a2056 commit 7acc9de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pymemcache/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def _fetch_cmd(self, name, keys, expect_cas):
result[key] = value
elif name == b'stats' and line.startswith(b'STAT'):
key_value = line.split()
result[key_value[1]] = key_value[2]
result[key_value[1]] = key_value[2] if len(key_value) > 2 else b""
elif name == b'stats' and line.startswith(b'ITEM'):
# For 'stats cachedump' commands
key_value = line.split()
Expand Down
5 changes: 5 additions & 0 deletions pymemcache/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,11 @@ def test_stats_with_args(self):
]
assert result == {b'fake_stats': 1}

def test_stats_with_blank_value(self):
client = self.make_client([b"STAT fake_stats \r\n", b"END\r\n"])
result = client.stats()
assert result == {b"fake_stats": b""}

def test_stats_conversions(self):
client = self.make_client([
# Most stats are converted to int
Expand Down

0 comments on commit 7acc9de

Please sign in to comment.