potentially incorrect conversion of pointer to unsigned long

Vincent Lefevre vincent at vinc17.org
Sun Jun 23 00:28:25 UTC 2019


On 2019-06-23 09:01:43 +1000, Cameron Simpson wrote:
> On 22Jun2019 21:14, vincent lefevre <vincent at vinc17.org> wrote:
> > On 2019-06-22 12:40:39 +0200, Oswald Buddenhagen wrote:
> > > On Sat, Jun 22, 2019 at 09:42:36AM +0200, Vincent Lefevre wrote:
> > > >         p->init = (unsigned long) safe_strdup (* ((char **) p->data));
> > > >
> > > > IMHO, the best solution for safety would be to use a union (I think
> > > > that this would require the use of C99 designators for the MuttVars
> > > > initialization). Otherwise check (uintptr_t) -1 <= (unsigned long) -1
> > > > in configure.
> > > >
> > > it's easier to just use !! or put != NULL at the end instead of the cast.
> > 
> > AFAIK, the value itself matters, not just the fact that it is
> > non-zero.
> 
> If I were doing this in Python or any other object oriented language I'd
> have a special sentinel value of the right type i.e. _not_ some magic -1
> value we expect to not match any other pointer-as-integer value and also not
> NULL, but an actual distinct special purpose (char*).

I don't understand what you mean. -1 is *not* some magic value.
When -1 is converted to the unsigned integer type, it yields the
maximum value of the type. Thus (uintptr_t) -1 <= (unsigned long) -1
is true iff unsigned long is at least as large as uintptr_t, i.e.
when converting a uintptr_t to unsigned long, there is no loss of
information. This way of checking the size of the unsigned integer
types in term of values is completely portable (as opposed to sizeof,
which includes the possible padding bits / bytes).

> So how about this:
> 
>    #define UNSET_CHAR_PTR "<unset>"
> 
> and to use UNSET_CHAR_PTR instead of (uintptr_t)-1 and (unsigned long)-1.

No, this is not the intended use of (uintptr_t) -1 and (unsigned long) -1
above.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the Mutt-dev mailing list