>From 49b96b160fdde9218bcaffa196ca9e6d5a233094 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Thu, 4 Sep 2014 18:29:16 +0200 Subject: [PATCH] fix empty name handling in dn_expand Empty name was rejected in dn_expand since commit 56b57f37a46dab432247bf29d96fcb11fbd02a6d which is a regression as reported by Natanael Copa. But it turns out only an "uncompressed" empty name was rejected, if an offset pointer is used to represent an empty name then dn_expand failed to null terminate the returned name. this fix makes dn_expand accept and return empty names correctly. --- src/network/dn_expand.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/network/dn_expand.c b/src/network/dn_expand.c index 849df19..4fa6af8 100644 --- a/src/network/dn_expand.c +++ b/src/network/dn_expand.c @@ -6,7 +6,7 @@ int __dn_expand(const unsigned char *base, const unsigned char *end, const unsig const unsigned char *p = src; char *dend = dest + (space > 254 ? 254 : space); int len = -1, i, j; - if (p==end || !*p) return -1; + if (p==end) return -1; /* detect reference loop using an iteration counter */ for (i=0; i < end-base; i+=2) { if (*p & 0xc0) { @@ -16,11 +16,13 @@ int __dn_expand(const unsigned char *base, const unsigned char *end, const unsig if (j >= end-base) return -1; p = base+j; } else if (*p) { - j = *p+1; - if (j>=end-p || j>dend-dest) return -1; - while (--j) *dest++ = *++p; - *dest++ = *++p ? '.' : 0; + j = *p++; + if (j >= end-p || j >= dend-dest) return -1; + while (j--) *dest++ = *p++; + if (*p) *dest++ = '.'; } else { + if (dest == dend) return -1; + *dest = 0; if (len < 0) len = p+1-src; return len; } -- 1.7.10.4