2012-08-02 18:00:55

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] regulator: core: Add intermediate cast to uintptr_t before casting to pointer

On Wed, Aug 01, 2012 at 12:08:08AM +0800, Axel Lin wrote:
> 於 二,2012-07-31 於 16:41 +0100,Mark Brown 提到:

> > So the above cast probably isn't right... you shouldn't need a double
> > cast here.

> The issue of original warning is: ( I compile the kernel on x86_64 )
> warning: cast to pointer from integer of different size

> What this patch does is:
> cast best_val to uintptr_t, which is an unsigned integer large enough to
> hold a void* pointer.

> And then cast it to (void *).

> Thus I did (void *)(uintptr_t)best_val.

Perhaps we need a temporary variable here. The double cast just looks
too horrible.


2012-08-05 16:10:55

by Axel Lin

[permalink] [raw]
Subject: Re: [PATCH] regulator: core: Add intermediate cast to uintptr_t before casting to pointer

2012/8/3 Mark Brown <[email protected]>:
> On Wed, Aug 01, 2012 at 12:08:08AM +0800, Axel Lin wrote:
>> ?? ?G?A2012-07-31 ?? 16:41 +0100?AMark Brown ?????G
>
>> > So the above cast probably isn't right... you shouldn't need a double
>> > cast here.
>
>> The issue of original warning is: ( I compile the kernel on x86_64 )
>> warning: cast to pointer from integer of different size
>
>> What this patch does is:
>> cast best_val to uintptr_t, which is an unsigned integer large enough to
>> hold a void* pointer.
>
>> And then cast it to (void *).
>
>> Thus I did (void *)(uintptr_t)best_val.
>
> Perhaps we need a temporary variable here. The double cast just looks
> too horrible.

Either is ok to me.
But the double case looks better to me because the intention is clear.
It seems uintptr_t is less commonly used in drivers code.
Maybe just use (void *)(unsigned long)best_val is better in readability.

2012-08-06 14:31:19

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] regulator: core: Add intermediate cast to uintptr_t before casting to pointer

On Mon, Aug 06, 2012 at 12:10:33AM +0800, Axel Lin wrote:

> Either is ok to me.
> But the double case looks better to me because the intention is clear.
> It seems uintptr_t is less commonly used in drivers code.
> Maybe just use (void *)(unsigned long)best_val is better in readability.

Two casts just looks terrible to me to be honest, it'll set off alarm
bells any time I glance at the code just from the visual pattern.