From 4faf0f63f0563513252967a2da1c51de6f5f7b0c Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 10 May 2020 11:46:16 -0700 Subject: Use reallocarray() when adding members to array in split_into_words() Signed-off-by: Alan Coopersmith --- process.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'process.c') diff --git a/process.c b/process.c index b6e2591..7d2a9e7 100644 --- a/process.c +++ b/process.c @@ -37,6 +37,7 @@ from The Open Group. #include "xauth.h" #include #include +#include #include #ifndef WIN32 #include @@ -251,6 +252,18 @@ skip_nonspace(register char *s) return s; } +#ifndef HAVE_REALLOCARRAY +static inline void * +reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb > 0) && (SIZE_MAX / nmemb < size)) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} +#endif + static const char ** split_into_words(char *src, int *argcp) /* argvify string */ { @@ -280,7 +293,7 @@ split_into_words(char *src, int *argcp) /* argvify string */ if (cur == total) { const char **new_argv; total += WORDSTOALLOC; - new_argv = realloc (argv, total * sizeof (char *)); + new_argv = reallocarray (argv, total, sizeof (char *)); if (new_argv != NULL) { argv = new_argv; } else { -- cgit v1.2.1