gencache: Refactor gencache_set_data_blob
authorVolker Lendecke <vl@samba.org>
Sun, 13 Dec 2015 20:16:36 +0000 (21:16 +0100)
committerJeremy Allison <jra@samba.org>
Mon, 14 Dec 2015 19:23:13 +0000 (20:23 +0100)
Replace 3 calls into talloc with 1. Add an overflow check.

With this change, it will be easier to avoid the talloc call for small
blobs in the future and do it on the stack.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/lib/gencache.c

index ed78476a6e5993de0934b9ea859e3faca2e93325..ed16d79f7ddd597bdf33bd93f309f6e86163b4e2 100644 (file)
@@ -283,6 +283,8 @@ bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
                            time_t timeout)
 {
        int ret;
+       fstring hdr;
+       int hdr_len;
        char* val;
        time_t last_stabilize;
        static int writecount;
@@ -307,19 +309,23 @@ bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
                return true;
        }
 
-       val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
-       if (val == NULL) {
+       hdr_len = fstr_sprintf(hdr, CACHE_DATA_FMT, (int)timeout);
+
+       if (hdr_len == -1) {
                return false;
        }
-       val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
-       if (val == NULL) {
+       if ((blob->length + (size_t)hdr_len) < blob->length) {
                return false;
        }
-       val = (char *)talloc_append_blob(NULL, val, *blob);
+
+       val = talloc_array(talloc_tos(), char, hdr_len + blob->length);
        if (val == NULL) {
                return false;
        }
 
+       memcpy(val, hdr, hdr_len);
+       memcpy(val+hdr_len, blob->data, blob->length);
+
        DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
                   "[%s] (%d seconds %s)\n", keystr,
                   timestring(talloc_tos(), timeout),