From c0d2a0be9c95cc38818810207986818b333bc1de Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 10 May 2024 23:26:51 +0200 Subject: Fix bug detected by GCC sanitizer --- lib/mbstring/u8cmp.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/mbstring') diff --git a/lib/mbstring/u8cmp.c b/lib/mbstring/u8cmp.c index 0059020..e54f984 100644 --- a/lib/mbstring/u8cmp.c +++ b/lib/mbstring/u8cmp.c @@ -2,8 +2,13 @@ #include "mbstring.h" +/* Technically you can’t pass nullptr to memcmp(), so we check for x.len and + y.len both being 0 */ + int u8cmp(struct u8view x, struct u8view y) { - return x.len != y.len ? (x.len > y.len ? +1 : -1) : memcmp(x.p, y.p, x.len); + if (x.len != y.len) + return x.len > y.len ? +1 : -1; + return x.len == 0 && y.len == 0 ? 0 : memcmp(x.p, y.p, x.len); } -- cgit v1.2.3