ubus: constify name argument to mdns_add_records
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index da65b1f..07a0857 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -66,10 +66,6 @@ cache_entry_free(struct cache_entry *s)
 {
        DBG(2, "%s\n", s->entry);
        avl_delete(&entries, &s->avl);
 {
        DBG(2, "%s\n", s->entry);
        avl_delete(&entries, &s->avl);
-       if (s->host)
-               free(s->host);
-       if (s->entry)
-               free(s->entry);
        free(s);
 }
 
        free(s);
 }
 
@@ -162,21 +158,26 @@ static struct cache_entry*
 cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
 {
        struct cache_entry *s;
 cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
 {
        struct cache_entry *s;
+       char *entry_buf;
+       char *host_buf;
        char *type;
 
        s = avl_find_element(&entries, entry, s, avl);
        if (s)
                return s;
 
        char *type;
 
        s = avl_find_element(&entries, entry, s, avl);
        if (s)
                return s;
 
-       s = malloc(sizeof(struct cache_entry));
-       memset(s, 0, sizeof(struct cache_entry));
-       s->avl.key = s->entry = strdup(entry);
+       s = calloc_a(sizeof(*s),
+               &entry_buf, strlen(entry) + 1,
+               &host_buf, hlen ? hlen + 1 : 0);
+
+       s->avl.key = s->entry = strcpy(entry_buf, entry);
        s->time = time(NULL);
        s->ttl = ttl;
 
        if (hlen)
        s->time = time(NULL);
        s->ttl = ttl;
 
        if (hlen)
-               s->host = strndup(s->entry, hlen);
-       type = strstr(s->entry, "._");
+               s->host = strncpy(host_buf, s->entry, hlen);
+
+       type = strstr(entry_buf, "._");
        if (type)
                type++;
        if (type)
        if (type)
                type++;
        if (type)