[SSSD] [PATCH] SYSDB: NULL-terminate the output of sysdb_get_{ranges, subdomains}

Jakub Hrozek jhrozek at redhat.com
Sun Sep 9 19:31:17 UTC 2012


I noticed (using valgrind) that the sysdb tests access memory outside
the array allocated as a result of the functions in subject. The attached
patch NULL-terminates the array so that it's safe to loop over the results.
-------------- next part --------------
>From 3f3dadf53eee7984f6ef848557aec03b439c6260 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Sun, 9 Sep 2012 21:07:37 +0200
Subject: [PATCH] SYSDB: NULL-terminate the output of
 sysdb_get_{ranges,subdomains}

---
 src/db/sysdb_ranges.c     | 3 ++-
 src/db/sysdb_subdomains.c | 4 +++-
 src/tests/sysdb-tests.c   | 4 ++--
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/db/sysdb_ranges.c b/src/db/sysdb_ranges.c
index 416bcdd020be6e527776eb20cd80a08bbd7dc744..25c75fbdcc8ecd8d7bc5be39a973ef0845edf4ff 100644
--- a/src/db/sysdb_ranges.c
+++ b/src/db/sysdb_ranges.c
@@ -78,7 +78,7 @@ errno_t sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
         goto done;
     }
 
-    list = talloc_zero_array(tmp_ctx, struct range_info *, res->count);
+    list = talloc_zero_array(tmp_ctx, struct range_info *, res->count+1);
     if (list == NULL) {
         ret = ENOMEM;
         goto done;
@@ -142,6 +142,7 @@ errno_t sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
             goto done;
         }
     }
+    list[res->count] = NULL;
 
     *range_count = res->count;
     *range_list = talloc_steal(mem_ctx, list);
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index 96b8b4b210e8b1eb32328c1c49495d6897da0365..b3998a2dbe875692d6730ff9b7bf2b2644601a79 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -59,7 +59,7 @@ errno_t sysdb_get_subdomains(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
         goto done;
     }
 
-    list = talloc_zero_array(tmp_ctx, struct sysdb_subdom *, res->count);
+    list = talloc_zero_array(tmp_ctx, struct sysdb_subdom *, res->count + 1);
     if (list == NULL) {
         ret = ENOMEM;
         goto done;
@@ -117,6 +117,8 @@ errno_t sysdb_get_subdomains(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
         }
     }
 
+    list[res->count] = NULL;
+
     *subdomain_count = res->count;
     *subdomain_list = talloc_steal(mem_ctx, list);
     ret = EOK;
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index be86f846a03de04322a569245502862fcc7f3ae2..88d959c0ba30b6b0da8faf523ac50be45c76502f 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -3414,7 +3414,7 @@ START_TEST(test_sysdb_subdomain_create)
     fail_unless(ret == EOK, "sysdb_get_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
     fail_unless(cur_subdomains != NULL, "No sub-domains returned.");
-    fail_unless(cur_subdomains[0] == NULL, "No empyt sub-domain list returned.");
+    fail_unless(cur_subdomains[0] == NULL, "No empty sub-domain list returned.");
 
     ret = sysdb_update_subdomains(test_ctx->sysdb, num_subdom1, new_subdom1);
     fail_unless(ret == EOK, "sysdb_update_subdomains failed with [%d][%s]",
@@ -3453,7 +3453,7 @@ START_TEST(test_sysdb_subdomain_create)
     fail_unless(ret == EOK, "sysdb_get_subdomains failed with [%d][%s]",
                             ret, strerror(ret));
     fail_unless(cur_subdomains != NULL, "No sub-domains returned.");
-    fail_unless(cur_subdomains[0] == NULL, "No empyt sub-domain list returned.");
+    fail_unless(cur_subdomains[0] == NULL, "No empty sub-domain list returned.");
 }
 END_TEST
 
-- 
1.7.11.4



More information about the sssd-devel mailing list