public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Christian Biesinger <cbiesinger@google.com>
To: Christian Biesinger via Gdb-patches <gdb-patches@sourceware.org>,
	Christian Biesinger <cbiesinger@google.com>
Subject: Re: [PATCH] sim: mn10300: clean up pointer casts
Date: Mon, 1 Nov 2021 15:35:23 -0400	[thread overview]
Message-ID: <CAPTJ0XE5qVE=ZdhEgE-o+LmMNh+FVj1jvTvOntXDyVqK-tnEMg@mail.gmail.com> (raw)
In-Reply-To: <YYA5LWrH4S/7tfSX@vapier>

On Mon, Nov 1, 2021 at 2:59 PM Mike Frysinger <vapier@gentoo.org> wrote:
>
> On 01 Nov 2021 14:28, Christian Biesinger via Gdb-patches wrote:
> > On Mon, Nov 1, 2021 at 12:22 PM Mike Frysinger <vapier@gentoo.org> wrote:
> > > On 01 Nov 2021 12:07, Christian Biesinger wrote:
> > > > On Sun, Oct 31, 2021 at 11:45 PM Mike Frysinger via Gdb-patches
> > > > <gdb-patches@sourceware.org> wrote:
> > > > > --- a/sim/mn10300/dv-mn103ser.c
> > > > > +++ b/sim/mn10300/dv-mn103ser.c
> > > > > @@ -238,7 +238,7 @@ do_polling_event (struct hw *me,
> > > > >  {
> > > > >    SIM_DESC sd = hw_system (me);
> > > > >    struct mn103ser *serial = hw_data(me);
> > > > > -  long serial_reg = (long) data;
> > > > > +  long serial_reg = (uintptr_t) data;
> > > >
> > > > Shouldn't you change the variable type too?
> > >
> > > the types are already confused, but long here is fine as any other.  uintptr_t
> > > is not really any more correct.  serial_reg comes in as an unsigned_word via
> > > the memory read/write callbacks, (encoded) cast to void*, and (decoded) cast
> > > to long.  the memory read/write callbacks verify the values are always within
> > > the serial_register_types enum range which looks like [0,16] range, so any of
> > > these types work.
> > >
> > > > Otherwise this patch seems to be somewhat pointless?
> > > >     if ( nr_bytes == 1 )
> > >
> > > i don't know which one you mean, or why you think any of them are pointless.
> > > the nr_bytes checks are in the read/write code paths of the memory mapped
> > > register callbacks.  so nr_bytes is needed & correct there.
> >
> > I forgot to delete that nr_bytes line; the pointless comment was about
> > casting to uintprtr_t but still storing the result in a long. Why not
> > keep it as a cast to long then?
>
> that was the point of the patch in the first place :).  i prob should have
> included the compiler errors in the log, but basically any target where the
> sizeof(long) != sizeof(void*) will generate an error.  LLP64 systems is where
> you'll find this, and most notably, 64-bit Windows does it.
> warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
>
> since sizeof(uintptr_t) is guaranteed to equal sizeof(void*), it's the "right"
> way to convert integers to/from pointers.  in this case, the event framework
> is using void* as an opaque storage, and this particular model is (ab)using
> that to pass values rather than pointers to larger storage.

I see, so what the patch does is to cast to uintptr_t to avoid the
compile error about losing precision and rely on an implicit
conversion to long?

My suggestion would be to make that a bit clearer in the commit
message and perhaps explicitly convert from uintptr_t to long.

Christian

  reply	other threads:[~2021-11-01 19:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-01  3:45 Mike Frysinger
2021-11-01 16:07 ` Christian Biesinger
2021-11-01 16:22   ` Mike Frysinger
2021-11-01 18:28     ` Christian Biesinger
2021-11-01 18:59       ` Mike Frysinger
2021-11-01 19:35         ` Christian Biesinger [this message]
2021-11-02  4:19           ` Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPTJ0XE5qVE=ZdhEgE-o+LmMNh+FVj1jvTvOntXDyVqK-tnEMg@mail.gmail.com' \
    --to=cbiesinger@google.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).