fix sendmmsg emulation return value for zero-length vector
[musl] / src / network / dn_expand.c
1 #include <resolv.h>
2 #include "libc.h"
3
4 int __dn_expand(const unsigned char *base, const unsigned char *end, const unsigned char *src, char *dest, int space)
5 {
6         const unsigned char *p = src;
7         char *dend = dest + (space > 254 ? 254 : space);
8         int len = -1, i, j;
9         if (p==end || !*p) return -1;
10         /* detect reference loop using an iteration counter */
11         for (i=0; i < end-base; i+=2) {
12                 if (*p & 0xc0) {
13                         if (p+1==end) return -1;
14                         j = ((p[0] & 0x3f) << 8) | p[1];
15                         if (len < 0) len = p+2-src;
16                         if (j >= end-base) return -1;
17                         p = base+j;
18                 } else if (*p) {
19                         j = *p+1;
20                         if (j>=end-p || j>dend-dest) return -1;
21                         while (--j) *dest++ = *++p;
22                         *dest++ = *++p ? '.' : 0;
23                 } else {
24                         if (len < 0) len = p+1-src;
25                         return len;
26                 }
27         }
28         return -1;
29 }
30
31 weak_alias(__dn_expand, dn_expand);