* No need to allocate memory for getpwnam()'s return,
  So remove these codes.

* replace getpwnam() with getpwnam_r() that is thread-safe.

Signed-off-by: Wei,Jiangang <weijg.f...@cn.fujitsu.com>
---
 testcases/kernel/mem/hugetlb/lib/libipc.c | 33 +++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/lib/libipc.c 
b/testcases/kernel/mem/hugetlb/lib/libipc.c
index 21fd8da..91cc952 100644
--- a/testcases/kernel/mem/hugetlb/lib/libipc.c
+++ b/testcases/kernel/mem/hugetlb/lib/libipc.c
@@ -83,17 +83,34 @@ int getipckey(void)
  */
 int getuserid(char *user)
 {
-       struct passwd *ent;
+       struct passwd pwd;
+       struct passwd *result;
+       char *buf;
+       size_t buflen;
+       int s;
 
-       ent = malloc(sizeof(struct passwd));
-       if (ent == NULL)
-               tst_brkm(TBROK | TERRNO, cleanup, "malloc ent");
+       buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+       if (buflen == -1)
+               /* Should be more than enough */
+               buflen = 16384;
 
-       ent = getpwnam(user);
-       if (ent == NULL)
-               tst_brkm(TBROK | TERRNO, cleanup, "getpwnam");
+       buf = malloc(buflen);
+       if (buf == NULL)
+               tst_brkm(TBROK, NULL, "malloc failed.\n");
 
-       return ent->pw_uid;
+       s = getpwnam_r(user, &pwd, buf, buflen, &result);
+       if (result == NULL) {
+               free(buf);
+               if (s == 0) {
+                       tst_brkm(TBROK, NULL, "Not found: %s", user);
+               } else {
+                       tst_brkm(TBROK, NULL, "getpwnam_r failed. (%d) %s",
+                               (s), strerror(s));
+               }
+       }
+       free(buf);
+
+       return pwd.pw_uid;
 }
 
 /*
-- 
1.9.3


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to