From 7e7501fa24bcd2e1484fee5648d88b8d94f0bf38 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 8 Mar 2019 20:38:22 +0100 Subject: [PATCH] unistr/u8-cmp: Fix undefined behaviour. Reported by Jeffrey Walton . * lib/unistr/u8-cmp.c (u8_cmp): Don't invoke memcmp if n is zero. --- ChangeLog | 6 ++++++ lib/unistr/u8-cmp.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 2e6126105d..074b1caca4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2019-03-08 Bruno Haible + + unistr/u8-cmp: Fix undefined behaviour. + Reported by Jeffrey Walton . + * lib/unistr/u8-cmp.c (u8_cmp): Don't invoke memcmp if n is zero. + 2019-03-08 Bruno Haible unictype/numeric: Fix undefined behaviour. diff --git a/lib/unistr/u8-cmp.c b/lib/unistr/u8-cmp.c index bd2f11bea9..5349ec63d0 100644 --- a/lib/unistr/u8-cmp.c +++ b/lib/unistr/u8-cmp.c @@ -26,5 +26,5 @@ int u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n) { /* Use the fact that the UTF-8 encoding preserves lexicographic order. */ - return memcmp ((const char *) s1, (const char *) s2, n); + return n == 0 ? 0 : memcmp ((const char *) s1, (const char *) s2, n); } -- 2.39.5