aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Floyd <pjfloyd@wanadoo.fr>2023-02-24 16:29:01 +0000
committerEd Maste <emaste@FreeBSD.org>2023-02-24 18:19:06 +0000
commit2c709ee70ade9fd8f77b37917a4169d667dda41d (patch)
tree5900682625321ec0084f418b058d11f34bce4b15
parent07ccf71451d7377b1a6f3367f738ce7ddb1f2a24 (diff)
downloadsrc-2c709ee70ade9fd8f77b37917a4.tar.gz
src-2c709ee70ade9fd8f77b37917a4.zip
libc: handle zero alignment in memalign()
For compatibility with glibc. The previous code would trigger a division by zero in roundup() and terminate. Instead, just pass through to malloc() for align == 0. PR: 269688 Reviewed by: imp, mjg MFC after: 1 week Pull Request: https://github.com/freebsd/freebsd-src/pull/655
-rw-r--r--lib/libc/gen/memalign.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/libc/gen/memalign.c b/lib/libc/gen/memalign.c
index 4f5ea2fd61fd..dc0ad34a117b 100644
--- a/lib/libc/gen/memalign.c
+++ b/lib/libc/gen/memalign.c
@@ -35,5 +35,12 @@ __FBSDID("$FreeBSD$");
void *
memalign(size_t align, size_t size)
{
- return (aligned_alloc(align, roundup(size, align)));
+ /*
+ * glibc allows align == 0, but that is not valid for roundup.
+ * Just pass through to malloc in that case.
+ */
+ if (align != 0)
+ return (aligned_alloc(align, roundup(size, align)));
+ else
+ return (malloc(size));
}