diff --git a/src/collections/string.c b/src/collections/string.c index 7cc72ce1f..e3ce9f422 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -102,10 +102,20 @@ void _z_string_free(_z_string_t **str) { } int _z_string_compare(const _z_string_t *left, const _z_string_t *right) { - if (_z_string_len(left) <= _z_string_len(right)) { - return strncmp(_z_string_data(left), _z_string_data(right), _z_string_len(left)); + size_t len_left = _z_string_len(left); + size_t len_right = _z_string_len(right); + + int result = strncmp(_z_string_data(left), _z_string_data(right), len_left < len_right ? len_left : len_right); + + if (result == 0) { + if (len_left < len_right) { + return -1; + } else if (len_left > len_right) { + return 1; + } } - return strncmp(_z_string_data(left), _z_string_data(right), _z_string_len(right)); + + return result; } bool _z_string_equals(const _z_string_t *left, const _z_string_t *right) {