git: 2c709ee70ade - main - libc: handle zero alignment in memalign()
Date: Fri, 24 Feb 2023 18:21:04 UTC
The branch main has been updated by emaste:
URL: https://cgit.FreeBSD.org/src/commit/?id=2c709ee70ade9fd8f77b37917a4169d667dda41d
commit 2c709ee70ade9fd8f77b37917a4169d667dda41d
Author: Paul Floyd <pjfloyd@wanadoo.fr>
AuthorDate: 2023-02-24 16:29:01 +0000
Commit: Ed Maste <emaste@FreeBSD.org>
CommitDate: 2023-02-24 18:19:06 +0000
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
---
lib/libc/gen/memalign.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
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));
}