diff mbox series

[OpenWrt-Devel,5/6] uclient-utils: Handle memory allocation failure for url file name

Message ID 20180218033640.17715-6-tobleminer@gmail.com
State Changes Requested
Delegated to: John Crispin
Headers show
Series uclient: Handle memory allocation failures | expand

Commit Message

Tobias Schramm Feb. 18, 2018, 3:36 a.m. UTC
Signed-off-by: Tobias Schramm <tobleminer@gmail.com>
---
 uclient-utils.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

John Crispin Feb. 18, 2018, 10:19 a.m. UTC | #1
On 18/02/18 04:36, Tobias Schramm wrote:
> Signed-off-by: Tobias Schramm <tobleminer@gmail.com>
> ---
>   uclient-utils.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/uclient-utils.c b/uclient-utils.c
> index a375eea..ee0f8e8 100644
> --- a/uclient-utils.c
> +++ b/uclient-utils.c
> @@ -177,8 +177,12 @@ char *uclient_get_url_filename(const char *url, const char *default_name)
>   	str++;
>   	len -= str - url;
>   
> -	if (len > 0)
> -		return strncpy(calloc(1, len + 1), str, len);
> +	if (len > 0) {
> +		char *fname = calloc(1, len + 1);
missing blank line  ...

     John

> +		if(!fname)
> +			return NULL;
>   
> +		return strncpy(fname, str, len);
> +	}
>   	return strdup(default_name);
>   }
Tobias Schramm Feb. 18, 2018, 9:51 p.m. UTC | #2
Yep Philip, you are right. That's a much nicer solution.
I'll wait for some more feedback and do just that in v3.

2018-02-18 19:35 GMT+01:00 Philip Prindeville
<monster_philip@redfish-solutions.com>:
>
>
>> On Feb 17, 2018, at 8:36 PM, Tobias Schramm <tobleminer@gmail.com> wrote:
>>
>> Signed-off-by: Tobias Schramm <tobleminer@gmail.com>
>> ---
>> uclient-utils.c | 8 ++++++--
>> 1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/uclient-utils.c b/uclient-utils.c
>> index a375eea..ee0f8e8 100644
>> --- a/uclient-utils.c
>> +++ b/uclient-utils.c
>> @@ -177,8 +177,12 @@ char *uclient_get_url_filename(const char *url, const char *default_name)
>>    str++;
>>    len -= str - url;
>>
>> -    if (len > 0)
>> -        return strncpy(calloc(1, len + 1), str, len);
>> +    if (len > 0) {
>
>
> Why not just use strndup() here instead?
>
> -Philip
>
>
>> +        char *fname = calloc(1, len + 1);
>> +        if(!fname)
>> +            return NULL;
>>
>> +        return strncpy(fname, str, len);
>> +    }
>>    return strdup(default_name);
>> }
>> --
>> 2.16.1
>> _______________________________________________
>> openwrt-devel mailing list
>> openwrt-devel@lists.openwrt.org
>> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
>
diff mbox series

Patch

diff --git a/uclient-utils.c b/uclient-utils.c
index a375eea..ee0f8e8 100644
--- a/uclient-utils.c
+++ b/uclient-utils.c
@@ -177,8 +177,12 @@  char *uclient_get_url_filename(const char *url, const char *default_name)
 	str++;
 	len -= str - url;
 
-	if (len > 0)
-		return strncpy(calloc(1, len + 1), str, len);
+	if (len > 0) {
+		char *fname = calloc(1, len + 1);
+		if(!fname)
+			return NULL;
 
+		return strncpy(fname, str, len);
+	}
 	return strdup(default_name);
 }