This is the mail archive of the
elfutils-devel@sourceware.org
mailing list for the elfutils project.
Re: [PATCH] gelf_getauxv: Use memcpy instead of pointer dereference to avoid alignment problems.
- From: Kurt Roeckx <kurt at roeckx dot be>
- To: elfutils-devel at lists dot fedorahosted dot org
- Date: Sun, 25 Aug 2013 21:40:49 +0200
- Subject: Re: [PATCH] gelf_getauxv: Use memcpy instead of pointer dereference to avoid alignment problems.
On Sun, Aug 25, 2013 at 09:18:42PM +0200, Florian Weimer wrote:
> On 08/25/2013 12:47 PM, Kurt Roeckx wrote:
>
> >- *dst = ((GElf_auxv_t *) data_scn->d.d_buf)[ndx];
> >+ memcpy(dst, &((GElf_auxv_t *) data_scn->d.d_buf)[ndx], sizeof(*dst));
>
> That's not correct, the cast is already undefined if the pointer
> lacks sufficient alignment. GCC might even translate the latter
> into the former.
You're of course right that that case is undefined behavior.
> memcpy(dst, data_scn->d.d_buf + ndx * sizeof(GElf_auxv_t),
> sizeof(GElf_axuv_t));
d_buf is a void *, and I don't think that has defined behavior either. It
should probably be:
memcpy(dst, (char *)data_scn->d.d_buf + ndx * sizeof(GElf_auxv_t), sizeof(GElf_axuv_t));
Kurt