From: Volker Lendecke Date: Sun, 13 Dec 2015 20:16:36 +0000 (+0100) Subject: gencache: Refactor gencache_set_data_blob X-Git-Url: http://git.samba.org/?a=commitdiff_plain;h=8ca3a58ec923d9e3f69c2a8971873acedce761d6;p=obnox%2Fsamba%2Fsamba-obnox.git gencache: Refactor gencache_set_data_blob 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 Reviewed-by: Jeremy Allison --- diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index ed78476a6e5..ed16d79f7dd 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -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),