From 6c28cb81b2a326a416968932919ea9d19f3b56ab Mon Sep 17 00:00:00 2001 From: "Anand V. Avati" Date: Thu, 26 Mar 2009 17:29:52 +0530 Subject: fix duplicate setting of values in inode->ctx and fd->ctx this patch avoids setting of duplicate key/value pairs in the context. note that consumers have to explicitly check for previous existance of key to avoid any kind of resource leak resulting from this overwrite. Signed-off-by: Anand V. Avati --- libglusterfs/src/inode.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'libglusterfs/src/inode.c') diff --git a/libglusterfs/src/inode.c b/libglusterfs/src/inode.c index c62d7ca9e..90a84f955 100644 --- a/libglusterfs/src/inode.c +++ b/libglusterfs/src/inode.c @@ -1109,20 +1109,28 @@ __inode_ctx_put (inode_t *inode, xlator_t *xlator, uint64_t value) { int ret = 0; int index = 0; + int put_idx = -1; for (index = 0; index < xlator->ctx->xl_count; index++) { - if (!inode->_ctx[index].key || - (inode->_ctx[index].key == (uint64_t)(long)xlator)) + if (!inode->_ctx[index].key) { + if (put_idx == -1) + put_idx = index; + /* dont break, to check if key already exists + further on */ + } + if (inode->_ctx[index].key == (uint64_t)(long) xlator) { + put_idx = index; break; + } } - if (index == xlator->ctx->xl_count) { + if (put_idx == -1) { ret = -1; goto out;; } - inode->_ctx[index].key = (uint64_t)(long) xlator; - inode->_ctx[index].value = value; + inode->_ctx[put_idx].key = (uint64_t)(long) xlator; + inode->_ctx[put_idx].value = value; out: return ret; } -- cgit