Skip to content

Commit 44c009b

Browse files
committed
ares_expand_name(): fix formatting and handling of root name response
Fixes issue introduced in prior commit with formatting and handling of parsing a root name response which should not be escaped. Fix By: Brad House
1 parent 362f91d commit 44c009b

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/lib/ares_expand_name.c

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,37 @@ int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
127127
}
128128
else
129129
{
130-
len = *p;
130+
int name_len = *p;
131+
len = name_len;
131132
p++;
133+
132134
while (len--)
133135
{
134-
if (!isprint(*p)) {
135-
/* Output as \DDD for consistency with RFC1035 5.1 */
136-
*q++ = '\\';
137-
*q++ = '0' + *p / 100;
138-
*q++ = '0' + (*p % 100) / 10;
139-
*q++ = '0' + (*p % 10);
140-
} else if (is_reservedch(*p)) {
141-
*q++ = '\\';
142-
*q++ = *p;
143-
} else {
144-
*q++ = *p;
145-
}
136+
/* Output as \DDD for consistency with RFC1035 5.1, except
137+
* for the special case of a root name response */
138+
if (!isprint(*p) && !(name_len == 1 && *p == 0))
139+
{
140+
141+
*q++ = '\\';
142+
*q++ = '0' + *p / 100;
143+
*q++ = '0' + (*p % 100) / 10;
144+
*q++ = '0' + (*p % 10);
145+
}
146+
else if (is_reservedch(*p))
147+
{
148+
*q++ = '\\';
149+
*q++ = *p;
150+
}
151+
else
152+
{
153+
*q++ = *p;
154+
}
146155
p++;
147156
}
148157
*q++ = '.';
149158
}
150-
}
159+
}
160+
151161
if (!indir)
152162
*enclen = aresx_uztosl(p + 1U - encoded);
153163

@@ -194,21 +204,29 @@ static int name_length(const unsigned char *encoded, const unsigned char *abuf,
194204
}
195205
else if (top == 0x00)
196206
{
197-
offset = *encoded;
207+
int name_len = *encoded;
208+
offset = name_len;
198209
if (encoded + offset + 1 >= abuf + alen)
199210
return -1;
200211
encoded++;
212+
201213
while (offset--)
202214
{
203-
if (!isprint(*encoded)) {
204-
n += 4;
205-
} else if (is_reservedch(*encoded)) {
206-
n += 2;
207-
} else {
208-
n += 1;
209-
}
215+
if (!isprint(*encoded) && !(name_len == 1 && *encoded == 0))
216+
{
217+
n += 4;
218+
}
219+
else if (is_reservedch(*encoded))
220+
{
221+
n += 2;
222+
}
223+
else
224+
{
225+
n += 1;
226+
}
210227
encoded++;
211228
}
229+
212230
n++;
213231
}
214232
else

0 commit comments

Comments
 (0)