summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-12-06 17:42:04 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-01-01 14:34:57 -0800
commit40bcc8c3303a858c786f1d6d04ae5bbdec6b04ff (patch)
tree1d6ad569496120004426e60a8c6d40b7584700d6
parent47f2e64d865568d4f00cb97e42ad5cfc47e9c161 (diff)
downloadxorg-util-makedepend-40bcc8c3303a858c786f1d6d04ae5bbdec6b04ff.tar.gz
Use reallocarray() on systems that provide it
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--configure.ac6
-rw-r--r--def.h5
-rw-r--r--include.c16
-rw-r--r--main.c4
-rw-r--r--parse.c9
5 files changed, 26 insertions, 14 deletions
diff --git a/configure.ac b/configure.ac
index 0ecfd7f..6cf0e3a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,6 +7,10 @@ AC_INIT([makedepend], [1.0.8],
[https://gitlab.freedesktop.org/xorg/util/makedepend/-/issues])
AC_CONFIG_SRCDIR([Makefile.am])
AC_CONFIG_HEADERS([makedepend-config.h])
+# Set common system defines for POSIX extensions, such as _GNU_SOURCE
+# Must be called before any macros that run the compiler (like AC_PROG_LIBTOOL)
+# to avoid autoconf errors.
+AC_USE_SYSTEM_EXTENSIONS
# Initialize Automake
AM_INIT_AUTOMAKE([foreign dist-xz])
@@ -21,7 +25,7 @@ XORG_DEFAULT_OPTIONS
XORG_WITH_LINT
dnl Checks for functions
-AC_CHECK_FUNCS([rename fchmod])
+AC_CHECK_FUNCS([rename fchmod reallocarray])
dnl Use 64-bit file operations on 32-bit systems that support them
AC_SYS_LARGEFILE
diff --git a/def.h b/def.h
index 676b395..34bc767 100644
--- a/def.h
+++ b/def.h
@@ -38,6 +38,11 @@ in this Software without prior written authorization from The Open Group.
#include <fcntl.h>
#include <sys/stat.h>
+#ifndef HAVE_REALLOCARRAY
+#define reallocarray(ptr, num, size) realloc(ptr, (num) * (size))
+#endif
+#define mallocarray(num, size) reallocarray(NULL, num, size)
+
#define MAXDEFINES 512
#define MAXFILES 2048
#define MAXINCFILES 128 /* "-include" files */
diff --git a/include.c b/include.c
index 9b1a9ae..38e3cd0 100644
--- a/include.c
+++ b/include.c
@@ -173,11 +173,12 @@ included_by(struct inclist *ip, struct inclist *newfile)
* If it is already on the list, don't stick it on again.
*/
if (ip->i_list == NULL) {
- ip->i_list = malloc(sizeof(struct inclist *) * ++ip->i_listlen);
- ip->i_merged = malloc(sizeof(boolean) * ip->i_listlen);
+ ip->i_listlen++;
+ ip->i_list = mallocarray(ip->i_listlen, sizeof(struct inclist *));
+ ip->i_merged = mallocarray(ip->i_listlen, sizeof(boolean));
}
else {
- for (int i = 0; i < ip->i_listlen; i++)
+ for (int i = 0; i < ip->i_listlen; i++) {
if (ip->i_list[i] == newfile) {
i = strlen(newfile->i_file);
if (!(ip->i_flags & INCLUDED_SYM) &&
@@ -197,9 +198,12 @@ included_by(struct inclist *ip, struct inclist *newfile)
}
return;
}
- ip->i_list = realloc(ip->i_list,
- sizeof(struct inclist *) * ++ip->i_listlen);
- ip->i_merged = realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen);
+ }
+ ip->i_listlen++;
+ ip->i_list = reallocarray(ip->i_list, ip->i_listlen,
+ sizeof(struct inclist *));
+ ip->i_merged = reallocarray(ip->i_merged, ip->i_listlen,
+ sizeof(boolean));
}
ip->i_list[ip->i_listlen - 1] = newfile;
ip->i_merged[ip->i_listlen - 1] = FALSE;
diff --git a/main.c b/main.c
index 571fe06..58178db 100644
--- a/main.c
+++ b/main.c
@@ -214,7 +214,7 @@ main(int argc, char *argv[])
}
if (p[-1])
nargc++;
- nargv = malloc(nargc * sizeof(char *));
+ nargv = mallocarray(nargc, sizeof(char *));
nargv[0] = argv[0];
argc = 1;
for (p = args; argc < nargc; p += strlen(p) + 1)
@@ -276,7 +276,7 @@ main(int argc, char *argv[])
if (numundefs == 1)
undeflist = malloc(sizeof(char *));
else
- undeflist = realloc(undeflist, numundefs * sizeof(char *));
+ undeflist = reallocarray(undeflist, numundefs, sizeof(char *));
if (argv[0][2] == '\0') {
if (argc < 2)
fatalerr("Missing argument for -U\n");
diff --git a/parse.c b/parse.c
index f3f569c..9727508 100644
--- a/parse.c
+++ b/parse.c
@@ -318,13 +318,12 @@ define2(const char *name, const char *val, struct inclist *file)
/* Make space if it's needed */
if (file->i_defs == NULL) {
- file->i_defs = malloc(sizeof(struct symtab *) * SYMTABINC);
+ file->i_defs = mallocarray(SYMTABINC, sizeof(struct symtab *));
file->i_ndefs = 0;
}
else if (!(file->i_ndefs % SYMTABINC))
- file->i_defs = realloc(file->i_defs,
- sizeof(struct symtab *) * (file->i_ndefs +
- SYMTABINC));
+ file->i_defs = reallocarray(file->i_defs, (file->i_ndefs + SYMTABINC),
+ sizeof(struct symtab *));
if (file->i_defs == NULL)
fatalerr("malloc()/realloc() failure in insert_defn()\n");
@@ -474,7 +473,7 @@ merge2defines(struct inclist *file1, struct inclist *file2)
if (deflen > 0) {
/* make sure deflen % SYMTABINC == 0 is still true */
deflen += (SYMTABINC - deflen % SYMTABINC) % SYMTABINC;
- i_defs = malloc(deflen * sizeof(struct symtab *));
+ i_defs = mallocarray(deflen, sizeof(struct symtab *));
if (i_defs == NULL)
return 0;
}