Alignment fixups for gethostbyname_r

Mike Frysinger vapier at gentoo.org
Mon Jan 7 16:39:39 UTC 2008


On Monday 07 January 2008, Daniel Jacobowitz wrote:
> On Sun, Jan 06, 2008 at 01:35:53AM -0500, Mike Frysinger wrote:
> > On Wednesday 19 December 2007, Daniel Jacobowitz wrote:
> > > --- libc/inet/resolv.c  (revision 189757)
> > > +++ libc/inet/resolv.c  (local)
> > > @@ -1534,6 +1534,15 @@ int attribute_hidden __read_etc_hosts_r(
> > >         char *cp, **alias;
> > >         int aliases, i, ret = HOST_NOT_FOUND;
> > >  
> > > +       /* Align to at least the size of a char * so we can put
> > > +          pointers in it.  */
> > > +       i = (unsigned long) buf % sizeof(char *);
> > > +       i = (sizeof(char *) - i) % sizeof(char *);
> > > +       if (buflen < i)
> > > +               return ERANGE;
> > > +       buf+=i;
> > > +       buflen-=i;
> >
> > considering buflen gets checked constantly before being utilized, i dont
> > think we need an additional check to see if it could possibly store a
> > pointer.  we just need to make sure the adjustment alone doesnt cause
> > size_t underflow.
>
> That's exactly what I just did, except for the spelling of the
> alignment; sizeof(size_t) is sufficient on all Linux platforms for
> everything that gethostbyname_r puts in the buffer.

i misunderstood the purpose of the second assignment.  i'll put together a 
test case and commit a fix ...

> > i think this should be sufficient:
> > /* Make sure the incoming char * buffer is aligned enough to
> >  * handle our random structures.  This define is the same as we
> >  * use for malloc alignment (which has same requirements). */
> > i = (size_t)buf & __alignof__(double __attribute_aligned__
> > (sizeof(size_t))); if (buflen < i)
> > 	return ERANGE;
> > buf += i;
> > buflen += i;
>
> - Why sizeof(size_t)?  What does that have to do with anything, if
> you're already using __alignof__?

this is taken from the existing malloc code which takes into account different 
arch quirks for alignment

> - "(size_t) buf & 8" is not what you're trying to calculate, you need
> a -1.

ah right, i thought you were after something else with the second line

> - buflen -= i, not plus

i pointed this typo out already ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: This is a digitally signed message part.
Url : http://lists.busybox.net/pipermail/uclibc/attachments/20080107/a001e445/attachment-0002.pgp 


More information about the uClibc mailing list