From 60083fb99d1b007310bcf8a8be97397f8176fbeb Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 19 Jun 2014 12:09:06 +0100
Subject: [PATCH 22/45] SYSDB: sysdb_search_entry fix memory leak
Allocate res on tmp_ctx instead of on mem_ctx.
Also use '_' prefix convention for output parameters.
Reviewed-by: Stephen Gallagher <sgallagh@redhat.com>
(cherry picked from commit 09579ae252c181c7884defc0612c36108f6cf509)
---
src/db/sysdb.h | 4 ++--
src/db/sysdb_ops.c | 29 ++++++++++++++++++++---------
2 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 510687495c26d8b560c1616dee231ec9e5c99785..1bb5fb86115be00577a4c5d07a9b28a518f1974e 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -516,8 +516,8 @@ int sysdb_search_entry(TALLOC_CTX *mem_ctx,
int scope,
const char *filter,
const char **attrs,
- size_t *msgs_count,
- struct ldb_message ***msgs);
+ size_t *_msgs_count,
+ struct ldb_message ***_msgs);
/* Search User (by uid, sid or name) */
int sysdb_search_user_by_name(TALLOC_CTX *mem_ctx,
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 355967311c7a54e9f86bd3cae0b34a9e42b8023f..3a41f514aef1f1cd5612180963df397127b85188 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -211,27 +211,38 @@ int sysdb_search_entry(TALLOC_CTX *mem_ctx,
int scope,
const char *filter,
const char **attrs,
- size_t *msgs_count,
- struct ldb_message ***msgs)
+ size_t *_msgs_count,
+ struct ldb_message ***_msgs)
{
+ TALLOC_CTX *tmp_ctx;
struct ldb_result *res;
int ret;
- ret = ldb_search(sysdb->ldb, mem_ctx, &res,
+ tmp_ctx = talloc_new(NULL);
+ if (tmp_ctx == NULL) {
+ ret = ENOMEM;
+ goto done;
+ }
+
+ ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
base_dn, scope, attrs,
filter?"%s":NULL, filter);
- if (ret) {
- return sysdb_error_to_errno(ret);
+ if (ret != EOK) {
+ ret = sysdb_error_to_errno(ret);
+ goto done;
}
- *msgs_count = res->count;
- *msgs = talloc_steal(mem_ctx, res->msgs);
+ *_msgs_count = res->count;
+ *_msgs = talloc_steal(mem_ctx, res->msgs);
if (res->count == 0) {
- return ENOENT;
+ ret = ENOENT;
+ goto done;
}
- return EOK;
+done:
+ talloc_zfree(tmp_ctx);
+ return ret;
}
/* =Search-Entry-by-SID-string============================================ */
--
2.4.3