• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff -Npur c-ares-1.15.0/ares_expand_name.c c-ares-1.15.0-new/ares_expand_name.c
2--- c-ares-1.15.0/ares_expand_name.c	2017-07-03 17:04:19.000000000 +0800
3+++ c-ares-1.15.0-new/ares_expand_name.c	2021-08-21 22:48:24.650973166 +0800
4@@ -38,6 +38,26 @@
5 static int name_length(const unsigned char *encoded, const unsigned char *abuf,
6                        int alen);
7
8+/* Reserved characters for names that need to be escaped */
9+static int is_reservedch(int ch)
10+{
11+  switch (ch) {
12+    case '"':
13+    case '.':
14+    case ';':
15+    case '\\':
16+    case '(':
17+    case ')':
18+    case '@':
19+    case '$':
20+      return 1;
21+    default:
22+      break;
23+  }
24+
25+  return 0;
26+}
27+
28 /* Expand an RFC1035-encoded domain name given by encoded.  The
29  * containing message is given by abuf and alen.  The result given by
30  * *s, which is set to a NUL-terminated allocated buffer.  *enclen is
31@@ -113,18 +133,37 @@ int ares_expand_name(const unsigned char
32         }
33       else
34         {
35-          len = *p;
36+          int name_len = *p;
37+          len = name_len;
38           p++;
39+
40           while (len--)
41             {
42-              if (*p == '.' || *p == '\\')
43-                *q++ = '\\';
44-              *q++ = *p;
45+              /* Output as \DDD for consistency with RFC1035 5.1, except
46+               * for the special case of a root name response  */
47+              if (!isprint(*p) && !(name_len == 1 && *p == 0))
48+                {
49+
50+                  *q++ = '\\';
51+                  *q++ = '0' + *p / 100;
52+                  *q++ = '0' + (*p % 100) / 10;
53+                  *q++ = '0' + (*p % 10);
54+                }
55+              else if (is_reservedch(*p))
56+                {
57+                  *q++ = '\\';
58+                  *q++ = *p;
59+                }
60+              else
61+                {
62+                  *q++ = *p;
63+                }
64               p++;
65             }
66           *q++ = '.';
67         }
68-    }
69+     }
70+
71   if (!indir)
72     *enclen = aresx_uztosl(p + 1U - encoded);
73
74@@ -171,15 +210,29 @@ static int name_length(const unsigned ch
75         }
76       else if (top == 0x00)
77         {
78-          offset = *encoded;
79+          int name_len = *encoded;
80+          offset = name_len;
81           if (encoded + offset + 1 >= abuf + alen)
82             return -1;
83           encoded++;
84+
85           while (offset--)
86             {
87-              n += (*encoded == '.' || *encoded == '\\') ? 2 : 1;
88+              if (!isprint(*encoded) && !(name_len == 1 && *encoded == 0))
89+                {
90+                  n += 4;
91+                }
92+              else if (is_reservedch(*encoded))
93+                {
94+                  n += 2;
95+                }
96+              else
97+                {
98+                  n += 1;
99+                }
100               encoded++;
101             }
102+
103           n++;
104         }
105       else
106