comparison lib/unistr/u8-cmp.c @ 40211:5f58d2ac35ea

unistr/u8-cmp: Fix undefined behaviour. Reported by Jeffrey Walton <noloader@gmail.com>. * lib/unistr/u8-cmp.c (u8_cmp): Don't invoke memcmp if n is zero.
author Bruno Haible <bruno@clisp.org>
date Fri, 08 Mar 2019 20:38:22 +0100
parents b06060465f09
children
comparison
equal deleted inserted replaced
40210:44073ad4207f 40211:5f58d2ac35ea
24 24
25 int 25 int
26 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n) 26 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
27 { 27 {
28 /* Use the fact that the UTF-8 encoding preserves lexicographic order. */ 28 /* Use the fact that the UTF-8 encoding preserves lexicographic order. */
29 return memcmp ((const char *) s1, (const char *) s2, n); 29 return n == 0 ? 0 : memcmp ((const char *) s1, (const char *) s2, n);
30 } 30 }