[Ocfs2-tools-devel] [PATCH 5/5] mount.ocfs2: Check if 's' is NULL before free it

Goldwyn Rodrigues rgoldwyn at suse.de
Fri Apr 24 08:11:46 PDT 2015


Hi piaojun,

On 04/02/2015 07:54 AM, piaojun wrote:
> In xstrconcat2() and xstrconcat3(), we should check if 's' is null before
> free it.
>
> Signed-off-by: Jun Piao <piaojun at huawei.com>
> Reviewed-by: Alex Chen <alex.chen at huawei.com>
>
> ---
>   mount.ocfs2/sundries.c | 21 +++++++++++++++------
>   1 file changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/mount.ocfs2/sundries.c b/mount.ocfs2/sundries.c
> index 2e1b95a..e1cefb6 100644
> --- a/mount.ocfs2/sundries.c
> +++ b/mount.ocfs2/sundries.c
> @@ -48,15 +48,20 @@ xstrconcat2 (const char *s, const char *t) {
>   char *
>   xstrconcat3 (const char *s, const char *t, const char *u) {
>        char *res;
> -
> -     if (!s) s = "";
> +     int free_flag = 1;
> +     if (!s){
> +         s = "";
> +         free_flag = 0;
> +     }
>        if (!t) t = "";
>        if (!u) u = "";
>        res = xmalloc(strlen(s) + strlen(t) + strlen(u) + 1);
>        strcpy(res, s);
>        strcat(res, t);
>        strcat(res, u);
> -     free((void *) s);
> +     if (free_flag){
> +         free((void *) s);
> +     }
>        return res;
>   }

Instead of using an additional flag, could you use -

if (strlen(s))
	free(s);

It looks neater, and is easier to understand.

-- 
-- 
Goldwyn



More information about the Ocfs2-tools-devel mailing list