]> git.meshlink.io Git - catta/blobdiff - dns.c
fix two memory leaks
[catta] / dns.c
diff --git a/dns.c b/dns.c
index f55ae78df2f8dda64c8b06b5da077d8f2f223a55..a402e298815a524f8c8b3a2963e2ba2ed9ece757 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -63,8 +63,8 @@ guint16 flx_dns_packet_get_field(flxDnsPacket *p, guint index) {
     return g_ntohs(((guint16*) FLX_DNS_PACKET_DATA(p))[index]);
 }
 
-/* Read the first label from string dest, unescape "\" and append it to *name */
-static gchar *unescape_label(gchar *dest, guint size, const gchar **name) {
+/* Read the first label from string *name, unescape "\" and write it to dest */
+gchar *flx_unescape_label(gchar *dest, guint size, const gchar **name) {
     guint i = 0;
     gchar *d;
     
@@ -106,19 +106,20 @@ static gchar *unescape_label(gchar *dest, guint size, const gchar **name) {
 }
 
 guint8* flx_dns_packet_append_name(flxDnsPacket *p, const gchar *name) {
-    guint8 *d, *f = NULL;
+    guint8 *d, *saved_ptr = NULL;
     guint saved_size;
     
     g_assert(p);
     g_assert(name);
 
     saved_size = p->size;
-
+    saved_ptr = flx_dns_packet_extend(p, 0);
+    
     while (*name) {
         guint n;
         guint8* prev;
         const gchar *pname;
-        char label[64];
+        gchar label[64];
 
         /* Check whether we can compress this name. */
 
@@ -135,22 +136,19 @@ guint8* flx_dns_packet_append_name(flxDnsPacket *p, const gchar *name) {
                 if (!(t = (guint16*) flx_dns_packet_extend(p, sizeof(guint16))))
                     return NULL;
 
-                if (!f)
-                    f = (guint8*) t;
-    
                 *t = g_htons((0xC000 | index));
-                return f;
+                return saved_ptr;
             }
         }
 
         pname = name;
         
-        if (!(unescape_label(label, sizeof(label), &name)))
+        if (!(flx_unescape_label(label, sizeof(label), &name)))
             goto fail;
 
         if (!(d = flx_dns_packet_append_string(p, label)))
             goto fail;
-        
+
         if (!p->name_table)
             p->name_table = g_hash_table_new_full((GHashFunc) flx_domain_hash, (GEqualFunc) flx_domain_equal, g_free, NULL);
 
@@ -162,7 +160,7 @@ guint8* flx_dns_packet_append_name(flxDnsPacket *p, const gchar *name) {
     
     *d = 0;
 
-    return f;
+    return saved_ptr;
 
 fail:
     p->size = saved_size;