diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-05-10 23:26:51 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-05-10 23:26:51 +0200 |
commit | c0d2a0be9c95cc38818810207986818b333bc1de (patch) | |
tree | 7630e4b37a4682071ca7370823b3d2a7fefc38db | |
parent | 44eaa2984a59156ba41615388d54cdc09859fef4 (diff) |
Fix bug detected by GCC sanitizer
-rw-r--r-- | lib/mbstring/u8cmp.c | 7 |
1 files changed, 6 insertions, 1 deletions
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); } |