summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Kelley <simon@thekelleys.org.uk>2018-09-26 16:50:35 +0100
committerSimon Kelley <simon@thekelleys.org.uk>2018-09-26 16:50:35 +0100
commit3a610a007fd80751330591c7e1a3a5d31e72727b (patch)
tree2630145f57b997e0b16f965199921f73433c2b3c
parent48b090cb5cae11cb4c3dfb2bac69a9c7fb1af12c (diff)
downloaddnsmasq-2.80test7.tar.gz
Finesse allocation of memory for "struct crec" cache entries.v2.80test7
These normally have enough space for a name of up to SMALLDNAME characters. When used to hold /etc/hosts entries, they are allocated with just enough bytes for the name held. When used to hold other configured stuff, (CNAMES DS records. DHCP names etc), the name is replaced by a pointer to a string held elsewhere, and F_NAMEP set. Hence only enough space to hold a char * is needed, rather than SMALLDNAME bytes.
-rw-r--r--src/cache.c21
-rw-r--r--src/dnsmasq.h3
2 files changed, 13 insertions, 11 deletions
diff --git a/src/cache.c b/src/cache.c
index 68cdc85..6daaf3e 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -825,7 +825,7 @@ static void add_hosts_cname(struct crec *target)
for (a = daemon->cnames; a; a = a->next)
if (a->alias[1] != '*' &&
hostname_isequal(cache_get_name(target), a->target) &&
- (crec = whine_malloc(sizeof(struct crec))))
+ (crec = whine_malloc(SIZEOF_POINTER_CREC)))
{
crec->flags = F_FORWARD | F_IMMORTAL | F_NAMEP | F_CONFIG | F_CNAME;
crec->ttd = a->ttl;
@@ -1029,8 +1029,7 @@ int read_hostsfile(char *filename, unsigned int index, int cache_size, struct cr
{
/* If set, add a version of the name with a default domain appended */
if (option_bool(OPT_EXPAND) && domain_suffix && !fqdn &&
- (cache = whine_malloc(sizeof(struct crec) +
- strlen(canon)+2+strlen(domain_suffix)-SMALLDNAME)))
+ (cache = whine_malloc(SIZEOF_BARE_CREC + strlen(canon) + 2 + strlen(domain_suffix))))
{
strcpy(cache->name.sname, canon);
strcat(cache->name.sname, ".");
@@ -1040,7 +1039,7 @@ int read_hostsfile(char *filename, unsigned int index, int cache_size, struct cr
add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz);
name_count++;
}
- if ((cache = whine_malloc(sizeof(struct crec) + strlen(canon)+1-SMALLDNAME)))
+ if ((cache = whine_malloc(SIZEOF_BARE_CREC + strlen(canon) + 1)))
{
strcpy(cache->name.sname, canon);
cache->flags = flags;
@@ -1113,7 +1112,7 @@ void cache_reload(void)
for (intr = daemon->int_names; intr; intr = intr->next)
if (a->alias[1] != '*' &&
hostname_isequal(a->target, intr->name) &&
- ((cache = whine_malloc(sizeof(struct crec)))))
+ ((cache = whine_malloc(SIZEOF_POINTER_CREC))))
{
cache->flags = F_FORWARD | F_NAMEP | F_CNAME | F_IMMORTAL | F_CONFIG;
cache->ttd = a->ttl;
@@ -1128,7 +1127,7 @@ void cache_reload(void)
#ifdef HAVE_DNSSEC
for (ds = daemon->ds; ds; ds = ds->next)
- if ((cache = whine_malloc(sizeof(struct crec))) &&
+ if ((cache = whine_malloc(SIZEOF_POINTER_CREC)) &&
(cache->addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen)))
{
cache->flags = F_FORWARD | F_IMMORTAL | F_DS | F_CONFIG | F_NAMEP;
@@ -1155,7 +1154,7 @@ void cache_reload(void)
for (nl = hr->names; nl; nl = nl->next)
{
if (hr->addr.s_addr != 0 &&
- (cache = whine_malloc(sizeof(struct crec))))
+ (cache = whine_malloc(SIZEOF_POINTER_CREC)))
{
cache->name.namep = nl->name;
cache->ttd = hr->ttl;
@@ -1164,7 +1163,7 @@ void cache_reload(void)
}
#ifdef HAVE_IPV6
if (!IN6_IS_ADDR_UNSPECIFIED(&hr->addr6) &&
- (cache = whine_malloc(sizeof(struct crec))))
+ (cache = whine_malloc(SIZEOF_POINTER_CREC)))
{
cache->name.namep = nl->name;
cache->ttd = hr->ttl;
@@ -1241,7 +1240,7 @@ static void add_dhcp_cname(struct crec *target, time_t ttd)
if ((aliasc = dhcp_spare))
dhcp_spare = dhcp_spare->next;
else /* need new one */
- aliasc = whine_malloc(sizeof(struct crec));
+ aliasc = whine_malloc(SIZEOF_POINTER_CREC);
if (aliasc)
{
@@ -1332,7 +1331,7 @@ void cache_add_dhcp_entry(char *host_name, int prot,
if ((crec = dhcp_spare))
dhcp_spare = dhcp_spare->next;
else /* need new one */
- crec = whine_malloc(sizeof(struct crec));
+ crec = whine_malloc(SIZEOF_POINTER_CREC);
if (crec) /* malloc may fail */
{
@@ -1432,7 +1431,7 @@ static void make_non_terminals(struct crec *source)
}
else
#endif
- crecp = whine_malloc(sizeof(struct crec));
+ crecp = whine_malloc(SIZEOF_POINTER_CREC);
if (crecp)
{
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
index e85ec2c..46fb4e5 100644
--- a/src/dnsmasq.h
+++ b/src/dnsmasq.h
@@ -442,6 +442,9 @@ struct crec {
} name;
};
+#define SIZEOF_BARE_CREC (sizeof(struct crec) - SMALLDNAME)
+#define SIZEOF_POINTER_CREC (sizeof(struct crec) + sizeof(char *) - SMALLDNAME)
+
#define F_IMMORTAL (1u<<0)
#define F_NAMEP (1u<<1)
#define F_REVERSE (1u<<2)