svn commit: trunk/uClibc/libc/inet
vapier at uclibc.org
vapier at uclibc.org
Wed Jan 9 05:49:45 UTC 2008
Author: vapier
Date: 2008-01-08 21:49:44 -0800 (Tue, 08 Jan 2008)
New Revision: 20830
Log:
Daniel Jacobowitz writes:
MontaVista noticed that when their kernels were configured to trap on unaligned
access gethostbyname_r could mysteriously crash. I tracked this down to an
unaligned buffer being passed to gethostbyname_r from some other part of uClibc
(afraid I don't remember where from any more). We have to pad the beginning of
the buffer to a pointer alignment before we store pointers in it.
Modified:
trunk/uClibc/libc/inet/resolv.c
Changeset:
Modified: trunk/uClibc/libc/inet/resolv.c
===================================================================
--- trunk/uClibc/libc/inet/resolv.c 2008-01-08 23:24:30 UTC (rev 20829)
+++ trunk/uClibc/libc/inet/resolv.c 2008-01-09 05:49:44 UTC (rev 20830)
@@ -234,7 +234,15 @@
#define DPRINTF(X,args...)
#endif /* DEBUG */
+/* 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). The offset is the number of bytes we need to adjust
+ * in order to attain desired alignment.
+ */
+#define ALIGN_ATTR __alignof__(double __attribute_aligned__ (sizeof(size_t)))
+#define ALIGN_BUFFER_OFFSET(buf) ((ALIGN_ATTR - ((size_t)buf % ALIGN_ATTR)) % ALIGN_ATTR)
+
/* Global stuff (stuff needing to be locked to be thread safe)... */
extern int __nameservers attribute_hidden;
extern char * __nameserver[MAX_SERVERS] attribute_hidden;
@@ -1534,6 +1542,15 @@
char *cp, **alias;
int aliases, i, ret = HOST_NOT_FOUND;
+ /* make sure user char * is aligned */
+ i = ALIGN_BUFFER_OFFSET(buf);
+ if (unlikely(i)) {
+ if (buflen < i)
+ return ERANGE;
+ buf += i;
+ buflen -= i;
+ }
+
if (buflen < sizeof(char *)*(ALIAS_DIM))
return ERANGE;
alias = (char **)buf;
@@ -2029,6 +2046,15 @@
DPRINTF("Nothing found in /etc/hosts\n");
+ /* make sure user char * is aligned */
+ i = ALIGN_BUFFER_OFFSET(buf);
+ if (unlikely(i)) {
+ if (buflen < i)
+ return ERANGE;
+ buf += i;
+ buflen -= i;
+ }
+
*h_errnop = NETDB_INTERNAL;
if (buflen < sizeof(*in))
return ERANGE;
More information about the uClibc-cvs
mailing list