From 12bbd939c1c2c55d0906b5a53a0b9b26b616c56b Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Fri, 10 Jun 2022 04:16:48 +0100
Subject: [PATCH 19/30] add null terminal at end of gr mem list

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 src/utils/cutils/utils_pwgr.c           | 4 +++-
 test/cutils/utils_pwgr/utils_pwgr_ut.cc | 9 +++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/utils/cutils/utils_pwgr.c b/src/utils/cutils/utils_pwgr.c
index b3aed14b..a3c41dd5 100644
--- a/src/utils/cutils/utils_pwgr.c
+++ b/src/utils/cutils/utils_pwgr.c
@@ -144,7 +144,7 @@ static char **hold_string_list(char **line, char *buf_start, char *buf_end, cons
 
     for (; walker < (char **)buf_end; ++walker) {
         if (**line == '\0') {
-            return result;
+            goto out;
         }
 
         (void)util_trim_space(*line);
@@ -155,6 +155,8 @@ static char **hold_string_list(char **line, char *buf_start, char *buf_end, cons
         }
     }
 
+out:
+    *walker = NULL;
     return result;
 }
 
diff --git a/test/cutils/utils_pwgr/utils_pwgr_ut.cc b/test/cutils/utils_pwgr/utils_pwgr_ut.cc
index c289e2b9..2a6b7e08 100644
--- a/test/cutils/utils_pwgr/utils_pwgr_ut.cc
+++ b/test/cutils/utils_pwgr/utils_pwgr_ut.cc
@@ -123,8 +123,13 @@ TEST(utils_pwgr, test_getgrent_r)
         ASSERT_STREQ(gr.gr_passwd, std::get<1>(testcase[i]).c_str());
         ASSERT_EQ(gr.gr_gid, std::get<2>(testcase[i]));
         if (string_list[i].size()) {
-            for (j = 0; j < string_list[i].size(); ++j) {
-                EXPECT_TRUE(strcmp(gr.gr_mem[j], string_list[i][j].c_str()) == 0);
+            char **walker = gr.gr_mem;
+            j = 0;
+            // use pointer to ensure gr_mem has end null pointer
+            while (walker != NULL && *walker != NULL) {
+                EXPECT_TRUE(strcmp(*walker, string_list[i][j].c_str()) == 0);
+                walker++;
+                j++;
             }
         }
         EXPECT_TRUE(pgr == &gr);
-- 
2.32.1 (Apple Git-133)

