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

Re: scrypt's malloc() alignment



On 11/15/12 16:28, Solar Designer wrote:
> A further change I may contribute is removing the dependency on having
> an integer type matching the pointer size (uintptr_t) by implementing
> the alignment with pointer math instead.
> 
> Currently, the scrypt code uses:
> 
> 	if ((B0 = malloc(128 * r * p + 63)) == NULL)
> 		goto err0;
> 	B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
> 
> and so on for other aligned allocations.
> 
> We can do:
> 
> 	void * B0;
> 	uint8_t * B;
> 
> 	if ((B = B0 = malloc(128 * r * p + 63)) == NULL)
> 		goto err0;
> 	B += 63;
> 	B -= (size_t)B & 63;
> 
> Any other integer type will do in place of size_t, although the compiler
> may print a warning if there's a size mismatch (but the code will work
> right anyway).

I was using uintptr_t because I figured it's safer than casting to a size_t
which might or might not be the same size, and I'm not aware of any system
where uintptr_t doesn't exist but other POSIX functionality does.

So I don't think there's anything much to be gained from changing this, and
possibly something to be lost.

-- 
Colin Percival
Security Officer Emeritus, FreeBSD | The power to serve
Founder, Tarsnap | www.tarsnap.com | Online backups for the truly paranoid