From 7a6b7c96e730344f58e7fdf6b060f23c839bcf74 Mon Sep 17 00:00:00 2001 From: John Scott Date: Tue, 28 Mar 2023 02:20:28 -0400 Subject: [PATCH] Include musl libc's implementation of reallocarray() as a subproject Work smarter, not harder. --- .gitignore | 1 + meson.build | 3 ++- subprojects/packagefiles/reallocarray-musl.patch | 15 +++++++++++++++ subprojects/reallocarray-musl.wrap | 9 +++++++++ which.c | 9 +-------- 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 subprojects/packagefiles/reallocarray-musl.patch create mode 100644 subprojects/reallocarray-musl.wrap diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..821b172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +subprojects/reallocarray-musl/ diff --git a/meson.build b/meson.build index 9f90f7c..4a0d0d5 100644 --- a/meson.build +++ b/meson.build @@ -44,4 +44,5 @@ if has_advisory_info assert(cc.has_function('posix_madvise', dependencies: [rt], prefix: '#define _POSIX_C_SOURCE 200809L'), 'posix_madvise() missing') endif -which = executable('which', 'which.c', dependencies: [pthread, rt], install: true) +reallocarray = subproject('reallocarray-musl').get_variable('reallocarray_musl_dep') +which = executable('which', 'which.c', dependencies: [pthread, rt, reallocarray], install: true) diff --git a/subprojects/packagefiles/reallocarray-musl.patch b/subprojects/packagefiles/reallocarray-musl.patch new file mode 100644 index 0000000..a1d287d --- /dev/null +++ b/subprojects/packagefiles/reallocarray-musl.patch @@ -0,0 +1,15 @@ +diff -u0 -r -N musl/meson.build musl2/meson.build +--- musl/meson.build ++++ musl2/meson.build +@@ -0,0 +1,6 @@ ++# SPDX-FileCopyrightText: 2023 John Scott ++# SPDX-License-Identifier: GPL-3.0-or-later ++# Note that musl is under a different medley of licenses. ++# The above license and copyright information is only for this file itself. ++project('reallocarray_musl', 'c') ++reallocarray_musl_dep = declare_dependency(link_with: [static_library('reallocarray_musl', 'src/malloc/reallocarray.c')]) +diff -u0 -r -N musl/src/malloc/reallocarray.c musl2/src/malloc/reallocarray.c +--- musl/src/malloc/reallocarray.c ++++ musl2/src/malloc/reallocarray.c +@@ -1 +0,0 @@ +-#define _BSD_SOURCE diff --git a/subprojects/reallocarray-musl.wrap b/subprojects/reallocarray-musl.wrap new file mode 100644 index 0000000..4a81715 --- /dev/null +++ b/subprojects/reallocarray-musl.wrap @@ -0,0 +1,9 @@ +# SPDX-FileCopyrightText: 2023 John Scott +# SPDX-License-Identifier: GPL-3.0-or-later +# Note that the above is meant to indicate the license that applies +# to this very file, and not to Musl libc. +[wrap-git] +url = git://git.musl-libc.org/musl +revision = master +depth = 1 +diff_files = reallocarray-musl.patch diff --git a/which.c b/which.c index fe834d0..87f0078 100644 --- a/which.c +++ b/which.c @@ -21,18 +21,11 @@ #include #include #include +void *reallocarray(void *, size_t, size_t); /* A NULL-terminated list of directories in PATH. */ static char **list; -static void *reallocarray(void *p, size_t m, size_t n) { - if(n && m > SIZE_MAX / n) { - errno = ENOMEM; - return NULL; - } - return realloc(p, m * n); -} - static bool file_is_executable(const char filename[restrict static 1]) { struct stat st; if(stat(filename, &st) == -1 || !S_ISREG(st.st_mode)) { -- 2.45.2