From a547acf91a502e2d79ff67ef86d1b791883ca43a Mon Sep 17 00:00:00 2001 From: Richard Lowe Date: Sun, 28 Apr 2019 18:59:18 +0000 Subject: 10880 posix_memalign() should avoid clobbering return parameter on failure Reviewed by: Toomas Soome Reviewed by: Gergő Doma Reviewed by: Robert Mustacchi Approved by: Dan McDonald MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/src/lib/libc/port/gen/posix_memalign.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'usr/src/lib/libc') diff --git a/usr/src/lib/libc/port/gen/posix_memalign.c b/usr/src/lib/libc/port/gen/posix_memalign.c index d05ac2d9ea..131e0d01b8 100644 --- a/usr/src/lib/libc/port/gen/posix_memalign.c +++ b/usr/src/lib/libc/port/gen/posix_memalign.c @@ -24,9 +24,10 @@ * Use is subject to license terms. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" + +#include + #include #include @@ -49,16 +50,19 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) { void *ptr = NULL; - int error = 0; - - if (alignment == 0 || - (alignment & (sizeof (void *) - 1)) != 0 || - (alignment & (alignment - 1)) != 0) - error = EINVAL; - else if (size != 0 && - (ptr = memalign(alignment, size)) == NULL) - error = ENOMEM; - *memptr = ptr; - return (error); + if ((alignment == 0) || !ISP2(alignment) || + (alignment & (sizeof (void *) - 1)) != 0) { + return (EINVAL); + } else if (size == 0) { + *memptr = NULL; + return (0); + } else { + if ((ptr = memalign(alignment, size)) == NULL) { + return (ENOMEM); + } else { + *memptr = ptr; + return (0); + } + } } -- cgit v1.2.3