aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWu Guanghao <wuguanghao3@huawei.com>2021-07-28 09:56:45 +0800
committerTheodore Ts'o <tytso@mit.edu>2021-08-02 21:56:42 -0400
commitfeccf49871de4b05f9d99aca2df578947be98188 (patch)
tree11305c83c45b7b220e00eb1972e0da3d2b90e3d8
parent53464654bd33e58e3fff079f34261b823d839f3b (diff)
downloade2fsprogs-feccf49871de4b05f9d99aca2df578947be98188.tar.gz
ss_add_info_dir: fix error handling when memory allocation fails
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>
-rw-r--r--lib/ss/help.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/ss/help.c b/lib/ss/help.c
index 5204401be..96eb10920 100644
--- a/lib/ss/help.c
+++ b/lib/ss/help.c
@@ -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;
}