X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fnetwork%2Fdn_expand.c;h=eac343af5ed4f8142985e214c4c531397678d0b1;hb=6aeb9c6703670649ee09b3c8575fb428168bb75c;hp=72a80e82a6f98273c33573221315f62b1daa5903;hpb=fcc522c92335783293ac19df318415cd97fbf66b;p=musl diff --git a/src/network/dn_expand.c b/src/network/dn_expand.c index 72a80e82..eac343af 100644 --- a/src/network/dn_expand.c +++ b/src/network/dn_expand.c @@ -1,27 +1,33 @@ #include -#include "libc.h" int __dn_expand(const unsigned char *base, const unsigned char *end, const unsigned char *src, char *dest, int space) { const unsigned char *p = src; - int len = -1, j; - if (space > 256) space = 256; - if (p==end) return -1; - for (;;) { + char *dend, *dbegin = dest; + int len = -1, i, j; + if (p==end || space <= 0) return -1; + dend = dest + (space > 254 ? 254 : space); + /* detect reference loop using an iteration counter */ + for (i=0; i < end-base; i+=2) { + /* loop invariants: p= end-base) return -1; p = base+j; } else if (*p) { - j = *p+1; + if (dest != dbegin) *dest++ = '.'; + j = *p++; + if (j >= end-p || j >= dend-dest) return -1; + while (j--) *dest++ = *p++; + } else { + *dest = 0; if (len < 0) len = p+1-src; - if (j>=end-p || j>space) return -1; - while (--j) *dest++ = *p++; - *dest++ = *++p ? '.' : 0; - } else return len; + return len; + } } + return -1; } weak_alias(__dn_expand, dn_expand);