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/fd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'libglusterfs/src/fd.c') diff --git a/libglusterfs/src/fd.c b/libglusterfs/src/fd.c index 44149246ba9..918b6c14dcc 100644 --- a/libglusterfs/src/fd.c +++ b/libglusterfs/src/fd.c @@ -548,6 +548,7 @@ fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value) { int index = 0; int ret = 0; + int set_idx = -1; if (!fd || !xlator) return -1; @@ -555,18 +556,25 @@ fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value) LOCK (&fd->lock); { for (index = 0; index < xlator->ctx->xl_count; index++) { - if (!fd->_ctx[index].key || - (fd->_ctx[index].key == (uint64_t)(long)xlator)) + if (!fd->_ctx[index].key) { + if (set_idx == -1) + set_idx = index; + /* dont break, to check if key already exists + further on */ + } + if (fd->_ctx[index].key == (uint64_t)(long) xlator) { + set_idx = index; break; + } } - if (index == xlator->ctx->xl_count) { + if (set_idx == -1) { ret = -1; goto unlock; } - fd->_ctx[index].key = (uint64_t)(long) xlator; - fd->_ctx[index].value = value; + fd->_ctx[set_idx].key = (uint64_t)(long) xlator; + fd->_ctx[set_idx].value = value; } unlock: UNLOCK (&fd->lock); -- cgit