Print this page
10880 posix_memalign() should avoid clobbering return parameter on failure

*** 22,34 **** /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ - #pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <stdlib.h> #include <errno.h> /* * SUSv3 - aligned memory allocation --- 22,35 ---- /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include "lint.h" + + #include <sys/sysmacros.h> + #include <stdlib.h> #include <errno.h> /* * SUSv3 - aligned memory allocation
*** 47,64 **** */ 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); } --- 48,68 ---- */ int posix_memalign(void **memptr, size_t alignment, size_t size) { void *ptr = NULL; + 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); ! } ! } }