>From f6f567a94d1f34b96ae05e2354bfaa994c9de840 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 9 Mar 2019 00:01:47 +0100 Subject: [PATCH 3/4] unistr/*, uniconv/*: Fix undefined behaviour. Reported by Jeffrey Walton . * lib/unistr/u-cpy.h (FUNC): Don't invoke memcpy with a zero size. * lib/unistr/u-cpy-alloc.h (FUNC): Likewise. * lib/uniconv/u8-conv-from-enc.c (u8_conv_from_encoding): Likewise. * lib/uniconv/u8-conv-to-enc.c (u8_conv_to_encoding): Likewise. --- ChangeLog | 9 +++++++++ lib/uniconv/u8-conv-from-enc.c | 3 ++- lib/uniconv/u8-conv-to-enc.c | 3 ++- lib/unistr/u-cpy-alloc.h | 3 ++- lib/unistr/u-cpy.h | 3 ++- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 074b1ca..687357e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2019-03-08 Bruno Haible + unistr/*, uniconv/*: Fix undefined behaviour. + Reported by Jeffrey Walton . + * lib/unistr/u-cpy.h (FUNC): Don't invoke memcpy with a zero size. + * lib/unistr/u-cpy-alloc.h (FUNC): Likewise. + * lib/uniconv/u8-conv-from-enc.c (u8_conv_from_encoding): Likewise. + * lib/uniconv/u8-conv-to-enc.c (u8_conv_to_encoding): Likewise. + +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. diff --git a/lib/uniconv/u8-conv-from-enc.c b/lib/uniconv/u8-conv-from-enc.c index 623dc1f..f8e9bd9 100644 --- a/lib/uniconv/u8-conv-from-enc.c +++ b/lib/uniconv/u8-conv-from-enc.c @@ -77,7 +77,8 @@ u8_conv_from_encoding (const char *fromcode, } } - memcpy ((char *) result, src, srclen); + if (srclen > 0) + memcpy ((char *) result, src, srclen); *lengthp = srclen; return result; } diff --git a/lib/uniconv/u8-conv-to-enc.c b/lib/uniconv/u8-conv-to-enc.c index ff0bc64..56e3121 100644 --- a/lib/uniconv/u8-conv-to-enc.c +++ b/lib/uniconv/u8-conv-to-enc.c @@ -60,7 +60,8 @@ u8_conv_to_encoding (const char *tocode, } } - memcpy (result, (const char *) src, srclen); + if (srclen > 0) + memcpy (result, (const char *) src, srclen); *lengthp = srclen; return result; } diff --git a/lib/unistr/u-cpy-alloc.h b/lib/unistr/u-cpy-alloc.h index 8afa66b..55fef32 100644 --- a/lib/unistr/u-cpy-alloc.h +++ b/lib/unistr/u-cpy-alloc.h @@ -33,7 +33,8 @@ FUNC (const UNIT *s, size_t n) for (; n > 0; n--) *destptr++ = *s++; #else - memcpy ((char *) dest, (const char *) s, n * sizeof (UNIT)); + if (n > 0) + memcpy ((char *) dest, (const char *) s, n * sizeof (UNIT)); #endif } return dest; diff --git a/lib/unistr/u-cpy.h b/lib/unistr/u-cpy.h index 1dc2129..c023a27 100644 --- a/lib/unistr/u-cpy.h +++ b/lib/unistr/u-cpy.h @@ -26,7 +26,8 @@ FUNC (UNIT *dest, const UNIT *src, size_t n) for (; n > 0; n--) *destptr++ = *src++; #else - memcpy ((char *) dest, (const char *) src, n * sizeof (UNIT)); + if (n > 0) + memcpy ((char *) dest, (const char *) src, n * sizeof (UNIT)); #endif return dest; } -- 2.7.4