Port-sparc archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Problems with packages on a netbsd-6 sparc



On Thu, May 17, 2012 at 08:48:00PM -0700, Greg A. Woods wrote:
> >       * Copy in passwd contents and make strings relative to space
> >       * at the end of the buffer.
> >       */
> > +    memcpy(cp, pw, sizeof(struct passwd));
> >      newpw = (struct passwd *) cp;
> > -    memcpy(newpw, pw, sizeof(struct passwd));
> >      cp += sizeof(struct passwd);
> >      FIELD_COPY(pw, newpw, pw_name, nsize);
> >      FIELD_COPY(pw, newpw, pw_passwd, psize);
> 
> Why not avoid the non-intuitive memcpy() all together all the time and
> just let the compiler do its job????
> 
>       *newpw = *pw;
> 
> C has allowed structure copies like this since at least C89, and much
> earlier in many good compilers (the data pointed to by the fields which
> are pointers still needs to be copied of course).

You need to read the entire thread!
C only allows you to take pointers to objects, cast them to certain other
types (including char * and void *), cast them back to the SAME type
and then dereference them.
This means that the compiler is allowed to assume that 'pw' is a pointer
to an item of the relevent type, and thus has the alignment constraints
of 'struct passwd', and so can be read (in this case) with 64bit accesses.
Since the compiler knows about memcpy() that also applies when it decides
on the code to use for the memcpy() call.
Since the alignment is tracked as a property of the value, no amount
of casting will remove it.

        David

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index