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

piaojun piaojun at huawei.com
Thu Apr 2 05:54:16 PDT 2015


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

@@ -64,8 +69,11 @@ xstrconcat3 (const char *s, const char *t, const char *u) {
 char *
 xstrconcat4 (const char *s, const char *t, const char *u, const char *v) {
      char *res;
-
-     if (!s) s = "";
+     int free_flag = 1;
+     if (!s) {
+         s = "";
+         free_flag = 0;
+     }
      if (!t) t = "";
      if (!u) u = "";
      if (!v) v = "";
@@ -74,7 +82,8 @@ xstrconcat4 (const char *s, const char *t, const char *u, const char *v) {
      strcat(res, t);
      strcat(res, u);
      strcat(res, v);
-     free((void *) s);
+     if (free_flag)
+         free((void *) s);
      return res;
 }

-- 
1.8.4.3




More information about the Ocfs2-tools-devel mailing list