Avoid passing NULL to 'memcpy' and 'memcmp'.

Reported by Jeffrey Walton <noloader@gmail.com> in
<https://lists.gnu.org/archive/html/guile-devel/2019-03/msg00001.html>.

Note that C11 section 7.1.4 (Use of library functions) states that:
"unless explicitly stated otherwise in the detailed descriptions [of
library functions] that follow: If an argument to a function has an
invalid value (such as ... a null pointer ...) ..., the behavior is
undefined."  Note that 'strxfrm' is an example of a standard C function
that explicitly states otherwise, allowing NULL to be passed in the
first argument if the size argument is zero, but no similar allowance is
specified for 'memcpy' or 'memcmp'.

* libguile/bytevectors.c (scm_uniform_array_to_bytevector): Call memcpy
only if 'byte_len' is non-zero.
* libguile/srfi-14.c (charsets_equal): Call memcmp only if the number of
ranges is non-zero.
* libguile/stime.c (setzone): Pass 1-character buffer to
'scm_to_locale_stringbuf', instead of NULL.
* libguile/strings.c (scm_to_locale_stringbuf): Call memcpy only if the
number of bytes to copy is non-zero.
This commit is contained in:
Mark H Weaver 2019-04-01 22:11:35 -04:00 committed by Andy Wingo
commit 980d8265c2
4 changed files with 23 additions and 9 deletions

View file

@ -1,6 +1,4 @@
/* srfi-14.c --- SRFI-14 procedures for Guile
Copyright 2001,2004,2006-2007,2009,2011,2018
/* Copyright 2001,2004,2006-2007,2009,2011,2018-2019
Free Software Foundation, Inc.
This file is part of Guile.
@ -377,6 +375,12 @@ charsets_equal (scm_t_char_set *a, scm_t_char_set *b)
if (a->len != b->len)
return 0;
/* Empty charsets may have ranges == NULL. We must avoid passing
NULL to memcmp, even if the length is zero, to avoid undefined
behavior. */
if (a->len == 0)
return 1;
if (memcmp (a->ranges, b->ranges, sizeof (scm_t_char_range) * a->len) != 0)
return 0;