Merge branch 'master' of ssh://thekelleys.org.uk/var/local/git/dnsmasq
diff --git a/src/rfc1035.c b/src/rfc1035.c
index 502744b..24b778a 100644
--- a/src/rfc1035.c
+++ b/src/rfc1035.c
@@ -1352,6 +1352,8 @@
 
   for (q = ntohs(header->qdcount); q != 0; q--)
     {
+      int count = 255; /* catch loops */
+      
       /* save pointer to name for copying into answers */
       nameoffset = p - (unsigned char *)header;
 
@@ -1364,7 +1366,7 @@
 
       ans = 0; /* have we answered this question */
 
-      while ((crecp = cache_find_by_name(NULL, name, now, F_CNAME)))
+      while (--count != 0 && (crecp = cache_find_by_name(NULL, name, now, F_CNAME)))
 	{
 	  char *cname_target = cache_get_cname_target(crecp);
 
@@ -1388,9 +1390,10 @@
 					  T_CNAME, C_IN, "d", cname_target))
 		    anscount++;
 		}
-	  
-	      strcpy(name, cname_target);
+
 	    }
+
+	  strcpy(name, cname_target);
 	}
 	  
       if (qtype == T_TXT || qtype == T_ANY)