Re: [PATCH 1/2] xalloc: general purpose memory allocation handling wrappers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2010-10-15 at 11:59 -0300, Davidlohr Bueso wrote:
> xalloc: general purpose memory allocation handling wrappers
> 
> Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>

malloc and calloc may return NULL (implementation dependent) when passed
a zero size, which isn't a failure.

It is legitimate and sometimes useful to call realloc with a 0 size to
effectively free() a block.  In this case the returned value may be NULL
(implementation dependent).

So I suggest the following:

[snip]
> +static inline void *xmalloc(const size_t size)
> +{
> +        void *ret = malloc(size);
> +
> +        if (!ret)

           if ( (!ret && size)

> +                err(EXIT_FAILURE, "cannot allocate %lu bytes", size);
> +        return ret;
> +}
> +
> +static inline void *xrealloc(void *ptr, const size_t size)
> +{
> +        void *ret = realloc(ptr, size);
> +
> +        if (!ret)

           if (!ret && size)

> +                err(EXIT_FAILURE, "cannot allocate %lu bytes", size);
> +        return ret;
> +}
> +
> +static inline void *xcalloc(const size_t nelems, const size_t size)
> +{
> +        void *ret = calloc(nelems, size);
> +
> +        if (!ret)

           if ( (!ret && size && nelems)

> +                err(EXIT_FAILURE, "cannot allocate %lu bytes", size);
> +        return ret;
> +}
> +
> +#endif

-- Lawrence Rust


--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux