diff mbox series

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

Message ID 20180218124610.32692-7-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, 12:46 p.m. UTC
Add null pointer check to allocation of url filename

Signed-off-by: Tobias Schramm <tobleminer@gmail.com>
---
 uclient-utils.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Comments

Philip Prindeville Feb. 18, 2018, 6:42 p.m. UTC | #1
> On Feb 18, 2018, at 5:46 AM, Tobias Schramm <tobleminer@gmail.com> wrote:
> 
> Add null pointer check to allocation of url filename
> 
> Signed-off-by: Tobias Schramm <tobleminer@gmail.com>
> ---
> uclient-utils.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/uclient-utils.c b/uclient-utils.c
> index a375eea..9c4dd42 100644
> --- a/uclient-utils.c
> +++ b/uclient-utils.c
> @@ -177,8 +177,13 @@ 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);

fname = strndup(str, len);

return fname;

-Philip


> 
> +        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..9c4dd42 100644
--- a/uclient-utils.c
+++ b/uclient-utils.c
@@ -177,8 +177,13 @@  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);
 }