[cairo] [PATCH 1/4] mempool: use wide enough type for pointer arithmetic

Uli Schlachter psychon at znc.in
Thu Feb 11 19:56:27 UTC 2016


Am 10.02.2016 um 21:49 schrieb Simon Richter:
> The "unsigned long" type on Windows is just 32 bits wide, so converting
> from and to a pointer is unsafe.
> 
> Replace this with intptr_t, which is guaranteed to be wide enough. It would
> be better to use uintptr_t here, but this is not available in several MSVC
> versions.
> ---
>  src/cairo-mempool.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/src/cairo-mempool.c b/src/cairo-mempool.c
> index 751ede3..d177bcc 100644
> --- a/src/cairo-mempool.c
> +++ b/src/cairo-mempool.c
> @@ -284,19 +284,19 @@ _cairo_mempool_init (cairo_mempool_t *pool,
>  		      void *base, size_t bytes,
>  		      int min_bits, int num_sizes)
>  {
> -    unsigned long tmp;
> +    intptr_t tmp;
>      int num_blocks;
>      int i;
>  
>      /* Align the start to an integral chunk */
> -    tmp = ((unsigned long) base) & ((1 << min_bits) - 1);
> +    tmp = ((intptr_t) base) & ((1 << min_bits) - 1);
>      if (tmp) {
>  	tmp = (1 << min_bits) - tmp;
>  	base = (char *)base + tmp;
>  	bytes -= tmp;
>      }

Correct me if I'm wrong, but this code doesn't care about the high bits of the
pointer. As long as sizeof(unsigned long) > min_bits (="always"), the old code
already did the right thing (= get the low min_bits bits of the pointer and use
that to align base as needed) and this patch just needs to silence the warning.

Assuming no one proves me wrong on the above, let's not bikeshed this too much:

Reviewed-by: Uli Schlachter <psychon at znc.in>

>  
> -    assert ((((unsigned long) base) & ((1 << min_bits) - 1)) == 0);
> +    assert ((((intptr_t) base) & ((1 << min_bits) - 1)) == 0);
>      assert (num_sizes < ARRAY_LENGTH (pool->free));
>  
>      pool->base = base;
> 


-- 
99 little bugs in the code
99 little bugs in the code
Take one down, patch it around
117 little bugs in the code
  -- @irqed


More information about the cairo mailing list