aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Floyd <pjfloyd@wanadoo.fr>2023-02-24 16:29:01 +0000
committerEd Maste <emaste@FreeBSD.org>2023-03-03 00:23:24 +0000
commit1b1d2aea020e88b3d2076207b13fec3adde42aa9 (patch)
treeac35c7d64dd1fb78a1fc3a29075e471542436afe
parent24fe8a518324e291b9ea3fe4e60f2e0fb4f857c5 (diff)
downloadsrc-1b1d2aea020e88b3d2076207b13fec3adde42aa9.tar.gz
src-1b1d2aea020e88b3d2076207b13fec3adde42aa9.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 (cherry picked from commit 2c709ee70ade9fd8f77b37917a4169d667dda41d)
-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 ff1c967482cd..88feeaef7afd 100644
--- a/lib/libc/gen/memalign.c
+++ b/lib/libc/gen/memalign.c
@@ -36,5 +36,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));
}