summaryrefslogtreecommitdiffhomepage
path: root/types.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-08-22 11:54:40 +0200
committerGitHub <noreply@github.com>2023-08-22 11:54:40 +0200
commitce39326dbc26a1b9cfe063f8961da61013fd1bd1 (patch)
treefb5736feee7dce6e15f38d79c2fcfaf81dcd9ebc /types.c
parent5d265bd52f40dd94671783148e4e6ff068c8153a (diff)
parent8f852eaf9c870de2078649410f029e0846117de2 (diff)
Merge pull request #168 from jow-/fix-binary-string-compare
types: improve comparison reliability of binary strings
Diffstat (limited to 'types.c')
-rw-r--r--types.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/types.c b/types.c
index 226c874..c9472f7 100644
--- a/types.c
+++ b/types.c
@@ -2006,7 +2006,16 @@ ucv_compare(int how, uc_value_t *v1, uc_value_t *v2, int *deltap)
/* ... otherwise if both operands are strings, compare bytewise ... */
else if (t1 == UC_STRING && t2 == UC_STRING) {
- delta = strcmp(ucv_string_get(v1), ucv_string_get(v2));
+ u1 = ucv_string_length(v1);
+ u2 = ucv_string_length(v2);
+
+ delta = memcmp(ucv_string_get(v1), ucv_string_get(v2),
+ (u1 < u2) ? u1 : u2);
+
+ if (delta == 0 && u1 < u2)
+ delta = -1;
+ else if (delta == 0 && u1 > u2)
+ delta = 1;
}
/* handle non-string cases... */