diff mbox series

[v2,11/12] misc/lsattr: check whether path is NULL in lsattr_dir_proc()

Message ID 20210630082724.50838-12-wuguanghao3@huawei.com
State Changes Requested
Headers show
Series [v2,01/12] profile_create_node: set magic before strdup(name) to avoid memory leak | expand

Commit Message

wuguanghao June 30, 2021, 8:27 a.m. UTC
From: Zhiqiang Liu <liuzhiqiang26@huawei.com>

In lsattr_dir_proc(), if malloc() return NULL, it will cause
a segmentation fault problem.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
---
 misc/lsattr.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Theodore Ts'o July 16, 2021, 3:58 a.m. UTC | #1
On Wed, Jun 30, 2021 at 04:27:23PM +0800, wuguanghao wrote:
> 
> diff --git a/misc/lsattr.c b/misc/lsattr.c
> index 0d954376..f3212069 100644
> --- a/misc/lsattr.c
> +++ b/misc/lsattr.c
> @@ -144,6 +144,12 @@ static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
>  	int dir_len = strlen(dir_name);
>  
>  	path = malloc(dir_len + strlen (de->d_name) + 2);
> +	if (!path) {
> +		fprintf(stderr, "%s",
> +			_("Couldn't allocate path variable "
> +			  "in lsattr_dir_proc"));
> +		return -1;
> +	}

The string is missing a closing newline.  Also, why not?

		fputs(_("Couldn't allocate path variable in lsattr_dir_proc"),
		      stderr);

					- Ted
Zhiqiang Liu July 17, 2021, 1:39 a.m. UTC | #2
On 2021/7/16 11:58, Theodore Y. Ts'o wrote:
> On Wed, Jun 30, 2021 at 04:27:23PM +0800, wuguanghao wrote:
>> diff --git a/misc/lsattr.c b/misc/lsattr.c
>> index 0d954376..f3212069 100644
>> --- a/misc/lsattr.c
>> +++ b/misc/lsattr.c
>> @@ -144,6 +144,12 @@ static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
>>  	int dir_len = strlen(dir_name);
>>  
>>  	path = malloc(dir_len + strlen (de->d_name) + 2);
>> +	if (!path) {
>> +		fprintf(stderr, "%s",
>> +			_("Couldn't allocate path variable "
>> +			  "in lsattr_dir_proc"));
>> +		return -1;
>> +	}
> The string is missing a closing newline.  Also, why not?
>
> 		fputs(_("Couldn't allocate path variable in lsattr_dir_proc"),
> 		      stderr);
>
> 					- Ted
>
> .

Thanks for your suggestion.

We will resend the v3 patch as your suggestion.
diff mbox series

Patch

diff --git a/misc/lsattr.c b/misc/lsattr.c
index 0d954376..f3212069 100644
--- a/misc/lsattr.c
+++ b/misc/lsattr.c
@@ -144,6 +144,12 @@  static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
 	int dir_len = strlen(dir_name);
 
 	path = malloc(dir_len + strlen (de->d_name) + 2);
+	if (!path) {
+		fprintf(stderr, "%s",
+			_("Couldn't allocate path variable "
+			  "in lsattr_dir_proc"));
+		return -1;
+	}
 
 	if (dir_len && dir_name[dir_len-1] == '/')
 		sprintf (path, "%s%s", dir_name, de->d_name);