On 05/08/2015 04:31 PM, Wei,Jiangang wrote:
> * 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/syscalls/ipc/lib/libipc.c | 43 
> +++++++++++++++++++-----------
>  1 file changed, 28 insertions(+), 15 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/ipc/lib/libipc.c 
> b/testcases/kernel/syscalls/ipc/lib/libipc.c
> index f10e257..63304f8 100644
> --- a/testcases/kernel/syscalls/ipc/lib/libipc.c
> +++ b/testcases/kernel/syscalls/ipc/lib/libipc.c
> @@ -141,21 +141,34 @@ void rm_sema(int sem_id)
>   */
>  int getuserid(char *user)
>  {
> -     struct passwd *ent;
> -
> -     /* allocate some space for the passwd struct */
> -     if ((ent = malloc(sizeof(struct passwd))) == NULL) {
> -             tst_brkm(TBROK, cleanup, "couldn't allocate space for passwd"
> -                      " structure");
> -     }
> -
> -     /* get the uid value for the user */
> -     if ((ent = getpwnam(user)) == NULL) {
> -             tst_brkm(TBROK, cleanup, "Couldn't get password entry for %s",
> -                      user);
> -     }
> -
> -     return (ent->pw_uid);
> +     struct passwd pwd;
> +     struct passwd *result;
> +     char *buf;
> +     size_t buflen;
> +     int s;
> +
> +     buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
> +     if (buflen == -1)
> +             /* Should be more than enough */
> +             buflen = 16384;
> +
> +     buf = malloc(buflen);
> +     if (buf == NULL)
> +             tst_brkm(TBROK, NULL, "malloc failed.\n");
> +
> +     s = getpwnam_r(user, &pwd, buf, buflen, &result);

Since the data stored in buf will not be used later, it can be freed even here
just after the function returned?

What do you think, Cyril?


Thanks,
Wanlong Gao

> +     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;
>  }
>  
>  /*
> 


------------------------------------------------------------------------------
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