[ccan] [PATCH 1/3] mem: add mem helper functions

Rusty Russell rusty at rustcorp.com.au
Thu Aug 20 11:47:32 AEST 2015


Cody P Schafer <dev at codyps.com> writes:
> +/**
> + * memeq - Are two byte arrays equal?
> + * @a: first array
> + * @al: bytes in first array
> + * @b: second array
> + * @bl: bytes in second array
> + *
> + * Example:
> + *	if (memeq(somebytes, bytes_len, otherbytes, otherbytes_len)) {
> + *		printf("memory blocks are the same!\n");
> + *	}
> + */
> +#define memeq(a, al, b, bl) (al == bl && !memcmp(a, b, bl))

You can't do this.  It double-evaluates bl.

It really needs to be an inline function:

static inline bool memeq(const void *a, size_t alen, const void *b, size_t blen)
{
        return alen == blen && !memcmp(a, b, blen);
}

Cheers,
Rusty.


More information about the ccan mailing list