Browse code

dns: fix missing out-of-memory check

- fix missing out-of-memory check when creating a negative cache
entry
- minor optimizations (likely)

Andrei Pelinescu-Onciul authored on 08/06/2009 08:50:07
Showing 1 changed files
... ...
@@ -1670,7 +1670,7 @@ inline static struct dns_hash_entry* dns_cache_do_request(str* name, int type)
1670 1670
 	if (type==T_A){
1671 1671
 		if ((ip=str2ip(name))!=0){
1672 1672
 				e=dns_cache_mk_ip_entry(name, ip);
1673
-				if (e)
1673
+				if (likely(e))
1674 1674
 					atomic_set(&e->refcnt, 1);/* because we ret. a ref. to it*/
1675 1675
 				goto end; /* we do not cache obvious stuff */
1676 1676
 		}
... ...
@@ -1679,7 +1679,7 @@ inline static struct dns_hash_entry* dns_cache_do_request(str* name, int type)
1679 1679
 	else if (type==T_AAAA){
1680 1680
 		if ((ip=str2ip6(name))!=0){
1681 1681
 				e=dns_cache_mk_ip_entry(name, ip);
1682
-				if (e)
1682
+				if (likely(e))
1683 1683
 					atomic_set(&e->refcnt, 1);/* because we ret. a ref. to it*/
1684 1684
 				goto end;/* we do not cache obvious stuff */
1685 1685
 		}
... ...
@@ -1701,12 +1701,12 @@ inline static struct dns_hash_entry* dns_cache_do_request(str* name, int type)
1701 1701
 	if (records){
1702 1702
 #ifdef CACHE_RELEVANT_RECS_ONLY
1703 1703
 		e=dns_cache_mk_rd_entry(name, type, &records);
1704
-		if (e){
1704
+		if (likely(e)){
1705 1705
 			l=e;
1706 1706
 			e=dns_get_related(l, type, &records);
1707 1707
 			/* e should contain the searched entry (if found) and l
1708 1708
 			 * all the entries (e and related) */
1709
-			if (e){
1709
+			if (likely(e)){
1710 1710
 				atomic_set(&e->refcnt, 1); /* 1 because we return a
1711 1711
 												ref. to it */
1712 1712
 			}else{
... ...
@@ -1747,9 +1747,12 @@ inline static struct dns_hash_entry* dns_cache_do_request(str* name, int type)
1747 1747
 #endif
1748 1748
 		free_rdata_list(records);
1749 1749
 	}else if (cfg_get(core, core_cfg, dns_neg_cache_ttl)){
1750
-		e=dns_cache_mk_bad_entry(name, type, cfg_get(core, core_cfg, dns_neg_cache_ttl), DNS_BAD_NAME);
1751
-		atomic_set(&e->refcnt, 1); /* 1 because we return a ref. to it */
1752
-		dns_cache_add(e); /* refcnt++ inside*/
1750
+		e=dns_cache_mk_bad_entry(name, type, 
1751
+				cfg_get(core, core_cfg, dns_neg_cache_ttl), DNS_BAD_NAME);
1752
+		if (likely(e)) {
1753
+			atomic_set(&e->refcnt, 1); /* 1 because we return a ref. to it */
1754
+			dns_cache_add(e); /* refcnt++ inside*/
1755
+		}
1753 1756
 		goto end;
1754 1757
 	}
1755 1758
 #ifndef CACHE_RELEVANT_RECS_ONLY