Skip to content

Commit

Permalink
musl: backport reallocarray.
Browse files Browse the repository at this point in the history
It's in POSIX-future and some applications have started depending on it.
It's easier to backport into musl than fix each individual package.

Since we are adding a new interface to libc, update common/shlibs entry
as well. This should probably have been done for all musl updates.
  • Loading branch information
ericonr committed May 24, 2021
1 parent e5c370f commit aa6a4aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion common/shlibs
Expand Up @@ -16,7 +16,7 @@
# PLEASE NOTE: when multiple packages provide the same SONAME, the first
# one (order top->bottom) is preferred over the next ones.
#
libc.so musl-1.1.21_1
libc.so musl-1.1.24_7
libc.so.6 glibc-2.32_1
libm.so.6 glibc-2.32_1
libpthread.so.0 glibc-2.32_1
Expand Down
42 changes: 42 additions & 0 deletions srcpkgs/musl/patches/reallocarray.patch
@@ -0,0 +1,42 @@
From 821083ac7b54eaa040d5a8ddc67c6206a175e0ca Mon Sep 17 00:00:00 2001
From: Ariadne Conill <ariadne@dereferenced.org>
Date: Sat, 1 Aug 2020 08:26:35 -0600
Subject: [PATCH] implement reallocarray

reallocarray is an extension introduced by OpenBSD, which introduces
calloc overflow checking to realloc.

glibc 2.28 introduced support for this function behind _GNU_SOURCE,
while glibc 2.29 allows its usage in _DEFAULT_SOURCE.

diff --git a/include/stdlib.h b/include/stdlib.h
index 194c2033..b54a051f 100644
--- include/stdlib.h
+++ include/stdlib.h
@@ -145,6 +145,7 @@ int getloadavg(double *, int);
int clearenv(void);
#define WCOREDUMP(s) ((s) & 0x80)
#define WIFCONTINUED(s) ((s) == 0xffff)
+void *reallocarray (void *, size_t, size_t);
#endif

#ifdef _GNU_SOURCE
diff --git a/src/malloc/reallocarray.c b/src/malloc/reallocarray.c
new file mode 100644
index 00000000..4a6ebe46
--- /dev/null
+++ src/malloc/reallocarray.c
@@ -0,0 +1,13 @@
+#define _BSD_SOURCE
+#include <errno.h>
+#include <stdlib.h>
+
+void *reallocarray(void *ptr, size_t m, size_t n)
+{
+ if (n && m > -1 / n) {
+ errno = ENOMEM;
+ return 0;
+ }
+
+ return realloc(ptr, m * n);
+}
2 changes: 1 addition & 1 deletion srcpkgs/musl/template
Expand Up @@ -2,7 +2,7 @@
pkgname=musl
reverts="1.2.0_1"
version=1.1.24
revision=6
revision=7
archs="*-musl"
bootstrap=yes
build_style=gnu-configure
Expand Down

0 comments on commit aa6a4aa

Please sign in to comment.