return true;
- new_allocated = array->allocated * 2;
- if (new_allocated == 0)
+ if (array->allocated == 0)
new_allocated = 16;
+ else {
+ return_val_if_fail (SIZE_MAX / array->allocated >= 2, false);
+ new_allocated = array->allocated * 2;
+ }
if (new_allocated < length)
new_allocated = length;
- new_memory = realloc (array->elem, new_allocated * sizeof (void*));
+ new_memory = reallocarray (array->elem, new_allocated, sizeof (void*));
return_val_if_fail (new_memory != NULL, false);
array->elem = new_memory;
CK_ULONG at;
CK_ULONG j;
CK_ULONG i;
+ size_t length;
/* How many attributes we already have */
current = p11_attrs_count (attrs);
/* Reallocate for how many we need */
- attrs = realloc (attrs, (current + count_to_add + 1) * sizeof (CK_ATTRIBUTE));
+ length = current + count_to_add;
+ return_val_if_fail (current <= length && length < SIZE_MAX, NULL);
+ attrs = reallocarray (attrs, length + 1, sizeof (CK_ATTRIBUTE));
return_val_if_fail (attrs != NULL, NULL);
at = current;
#endif /* HAVE_STRNDUP */
+#ifndef HAVE_REALLOCARRAY
+
+void *
+reallocarray (void *ptr,
+ size_t nmemb,
+ size_t size)
+{
+ assert (nmemb > 0 && size > 0);
+ if (SIZE_MAX / nmemb < size) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ return realloc (ptr, nmemb * size);
+}
+
+#endif /* HAVE_MEMDUP */
+
#ifndef HAVE_STRCONCAT
#include <stdarg.h>
#endif /* HAVE_STRDUP */
+#ifndef HAVE_REALLOCARRAY
+
+void * reallocarray (void *ptr,
+ size_t nmemb,
+ size_t size);
+
+#endif /* HAVE_REALLOCARRAY */
+
#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#else
AC_CHECK_FUNCS([getprogname getexecname basename mkstemp mkdtemp])
AC_CHECK_FUNCS([getauxval issetugid getresuid secure_getenv])
AC_CHECK_FUNCS([strnstr memdup strndup strerror_l strerror_r])
+ AC_CHECK_FUNCS([reallocarray])
AC_CHECK_FUNCS([fdwalk])
AC_CHECK_FUNCS([setenv])
AC_CHECK_FUNCS([getpeereid])