From 71d8e89f43de90571ed7e7486035f56190f10888 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Wed, 17 Jan 2024 09:26:40 -0800 Subject: Use reallocarray() if available Signed-off-by: Alan Coopersmith --- xlsfonts.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'xlsfonts.c') diff --git a/xlsfonts.c b/xlsfonts.c index 0afc53f..489b49f 100644 --- a/xlsfonts.c +++ b/xlsfonts.c @@ -36,6 +36,14 @@ in this Software without prior written authorization from The Open Group. #include #include "dsimple.h" +#ifdef HAVE_BSD_STDLIB_H +#include +#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++) { -- cgit v1.2.3