summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-08-13 14:50:51 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2022-08-13 15:35:32 -0700
commit7e039ed98ad9ef4083a01d3e5a03e54302706550 (patch)
treea4b21509e6bcf2dcccce64c8387964668441c085
parent23fa33d7061a2bba7f0732ea0c8c324a560a65d3 (diff)
Use reallocarray() if it is available in libc or libbsd
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--configure.ac6
-rw-r--r--fslsfonts.c12
2 files changed, 16 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index fe1e011..bff7560 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,6 +40,12 @@ XORG_DEFAULT_OPTIONS
# Checks for pkg-config packages
PKG_CHECK_MODULES(FSLSFONTS, [xproto >= 7.0.25 libfs])
+# Checks for library functions
+AC_SEARCH_LIBS([reallocarray], [bsd])
+AS_IF([test "x$ac_cv_search_reallocarray" = "x-lbsd"],
+ [AC_CHECK_HEADERS([bsd/stdlib.h])])
+AC_CHECK_FUNCS([reallocarray])
+
AC_CONFIG_FILES([
Makefile
man/Makefile])
diff --git a/fslsfonts.c b/fslsfonts.c
index 3608e5a..ce8f7ab 100644
--- a/fslsfonts.c
+++ b/fslsfonts.c
@@ -53,6 +53,14 @@ in this Software without prior written authorization from The Open Group.
#include <stdlib.h>
#include <assert.h>
+#ifdef HAVE_BSD_STDLIB_H
+#include <bsd/stdlib.h>
+#endif
+
+#ifndef HAVE_REALLOCARRAY
+#define reallocarray(old, num, size) realloc(old, (num) * (size))
+#endif
+
#ifndef N_START
#define N_START 1000 /* Maximum # of fonts to start with */
#endif
@@ -267,8 +275,8 @@ get_list(const char *pattern)
else {
FontList *old_list = font_list;
- font_list = realloc(old_list, (font_cnt + (unsigned) available)
- * sizeof(FontList));
+ font_list = reallocarray(old_list, (font_cnt + (unsigned) available),
+ sizeof(FontList));
if (font_list == NULL) {
free(old_list);