|
| 1 | +SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> |
| 2 | +SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +diff --git utils-std-0.1.1/cmd/cut.c utils-std-0.1.1/cmd/cut.c |
| 5 | +index e56a4ca..2481759 100644 |
| 6 | +--- utils-std-0.1.1/cmd/cut.c |
| 7 | ++++ utils-std-0.1.1/cmd/cut.c |
| 8 | +@@ -5,8 +5,8 @@ |
| 9 | + #define _POSIX_C_SOURCE 202405L |
| 10 | + |
| 11 | + #include "../config.h" |
| 12 | +-#include "../lib/reallocarray.h" |
| 13 | + #include "../libutils/getopt_nolong.h" |
| 14 | ++#include "../libutils/reallocarray.h" |
| 15 | + |
| 16 | + #include <assert.h> |
| 17 | + #include <ctype.h> |
| 18 | +@@ -121,7 +121,7 @@ parse_list(char *s) |
| 19 | + |
| 20 | + if(max > list_len) |
| 21 | + { |
| 22 | +- list = reallocarray(list, max, sizeof(*list)); |
| 23 | ++ list = utils_reallocarray(list, max, sizeof(*list)); |
| 24 | + if(list == NULL) |
| 25 | + { |
| 26 | + fprintf(stderr, "%s: error: Failed memory allocation: %s\n", argv0, strerror(errno)); |
| 27 | +@@ -285,7 +285,7 @@ cut_c(FILE *in, const char *filename) |
| 28 | + |
| 29 | + if(nread > line_wsz) |
| 30 | + { |
| 31 | +- line_w = reallocarray(line_w, nread, sizeof(*line_w)); |
| 32 | ++ line_w = utils_reallocarray(line_w, nread, sizeof(*line_w)); |
| 33 | + if(line_w == NULL) |
| 34 | + { |
| 35 | + fprintf(stderr, "%s: error: Failed memory allocation: %s\n", argv0, strerror(errno)); |
| 36 | +diff --git utils-std-0.1.1/cmd/df.c utils-std-0.1.1/cmd/df.c |
| 37 | +index 85627a0..61082ca 100644 |
| 38 | +--- utils-std-0.1.1/cmd/df.c |
| 39 | ++++ utils-std-0.1.1/cmd/df.c |
| 40 | +@@ -6,9 +6,9 @@ |
| 41 | + #define _DEFAULT_SOURCE // mntent in glibc 2.19+ |
| 42 | + |
| 43 | + #include "../config.h" |
| 44 | +-#include "../lib/reallocarray.h" |
| 45 | + #include "../libutils/getopt_nolong.h" |
| 46 | + #include "../libutils/humanize.h" |
| 47 | ++#include "../libutils/reallocarray.h" |
| 48 | + |
| 49 | + #include <ctype.h> // iscntrl, isspace |
| 50 | + #include <errno.h> // errno |
| 51 | +@@ -249,7 +249,7 @@ main(int argc, char *argv[]) |
| 52 | + // So reallocarray to the rescue |
| 53 | + size_t devices_found = 0; |
| 54 | + size_t devices_len = 100; |
| 55 | +- devices = reallocarray(NULL, devices_len, sizeof(dev_t)); |
| 56 | ++ devices = utils_reallocarray(NULL, devices_len, sizeof(dev_t)); |
| 57 | + if(!devices) |
| 58 | + { |
| 59 | + fprintf(stderr, |
| 60 | +@@ -354,7 +354,7 @@ main(int argc, char *argv[]) |
| 61 | + devices_len *= 2; |
| 62 | + if(devices_len <= 0) abort(); |
| 63 | + |
| 64 | +- devices = reallocarray(devices, devices_len, sizeof(dev_t)); |
| 65 | ++ devices = utils_reallocarray(devices, devices_len, sizeof(dev_t)); |
| 66 | + |
| 67 | + if(!devices) |
| 68 | + { |
| 69 | +diff --git utils-std-0.1.1/cmd/join.c utils-std-0.1.1/cmd/join.c |
| 70 | +index ed93d92..f411a0a 100644 |
| 71 | +--- utils-std-0.1.1/cmd/join.c |
| 72 | ++++ utils-std-0.1.1/cmd/join.c |
| 73 | +@@ -33,12 +33,11 @@ |
| 74 | + * SUCH DAMAGE. |
| 75 | + */ |
| 76 | + |
| 77 | +-#define _POSIX_C_SOURCE 202405L |
| 78 | + #define _BSD_SOURCE // fgetln, strsep |
| 79 | + |
| 80 | +-#include "../lib/reallocarray.h" |
| 81 | + #include "../libutils/err.h" |
| 82 | + #include "../libutils/getopt_nolong.h" |
| 83 | ++#include "../libutils/reallocarray.h" |
| 84 | + |
| 85 | + #include <assert.h> |
| 86 | + #include <errno.h> |
| 87 | +@@ -304,7 +303,7 @@ slurp(INPUT *F) |
| 88 | + cnt = F->setalloc; |
| 89 | + F->setalloc += 50; |
| 90 | + if(F->setalloc <= 0) utils_errx(1, "slurp setalloc overflow"); |
| 91 | +- if((F->set = reallocarray(F->set, F->setalloc, sizeof(LINE))) == NULL) |
| 92 | ++ if((F->set = utils_reallocarray(F->set, F->setalloc, sizeof(LINE))) == NULL) |
| 93 | + utils_err(1, "Failed (re)allocating slurp set"); |
| 94 | + memset(F->set + cnt, 0, 50 * sizeof(LINE)); |
| 95 | + |
| 96 | +@@ -356,7 +355,7 @@ slurp(INPUT *F) |
| 97 | + { |
| 98 | + lp->fieldalloc += 50; |
| 99 | + if(lp->fieldalloc < 0) utils_errx(1, "slurp fieldalloc overflow"); |
| 100 | +- if((lp->fields = reallocarray(lp->fields, lp->fieldalloc, sizeof(char *))) == NULL) |
| 101 | ++ if((lp->fields = utils_reallocarray(lp->fields, lp->fieldalloc, sizeof(char *))) == NULL) |
| 102 | + utils_err(1, "Failed (re)allocating slurp fieldalloc"); |
| 103 | + } |
| 104 | + lp->fields[lp->fieldcnt++] = fieldp; |
| 105 | +@@ -581,7 +580,7 @@ fieldarg(char *option) |
| 106 | + { |
| 107 | + olistalloc += 50; |
| 108 | + if(olistalloc <= 0) utils_errx(1, "fieldarg olistalloc overflow"); |
| 109 | +- if((olist = reallocarray(olist, olistalloc, sizeof(OLIST))) == NULL) |
| 110 | ++ if((olist = utils_reallocarray(olist, olistalloc, sizeof(OLIST))) == NULL) |
| 111 | + utils_err(1, "Failed (re)allocating fieldarg"); |
| 112 | + } |
| 113 | + olist[olistcnt].filenum = filenum; |
| 114 | +diff --git utils-std-0.1.1/common.mk utils-std-0.1.1/common.mk |
| 115 | +index 0b29a48..8d5be14 100644 |
| 116 | +--- utils-std-0.1.1/common.mk |
| 117 | ++++ utils-std-0.1.1/common.mk |
| 118 | +@@ -9,4 +9,4 @@ lib/tr_str.o: lib/tr_str.c lib/tr_str.h |
| 119 | + cmd/cat: cmd/cat.c libutils/fs.o libutils/getopt_nolong.o |
| 120 | + cmd/printf: cmd/printf.c |
| 121 | + cmd/rm: cmd/rm.c libutils/consent.o libutils/getopt_nolong.o |
| 122 | +-cmd/tr: cmd/tr.c lib/tr_str.o libutils/err.o libutils/getopt_nolong.o |
| 123 | ++cmd/tr: cmd/tr.c lib/tr_str.o libutils/err.o libutils/getopt_nolong.o libutils/reallocarray.o |
| 124 | +diff --git utils-std-0.1.1/configure utils-std-0.1.1/configure |
| 125 | +index 7ccef56..d9905d2 100755 |
| 126 | +--- utils-std-0.1.1/configure |
| 127 | ++++ utils-std-0.1.1/configure |
| 128 | +@@ -146,14 +146,14 @@ add_commands() { |
| 129 | + # when there's 3+ commands starting with the same letter, put them on a new line |
| 130 | + commands=" |
| 131 | + arch base64 basename |
| 132 | +- cat chmod chown chroot cksum cmp date dirname |
| 133 | ++ cat chmod chown chroot cksum cmp cut date dirname |
| 134 | + echo env expr false getconf head id install link logname |
| 135 | + mesg mkdir mkfifo mktemp mv |
| 136 | + nice nohup nproc |
| 137 | + paste pathchk printf pwd |
| 138 | + realpath renice rm rmdir |
| 139 | + seq sha1sum sha256sum sha512sum shuf sleep split strings sync |
| 140 | +- tee test time timeout touch true truncate tty |
| 141 | ++ tee test time timeout touch tr true truncate tty |
| 142 | + uname uniq unlink |
| 143 | + wc which whoami yes |
| 144 | + " |
| 145 | +@@ -254,8 +254,7 @@ add_commands() { |
| 146 | + |
| 147 | + echo |
| 148 | + |
| 149 | +-check_header mntent.h |
| 150 | +-has_mntent_h=$? |
| 151 | ++check_header mntent.h && add_commands df |
| 152 | + |
| 153 | + check_header sys/sysmacros.h && add_commands mknod |
| 154 | + |
| 155 | +@@ -265,16 +264,13 @@ add_commands() { |
| 156 | + check_conftest configure.d/sendfile_linux.c && cpp_define HAS_SENDFILE |
| 157 | + check_conftest configure.d/copy_file_range.c && cpp_define HAS_COPY_FILE_RANGE |
| 158 | + |
| 159 | +-check_conftest configure.d/reallocarray.c |
| 160 | +-has_reallocarray=$? |
| 161 | +-test 0 -eq $has_reallocarray && add_commands tr cut |
| 162 | ++check_conftest configure.d/reallocarray.c && cpp_define HAS_REALLOCARRAY |
| 163 | + |
| 164 | + check_conftest configure.d/getopt_long.c && cpp_define HAS_GETOPT_LONG |
| 165 | + |
| 166 | + check_conftest configure.d/syncfs.c && cpp_define HAS_SYNCFS |
| 167 | + |
| 168 | +-check_conftest configure.d/fgetln.c |
| 169 | +-has_fgetln=$? |
| 170 | ++check_conftest configure.d/fgetln.c && add_commands join |
| 171 | + |
| 172 | + check_conftest configure.d/mkstemps.c && cpp_define HAS_MKSTEMPS |
| 173 | + check_conftest configure.d/mkdtemps.c && cpp_define HAS_MKDTEMPS |
| 174 | +@@ -288,13 +284,6 @@ add_commands() { |
| 175 | + |
| 176 | + echo |
| 177 | + |
| 178 | +-if test 0 -eq $has_reallocarray; then |
| 179 | +- test 0 -eq $has_mntent_h && add_commands df |
| 180 | +- test 0 -eq $has_fgetln && add_commands join |
| 181 | +-fi |
| 182 | +- |
| 183 | +-echo |
| 184 | +- |
| 185 | + ## Configuration write |
| 186 | + |
| 187 | + printf 'Writing to config.mk ...' |
| 188 | +diff --git utils-std-0.1.1/configure.d/reallocarray.c utils-std-0.1.1/configure.d/reallocarray.c |
| 189 | +index 022df8f..426a6ce 100644 |
| 190 | +--- utils-std-0.1.1/configure.d/reallocarray.c |
| 191 | ++++ utils-std-0.1.1/configure.d/reallocarray.c |
| 192 | +@@ -3,8 +3,7 @@ |
| 193 | + // SPDX-License-Identifier: MPL-2.0 |
| 194 | + |
| 195 | + #define _POSIX_C_SOURCE 202405L |
| 196 | +- |
| 197 | +-#include "../lib/reallocarray.h" |
| 198 | ++#define _BSD_SOURCE |
| 199 | + |
| 200 | + #include <stdlib.h> // reallocarray |
| 201 | + |
| 202 | +diff --git utils-std-0.1.1/lib/reallocarray.h utils-std-0.1.1/lib/reallocarray.h |
| 203 | +deleted file mode 100644 |
| 204 | +index 2d91abd..0000000 |
| 205 | +--- utils-std-0.1.1/lib/reallocarray.h |
| 206 | ++++ /dev/null |
| 207 | +@@ -1,19 +0,0 @@ |
| 208 | +-// utils-std: Collection of commonly available Unix tools |
| 209 | +-// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> |
| 210 | +-// SPDX-License-Identifier: 0BSD |
| 211 | +- |
| 212 | +-#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 202405L |
| 213 | +-#error reallocarray needs: define _POSIX_C_SOURCE 202405L |
| 214 | +-#endif |
| 215 | +- |
| 216 | +-// pre-POSIX.1-2024 reallocarray fallback |
| 217 | +-#if !defined(_POSIX_VERSION) || _POSIX_VERSION < 202405L |
| 218 | +-#define _BSD_SOURCE |
| 219 | +-// FreeBSD |
| 220 | +-#undef _POSIX_C_SOURCE |
| 221 | +-#if __NetBSD_Version__ < 1000000000 |
| 222 | +-#define _OPENBSD_SOURCE |
| 223 | +-#endif |
| 224 | +-#endif |
| 225 | +- |
| 226 | +-#include <stdlib.h> // reallocarray |
| 227 | +diff --git utils-std-0.1.1/lib/tr_str.c utils-std-0.1.1/lib/tr_str.c |
| 228 | +index 23dfcab..7cb14b9 100644 |
| 229 | +--- utils-std-0.1.1/lib/tr_str.c |
| 230 | ++++ utils-std-0.1.1/lib/tr_str.c |
| 231 | +@@ -31,15 +31,12 @@ |
| 232 | + * SUCH DAMAGE. |
| 233 | + */ |
| 234 | + |
| 235 | +-// clang-format off |
| 236 | + #define _POSIX_C_SOURCE 202405L |
| 237 | +-// Needs to be the first included header due to this horrible BSD_VISIBLE macro |
| 238 | +-#include "./reallocarray.h" |
| 239 | + |
| 240 | + #include "./tr_str.h" |
| 241 | +-// clang-format on |
| 242 | + |
| 243 | + #include "../libutils/err.h" |
| 244 | ++#include "../libutils/reallocarray.h" |
| 245 | + |
| 246 | + #include <assert.h> |
| 247 | + #include <ctype.h> |
| 248 | +@@ -190,7 +187,7 @@ genclass(STR *s) |
| 249 | + { |
| 250 | + len = NCHARS + 1; |
| 251 | + assert(len != 0); |
| 252 | +- cp->set = reallocarray(NULL, len, sizeof(*cp->set)); |
| 253 | ++ cp->set = utils_reallocarray(NULL, len, sizeof(*cp->set)); |
| 254 | + if(cp->set == NULL) utils_err(1, NULL); |
| 255 | + |
| 256 | + len = 0; |
| 257 | +@@ -201,7 +198,7 @@ genclass(STR *s) |
| 258 | + cp->set[len++] = OOBCH; |
| 259 | + |
| 260 | + assert(len != 0); |
| 261 | +- cp->set = reallocarray(cp->set, len, sizeof(*cp->set)); |
| 262 | ++ cp->set = utils_reallocarray(cp->set, len, sizeof(*cp->set)); |
| 263 | + if(cp->set == NULL) utils_err(1, NULL); |
| 264 | + } |
| 265 | + |
| 266 | +diff --git utils-std-0.1.1/libutils/reallocarray.c utils-std-0.1.1/libutils/reallocarray.c |
| 267 | +new file mode 100644 |
| 268 | +index 0000000..14b4fd8 |
| 269 | +--- /dev/null |
| 270 | ++++ utils-std-0.1.1/libutils/reallocarray.c |
| 271 | +@@ -0,0 +1,30 @@ |
| 272 | ++// Copied from musl |
| 273 | ++// Copyright © 2020 Ariadne Conill <ariadne@dereferenced.org> |
| 274 | ++// SPDX-License-Identifier: MIT |
| 275 | ++ |
| 276 | ++#define _POSIX_C_SOURCE 202405L |
| 277 | ++#define _BSD_SOURCE |
| 278 | ++#include "./reallocarray.h" |
| 279 | ++ |
| 280 | ++#include <errno.h> |
| 281 | ++#include <stdlib.h> |
| 282 | ++ |
| 283 | ++#ifdef HAS_REALLOCARRAY |
| 284 | ++void * |
| 285 | ++utils_reallocarray(void *ptr, size_t m, size_t n) |
| 286 | ++{ |
| 287 | ++ return reallocarray(ptr, m, n); |
| 288 | ++} |
| 289 | ++#else /* HAS_REALLOCARRAY */ |
| 290 | ++void * |
| 291 | ++utils_reallocarray(void *ptr, size_t m, size_t n) |
| 292 | ++{ |
| 293 | ++ if(n && m > -1 / n) |
| 294 | ++ { |
| 295 | ++ errno = ENOMEM; |
| 296 | ++ return 0; |
| 297 | ++ } |
| 298 | ++ |
| 299 | ++ return realloc(ptr, m * n); |
| 300 | ++} |
| 301 | ++#endif |
| 302 | +diff --git utils-std-0.1.1/libutils/reallocarray.h utils-std-0.1.1/libutils/reallocarray.h |
| 303 | +new file mode 100644 |
| 304 | +index 0000000..dcafb1e |
| 305 | +--- /dev/null |
| 306 | ++++ utils-std-0.1.1/libutils/reallocarray.h |
| 307 | +@@ -0,0 +1,13 @@ |
| 308 | ++// utils-std: Collection of commonly available Unix tools |
| 309 | ++// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> |
| 310 | ++// SPDX-License-Identifier: 0BSD |
| 311 | ++ |
| 312 | ++#ifndef _BSD_SOURCE |
| 313 | ++# if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 202405L |
| 314 | ++# error reallocarray needs: define _POSIX_C_SOURCE 202405L or define _BSD_SOURCE |
| 315 | ++# endif |
| 316 | ++#endif |
| 317 | ++ |
| 318 | ++#include <stdlib.h> // reallocarray |
| 319 | ++ |
| 320 | ++void *utils_reallocarray(void *ptr, size_t m, size_t n); |
0 commit comments