Whamcloud - gitweb
ss_add_info_dir: fix error handling when memory allocation fails
authorWu Guanghao <wuguanghao3@huawei.com>
Wed, 28 Jul 2021 01:56:45 +0000 (09:56 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 3 Aug 2021 01:56:42 +0000 (21:56 -0400)
If the realloc() and malloc() calls fail, avoid a memory leak as well
as a potential seg fault.

[ Fix error code setting to avoid depending on malloc() and realloc()
  setting errno. -- TYT ]

Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Reviewed-by: Wu Bo <wubo40@huawei.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ss/help.c

index 5204401..96eb109 100644 (file)
@@ -148,13 +148,16 @@ void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr)
     dirs = (char **)realloc((char *)dirs,
                            (unsigned)(n_dirs + 2)*sizeof(char *));
     if (dirs == (char **)NULL) {
-       info->info_dirs = (char **)NULL;
-       *code_ptr = errno;
+       *code_ptr = ENOMEM;
        return;
     }
     info->info_dirs = dirs;
     dirs[n_dirs + 1] = (char *)NULL;
     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
+    if (dirs[n_dirs] == (char *)NULL) {
+        *code_ptr = ENOMEM;
+        return;
+    }
     strcpy(dirs[n_dirs], info_dir);
     *code_ptr = 0;
 }