Skip to contentSkip to conversation

ifstat: Add NULL pointer check for result of ll_index_to_name()

PATCH In review Maks Mishin 2 participants 2 messages Jan 6, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ifstat: Add NULL pointer check for result of ll_index_to_name()

Result of ll_index_to_name() function do not being checked for NULL pointer before using in strdup() function.
Add intermediate variable name for result of ll_index_to_name() function. Function result -1 when name is NULL.

---
 misc/ifstat.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
misc/ifstat.c +6 -1
index f6f9ba50..e6c38812 100644
@@ -129,7 +129,12 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
 		abort();
 
 	n->ifindex = ifsm->ifindex;
-	n->name = strdup(ll_index_to_name(ifsm->ifindex));
+	
+	const char* name = ll_index_to_name(ifsm->ifindex);
+	if (name == NULL) {
+		return -1;
+	}
+	n->name = strdup(name);
 
 	if (sub_type == NO_SUB_TYPE) {
 		memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));
Signature · 1 line
2.34.1
#1 lore raw
Quoted 18 lines · On Sat, 6 Jan 2024 23:10:10 +0300 Maks Mishin <[email protected]> wrote:

Result of ll_index_to_name() function do not being checked for NULL pointer before using in strdup() function.
Add intermediate variable name for result of ll_index_to_name() function. Function result -1 when name is NULL.

---
 misc/ifstat.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
misc/ifstat.c +0 -0
index f6f9ba50..e6c38812 100644
@@ -129,7 +129,12 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
 		abort();
 
 	n->ifindex = ifsm->ifindex;

misc/ifstat.c static int get_nlmsg_extended(struct nlmsghdr *m, void *arg) · line 140

Quoted 10 lines
misc/ifstat.c +6 -1
-	n->name = strdup(ll_index_to_name(ifsm->ifindex));
+	
+	const char* name = ll_index_to_name(ifsm->ifindex);
+	if (name == NULL) {
+		return -1;
+	}
+	n->name = strdup(name);
 
 	if (sub_type == NO_SUB_TYPE) {
 		memcpy(&n->val, RTA_DATA(tb[filter_type]), sizeof(n->val));

iproute follows kernel coding style.
checkpatch complains about this patch.

Also, there are several more strdup() calls in ifstat.

### [PATCH] ifstat: Add NULL pointer check for result of ll_index_to_name()

ERROR:TRAILING_WHITESPACE: trailing whitespace
#105: FILE: misc/ifstat.c:132:
+^I$

ERROR:POINTER_LOCATION: "foo* bar" should be "foo *bar" #106: FILE: misc/ifstat.c:133:
+ const char* name = ll_index_to_name(ifsm->ifindex);

WARNING:BRACES: braces {} are not necessary for single statement blocks #107: FILE: misc/ifstat.c:134:
+ if (name == NULL) {
+ return -1;
+ }

total: 2 errors, 1 warnings, 13 lines checked