From 673ad15bb12a614f962f3eb1805bae0a4a6977d5 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Thu, 19 Dec 2024 21:29:16 +0100 Subject: [PATCH] fix: improve _z_string_compare Co-authored-by: Alexander Bushnev --- src/collections/string.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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) {