Alignment fixups for gethostbyname_r

Daniel Jacobowitz drow at false.org
Mon Jan 7 13:22:29 UTC 2008


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 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__?
- "(size_t) buf & 8" is not what you're trying to calculate, you need
a -1.
- buflen -= i, not plus

-- 
Daniel Jacobowitz
CodeSourcery



More information about the uClibc mailing list