diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2024-01-17 09:26:40 -0800 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2024-01-17 09:26:40 -0800 |
commit | 71d8e89f43de90571ed7e7486035f56190f10888 (patch) | |
tree | b6b699c6ac7638617558d04102fa22ab31297bfa | |
parent | 2b9d8f5bac5d2352f8021548b4852014ed683b2c (diff) |
Use reallocarray() if available
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | configure.ac | 7 | ||||
-rw-r--r-- | xlsfonts.c | 14 |
2 files changed, 18 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac index f70eab3..a7660c4 100644 --- a/configure.ac +++ b/configure.ac @@ -27,6 +27,7 @@ AC_INIT(xlsfonts, [1.0.7], [https://gitlab.freedesktop.org/xorg/app/xlsfonts/issues],xlsfonts) AC_CONFIG_SRCDIR([Makefile.am]) AC_CONFIG_HEADERS([config.h]) +AC_USE_SYSTEM_EXTENSIONS # Initialize Automake AM_INIT_AUTOMAKE([foreign dist-xz]) @@ -40,6 +41,12 @@ XORG_DEFAULT_OPTIONS # Checks for pkg-config packages PKG_CHECK_MODULES(XLSFONTS, [x11 xproto >= 7.0.17]) +# 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]) @@ -36,6 +36,14 @@ in this Software without prior written authorization from The Open Group. #include <limits.h> #include "dsimple.h" +#ifdef HAVE_BSD_STDLIB_H +#include <bsd/stdlib.h> +#endif + +#ifndef HAVE_REALLOCARRAY +#define reallocarray(old, num, size) realloc(old, (num) * (size)) +#endif + #define N_START INT_MAX /* Maximum # of fonts to start with (should * always be be > 10000 as modern OSes like * Solaris 8 already have more than 9000 XLFD @@ -202,7 +210,7 @@ get_list(const char *pattern) return; } - font_list = realloc(font_list, (font_cnt + 1) * sizeof(FontList)); + font_list = reallocarray(font_list, (font_cnt + 1), sizeof(FontList)); if (font_list == NULL) Fatal_Error("Out of memory!"); font_list[font_cnt].name = pattern; @@ -241,8 +249,8 @@ get_list(const char *pattern) nnames = available * 2; } - font_list = realloc(font_list, - (font_cnt + available) * sizeof(FontList)); + font_list = reallocarray(font_list, + (font_cnt + available), sizeof(FontList)); if (font_list == NULL) Fatal_Error("Out of memory!"); for (int i = 0; i < available; i++) { |