[Ocfs2-tools-devel] [PATCH 01/13] libocfs2: Catch memalign()s that will abort older glibcs.

Sunil Mushran sunil.mushran at oracle.com
Tue Jun 2 13:40:38 PDT 2009


Joel Becker wrote:
> Older glibcs (before 2007/07, this includes the glibc in el5) would
> abort if __libc_memalign() couldn't allocate the memory.  That's
> obviously a bogus behavior, but we have to handle it.
>
> It's simple, though.  We try with malloc() first.  If that succeeds, we
> know the memory is there and retry with posix_memalign().
>
> Signed-off-by: Joel Becker <joel.becker at oracle.com>
>   
trivial improvement below
Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>

> ---
>  libocfs2/memory.c |   14 ++++++++++++++
>  1 files changed, 14 insertions(+), 0 deletions(-)
>
> diff --git a/libocfs2/memory.c b/libocfs2/memory.c
> index 589e114..a61fe42 100644
> --- a/libocfs2/memory.c
> +++ b/libocfs2/memory.c
> @@ -102,9 +102,23 @@ errcode_t ocfs2_malloc_blocks(io_channel *channel, int num_blocks,
>  {
>  	errcode_t ret;
>  	int blksize;
> +	size_t bytes;
>  	void **pp = (void **)ptr;
> +	void *tmp;
>  
>  	blksize = io_get_blksize(channel);
> +	if (((unsigned long long)num_blocks * blksize) > SIZE_MAX)
> +		return OCFS2_ET_NO_MEMORY;
> +	bytes = num_blocks * blksize;
> +
> +	/*
> +	 * Older glibcs abort when they can't memalign() something.
> +	 * Ugh!  Check with malloc() first.
> +	 */
> +	tmp = malloc(bytes);
> +	if (!tmp)
> +		return OCFS2_ET_NO_MEMORY;
> +	free(tmp);
>  
>  	ret = posix_memalign(pp, blksize, num_blocks * blksize);
>   

ret = posix_memalign(pp, blksize, bytes);

>  	if (!ret)
>   




More information about the Ocfs2-tools-devel mailing list