From e2e406e650e85c7cf5c4cf45eb3ad7f70580bab5 Mon Sep 17 00:00:00 2001
From: Alexey Gladkov <legion@kernel.org>
Date: Mon, 17 Aug 2026 13:23:31 +0200
Subject: [PATCH 2/2] Use reallocarray if available

Signed-off-by: Alexey Gladkov <legion@kernel.org>
---
 configure.ac                   |  2 +-
 src/Makefile.am                |  2 ++
 src/include/kbd/reallocarray.h | 26 ++++++++++++++++++++++++++
 src/libkfont/loadunimap.c      |  8 ++++++--
 src/libkfont/psffontop.c       |  4 +++-
 src/libkfont/psfxtable.c       |  8 ++++++--
 6 files changed, 44 insertions(+), 6 deletions(-)
 create mode 100644 src/include/kbd/reallocarray.h

Index: kbd-2.6.4/configure.ac
===================================================================
--- kbd-2.6.4.orig/configure.ac
+++ kbd-2.6.4/configure.ac
@@ -58,7 +58,7 @@ AC_FUNC_CLOSEDIR_VOID
 AC_FUNC_FORK
 AC_FUNC_STAT
 
-AC_CHECK_FUNCS([alarm memset setlocale strcasecmp strchr strdup strerror \
+AC_CHECK_FUNCS([alarm memset setlocale strcasecmp strchr reallocarray strdup strerror \
 strspn strstr strtol strtoul setpgrp malloc realloc])
 
 AC_SEARCH_LIBS([timer_create], [rt])
Index: kbd-2.6.4/src/libkfont/Makefile.am
===================================================================
--- kbd-2.6.4.orig/src/libkfont/Makefile.am
+++ kbd-2.6.4/src/libkfont/Makefile.am
@@ -53,6 +53,8 @@ else
 noinst_LTLIBRARIES = libkfont.la
 endif
 
+noinst_HEADERS = reallocarray.h
+
 %.pc: %.pc.in Makefile
 	$(SED) \
 	    -e 's,[@]VERSION[@],$(VERSION),g' \
Index: kbd-2.6.4/src/libkfont/loadunimap.c
===================================================================
--- kbd-2.6.4.orig/src/libkfont/loadunimap.c
+++ kbd-2.6.4/src/libkfont/loadunimap.c
@@ -19,6 +19,7 @@
 #include <linux/kd.h>
 
 #include <kbdfile.h>
+#include "reallocarray.h"
 
 #include "paths.h"
 #include "libcommon.h"
@@ -103,12 +104,15 @@ add_unipair(struct kfont_context *ctx,
 		unsigned short *listct)
 {
 	if (*listct == *listsz) {
+		struct unipair *new_list;
+
 		*listsz += 4096;
-		*list = realloc(*list, *listsz * sizeof(**list));
-		if (!*list) {
+		new_list = kbd_reallocarray(*list, *listsz, sizeof(**list));
+		if (!new_list) {
 			KFONT_ERR(ctx, "realloc: %m");
 			return -EX_OSERR;
 		}
+		*list = new_list;
 	}
 
 	(*list)[*listct].fontpos = fp;
Index: kbd-2.6.4/src/libkfont/psffontop.c
===================================================================
--- kbd-2.6.4.orig/src/libkfont/psffontop.c
+++ kbd-2.6.4/src/libkfont/psffontop.c
@@ -18,6 +18,8 @@
 #include "kfontP.h"
 #include "utf8.h"
 
+#include "reallocarray.h"
+
 static uint32_t
 assemble_uint32(unsigned char *ip)
 {
@@ -334,7 +336,7 @@ kfont_read_psffont(struct kfont_context
 		return -EX_DATAERR;
 	}
 
-	ptr = realloc(*uclistheadsp, uclist_count * sizeof(*ptr));
+	ptr = kbd_reallocarray(*uclistheadsp, uclist_count, sizeof(*ptr));
 	if (!ptr) {
 		KFONT_ERR(ctx, "realloc: %m");
 		return -EX_OSERR;
Index: kbd-2.6.4/src/libkfont/psfxtable.c
===================================================================
--- kbd-2.6.4.orig/src/libkfont/psfxtable.c
+++ kbd-2.6.4/src/libkfont/psfxtable.c
@@ -18,6 +18,8 @@
 #include "libcommon.h"
 #include "kfontP.h"
 
+#include "reallocarray.h"
+
 static int
 str_to_unicode(struct kfont_context *ctx,
 		const char *p, char **e, int base,
@@ -212,13 +214,15 @@ kfont_read_unicodetable(struct kfont_con
 		struct unicode_list **uclistheadsp)
 {
 	char buf[65536];
+	struct unicode_list *ptr;
 	unsigned int i;
 
-	*uclistheadsp = realloc(*uclistheadsp, fontlen * sizeof(struct unicode_list));
-	if (!*uclistheadsp) {
+	ptr = kbd_reallocarray(*uclistheadsp, fontlen, sizeof(*ptr));
+	if (!ptr) {
 		KFONT_ERR(ctx, "realloc: %m");
 		return -EX_OSERR;
 	}
+	*uclistheadsp = ptr;
 
 	for (i = 0; i < fontlen; i++) {
 		struct unicode_list *up = &((*uclistheadsp)[i]);
Index: kbd-2.6.4/src/libkfont/reallocarray.h
===================================================================
--- /dev/null
+++ kbd-2.6.4/src/libkfont/reallocarray.h
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: LGPL-2.0-or-later
+#ifndef KBD_REALLOCARRAY_H
+#define KBD_REALLOCARRAY_H
+
+#include "config.h"
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+static inline void *
+kbd_reallocarray(void *ptr, size_t nmemb, size_t size)
+{
+#ifdef HAVE_REALLOCARRAY
+	return reallocarray(ptr, nmemb, size);
+#else
+	if (size && nmemb > SIZE_MAX / size) {
+		errno = ENOMEM;
+		return NULL;
+	}
+
+	return realloc(ptr, nmemb * size);
+#endif
+}
+
+#endif /* KBD_REALLOCARRAY_H */
