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

David Gibson david at gibson.dropbear.id.au
Fri Aug 21 04:10:40 AEST 2015


On Thu, Aug 20, 2015 at 11:17:32AM +0930, Paul 'Rusty' Russell wrote:
> 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);
> }

Ah, good catch.


-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20150820/80bcd3d9/attachment.sig>


More information about the ccan mailing list