diff mbox series

[06/12] append_pathname: check the value returned by realloc to avoid segfault

Message ID 07fe127f-3814-7d12-dea6-b84d9ab4410e@huawei.com
State Superseded
Headers show
Series e2fsprogs: some bugfixs and some code cleanups | expand

Commit Message

wuguanghao May 24, 2021, 11:23 a.m. UTC
In append_pathname(), we need to add a new path to save the value returned by realloc,
otherwise the name->path may be NULL, causing segfault

Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
 contrib/fsstress.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

--

Comments

Artem Blagodarenko May 25, 2021, 9:37 a.m. UTC | #1
Hello Wu,

Thanks for the patch.

> On 24 May 2021, at 14:23, Wu Guanghao <wuguanghao3@huawei.com> wrote:
> 
> In append_pathname(), we need to add a new path to save the value returned by realloc,
> otherwise the name->path may be NULL, causing segfault
> 
> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> ---
> contrib/fsstress.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/contrib/fsstress.c b/contrib/fsstress.c
> index 2a983482..530bd920 100644
> --- a/contrib/fsstress.c
> +++ b/contrib/fsstress.c
> @@ -599,7 +599,7 @@ void add_to_flist(int ft, int id, int parent)
> void append_pathname(pathname_t * name, char *str)
> {
> 	int len;
> -
> +	char *path;:
> 	len = strlen(str);
> #ifdef DEBUG
> 	if (len && *str == '/' && name->len == 0) {
> @@ -609,7 +609,13 @@ void append_pathname(pathname_t * name, char *str)
> 
> 	}
> #endif
> -	name->path = realloc(name->path, name->len + 1 + len);
> +	path = realloc(name->path, name->len + 1 + len);
> +	if (path == NULL) {
> +		fprintf(stderr, "fsstress: append_pathname realloc failed\n");
> +		chadir(homedir);

Did you mean chdir() here?

> +		abort();
> +	}
> +	name->path = path;
> 	strcpy(&name->path[name->len], str);
> 	name->len += len;
> }
> -- 


Thanks,
Best regards,
Artem Blagodarenko.
wuguanghao May 25, 2021, 11:26 a.m. UTC | #2
Hello Artem Blagodarenko,

Thank you for your review, it should be chdir instead of chadir,
I will modify it in the v2 version. Next time I will take care not to make similar mistakes.
Thanks a lot.

Thanks,
Best regards,
Wu Guanghao

在 2021/5/25 17:37, Благодаренко Артём 写道:
> Hello Wu,
> 
> Thanks for the patch.
> 
>> On 24 May 2021, at 14:23, Wu Guanghao <wuguanghao3@huawei.com> wrote:
>>
>> In append_pathname(), we need to add a new path to save the value returned by realloc,
>> otherwise the name->path may be NULL, causing segfault
>>
>> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>> ---
>> contrib/fsstress.c | 10 ++++++++--
>> 1 file changed, 8 insertions(+), 2 deletions(-)
>>
>> diff --git a/contrib/fsstress.c b/contrib/fsstress.c
>> index 2a983482..530bd920 100644
>> --- a/contrib/fsstress.c
>> +++ b/contrib/fsstress.c
>> @@ -599,7 +599,7 @@ void add_to_flist(int ft, int id, int parent)
>> void append_pathname(pathname_t * name, char *str)
>> {
>> 	int len;
>> -
>> +	char *path;:
>> 	len = strlen(str);
>> #ifdef DEBUG
>> 	if (len && *str == '/' && name->len == 0) {
>> @@ -609,7 +609,13 @@ void append_pathname(pathname_t * name, char *str)
>>
>> 	}
>> #endif
>> -	name->path = realloc(name->path, name->len + 1 + len);
>> +	path = realloc(name->path, name->len + 1 + len);
>> +	if (path == NULL) {
>> +		fprintf(stderr, "fsstress: append_pathname realloc failed\n");
>> +		chadir(homedir);
> 
> Did you mean chdir() here?
> 
>> +		abort();
>> +	}
>> +	name->path = path;
>> 	strcpy(&name->path[name->len], str);
>> 	name->len += len;
>> }
>> -- 
> 
> 
> Thanks,
> Best regards,
> Artem Blagodarenko.
> 
> .
>
diff mbox series

Patch

diff --git a/contrib/fsstress.c b/contrib/fsstress.c
index 2a983482..530bd920 100644
--- a/contrib/fsstress.c
+++ b/contrib/fsstress.c
@@ -599,7 +599,7 @@  void add_to_flist(int ft, int id, int parent)
 void append_pathname(pathname_t * name, char *str)
 {
 	int len;
-
+	char *path;
 	len = strlen(str);
 #ifdef DEBUG
 	if (len && *str == '/' && name->len == 0) {
@@ -609,7 +609,13 @@  void append_pathname(pathname_t * name, char *str)

 	}
 #endif
-	name->path = realloc(name->path, name->len + 1 + len);
+	path = realloc(name->path, name->len + 1 + len);
+	if (path == NULL) {
+		fprintf(stderr, "fsstress: append_pathname realloc failed\n");
+		chadir(homedir);
+		abort();
+	}
+	name->path = path;
 	strcpy(&name->path[name->len], str);
 	name->len += len;
 }