mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-16 00:30:21 +02:00
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:
parent
275c96dd1f
commit
6b1de860ab
4 changed files with 24 additions and 7 deletions
|
@ -1,4 +1,4 @@
|
|||
/* Copyright (C) 2009-2015 Free Software Foundation, Inc.
|
||||
/* Copyright (C) 2009-2015, 2019 Free Software Foundation, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public License
|
||||
|
@ -662,7 +662,11 @@ SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
|
|||
SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
|
||||
|
||||
ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
|
||||
memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
|
||||
if (byte_len != 0)
|
||||
/* Empty arrays may have elements == NULL. We must avoid passing
|
||||
NULL to memcpy, even if the length is zero, to avoid undefined
|
||||
behavior. */
|
||||
memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
|
||||
|
||||
scm_array_handle_release (&h);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue