Skip to content

Commit

Permalink
Consider also the key length in the comparison
Browse files Browse the repository at this point in the history
This make a difference when dictionary keys contain null-bytes.
  • Loading branch information
lelit committed Aug 24, 2017
1 parent a9959b2 commit 694fbac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rapidjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,10 @@ struct DictItem {
{}

bool operator<(const DictItem& other) const {
return strcmp(other.key_str, this->key_str) < 0;
Py_ssize_t tks = this->key_size;
Py_ssize_t oks = other.key_size;
int cmp = strncmp(other.key_str, this->key_str, oks < tks ? oks : tks);
return (cmp == 0) ? oks < tks : cmp < 0;
}
};

Expand Down
4 changes: 4 additions & 0 deletions tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def test_sort_keys(dumps):
assert dumps(o, sort_keys=True, indent=4) == expected2
assert dumps(o, sort_keys=True, indent=0) == expected0

o = {'a0': 'a0', 'a': 'a', 'a1': 'a1', 'a\x00b': 'a\x00b'}
assert sorted(o.keys()) == ['a', 'a\x00b', 'a0', 'a1']
assert dumps(o, sort_keys=True) == '{"a":"a","a\\u0000b":"a\\u0000b","a0":"a0","a1":"a1"}'


@pytest.mark.unit
def test_default():
Expand Down

0 comments on commit 694fbac

Please sign in to comment.