diff mbox series

[iproute2] ifstat: handle strdup return value

Message ID 20240314122040.4644-1-dkirjanov@suse.de (mailing list archive)
State Superseded
Delegated to: David Ahern
Headers show
Series [iproute2] ifstat: handle strdup return value | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Denis Kirjanov March 14, 2024, 12:20 p.m. UTC
get_nlmsg_extended missing the check as it's done
in get_nlmsg

Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
---
 misc/ifstat.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Ratheesh Kannoth March 15, 2024, 2:23 a.m. UTC | #1
On 2024-03-14 at 17:50:40, Denis Kirjanov (kirjanov@gmail.com) wrote:
> get_nlmsg_extended missing the check as it's done
> in get_nlmsg
>
> Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
> ---
>  misc/ifstat.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/misc/ifstat.c b/misc/ifstat.c
> index 685e66c9..f94b11bc 100644
> --- a/misc/ifstat.c
> +++ b/misc/ifstat.c
> @@ -140,6 +140,11 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
>
>  	n->ifindex = ifsm->ifindex;
>  	n->name = strdup(ll_index_to_name(ifsm->ifindex));
> +	if (!n->name) {
> +		free(n);
> +		errno = ENOMEM;
strdup() will set the errno right ? why do you need to set it explicitly ?
> +		return -1;
> +	}
>
>  	if (sub_type == NO_SUB_TYPE) {
>  		memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));
> --
> 2.30.2
>
David Ahern March 15, 2024, 2:34 a.m. UTC | #2
On 3/14/24 8:23 PM, Ratheesh Kannoth wrote:
>> diff --git a/misc/ifstat.c b/misc/ifstat.c
>> index 685e66c9..f94b11bc 100644
>> --- a/misc/ifstat.c
>> +++ b/misc/ifstat.c
>> @@ -140,6 +140,11 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
>>
>>  	n->ifindex = ifsm->ifindex;
>>  	n->name = strdup(ll_index_to_name(ifsm->ifindex));
>> +	if (!n->name) {
>> +		free(n);
>> +		errno = ENOMEM;
> strdup() will set the errno right ? why do you need to set it explicitly ?

agreed.

pw-bot: cr
Stephen Hemminger March 15, 2024, 4:46 a.m. UTC | #3
On Fri, 15 Mar 2024 07:53:29 +0530
Ratheesh Kannoth <rkannoth@marvell.com> wrote:

> > diff --git a/misc/ifstat.c b/misc/ifstat.c
> > index 685e66c9..f94b11bc 100644
> > --- a/misc/ifstat.c
> > +++ b/misc/ifstat.c
> > @@ -140,6 +140,11 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
> >
> >  	n->ifindex = ifsm->ifindex;
> >  	n->name = strdup(ll_index_to_name(ifsm->ifindex));
> > +	if (!n->name) {
> > +		free(n);
> > +		errno = ENOMEM;  
> strdup() will set the errno right ? why do you need to set it explicitly ?
> > +		return -1;

Man page for strdup says:

RETURN VALUE
       On success, the strdup() function returns a pointer to  the  duplicated
       string.  It returns NULL if insufficient memory was available, with er‐
       rno set to indicate the error.
diff mbox series

Patch

diff --git a/misc/ifstat.c b/misc/ifstat.c
index 685e66c9..f94b11bc 100644
--- a/misc/ifstat.c
+++ b/misc/ifstat.c
@@ -140,6 +140,11 @@  static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
 
 	n->ifindex = ifsm->ifindex;
 	n->name = strdup(ll_index_to_name(ifsm->ifindex));
+	if (!n->name) {
+		free(n);
+		errno = ENOMEM;
+		return -1;
+	}
 
 	if (sub_type == NO_SUB_TYPE) {
 		memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));