-/* 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
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);
/* srfi-14.c --- SRFI-14 procedures for Guile
*
- * Copyright (C) 2001, 2004, 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2004, 2006, 2007, 2009, 2011,
+ * 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
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;
if (!SCM_UNBNDP (zone))
{
static char *tmpenv[2];
+ char dummy_buf[1];
char *buf;
size_t zone_len;
- zone_len = scm_to_locale_stringbuf (zone, NULL, 0);
+ zone_len = scm_to_locale_stringbuf (zone, dummy_buf, 0);
buf = scm_malloc (zone_len + sizeof (tzvar) + 1);
strcpy (buf, tzvar);
buf[sizeof(tzvar)-1] = '=';
/* Copyright (C) 1995, 1996, 1998, 2000, 2001, 2004, 2006,
- * 2008-2016, 2018 Free Software Foundation, Inc.
+ * 2008-2016, 2018, 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
size_t
scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
{
- size_t len;
+ size_t len, copy_len;
char *result = NULL;
if (!scm_is_string (str))
scm_wrong_type_arg_msg (NULL, 0, str, "string");
result = scm_to_locale_stringn (str, &len);
- memcpy (buf, result, (len > max_len) ? max_len : len);
+ copy_len = (len > max_len) ? max_len : len;
+ if (copy_len != 0)
+ /* Some users of 'scm_to_locale_stringbuf' may pass NULL for buf
+ when max_len is zero, and yet we must avoid passing NULL to
+ memcpy to avoid undefined behavior. */
+ memcpy (buf, result, copy_len);
free (result);
scm_remember_upto_here_1 (str);