Skip to contentSkip to conversation

ip: check return value of iproute_flush_cache() in irpoute.c

PATCH In review Anton Moryakov 2 participants 5 messages Feb 17, 2025 – Feb 19, 2025

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

PATCH 1/1 ip: check return value of iproute_flush_cache() in irpoute.c

Static analyzer reported:
Return value of function 'iproute_flush_cache', called at iproute.c:1732,
is not checked. The return value is obtained from function 'open64' and possibly contains an error code.

Corrections explained:
The function iproute_flush_cache() may return an error code, which was previously ignored. This could lead to unexpected behavior if the cache flush fails. Added error handling to ensure the function fails gracefully when iproute_flush_cache() returns an error.

Triggers found by static analyzer Svace.

---
 ip/iproute.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
ip/iproute.c +4 -1
index e1fe26ce..64e7d77e 100644
@@ -1729,7 +1729,10 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)
 
 	if (filter.cloned) {
 		if (family != AF_INET6) {
-			iproute_flush_cache();
+			ret = iproute_flush_cache();
+			if(ret < 0)
+				return ret;
+				
 			if (show_stats)
 				printf("*** IPv4 routing cache is flushed.\n");
 		}
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/1 ip: handle NULL return from localtime in strxf_time in

Static analyzer reported:
Pointer 'tp', returned from function 'localtime' at ipxfrm.c:352, may be NULL and is dereferenced at ipxfrm.c:354 by calling function 'strftime'.

Corrections explained:
The function localtime() may return NULL if the provided time value is invalid. This commit adds a check for NULL and handles the error case by copying "invalid-time" into the output buffer.
Unlikely, but may return an error

Triggers found by static analyzer Svace.

---
 ip/ipxfrm.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
ip/ipxfrm.c +6 -1
index 90d25aac..9bfd96ab 100644
@@ -351,7 +351,12 @@ static const char *strxf_time(__u64 time)
 		t = (long)time;
 		tp = localtime(&t);
 
-		strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
+		if (!tp) {
+			/* Handle error case */
+			strcpy(str, "invalid-time");
+		} else {
+			strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
+		}
 	}
 
 	return str;
Signature · 1 line
2.30.2
#2 lore raw

PATCH 1/1 ip: remove duplicate condition in ila_csum_name2mode in

In reply to Anton Moryakov

Static analyzer reported:
expression is identical to previous conditio

Corrections explained:
The condition checking for "neutral-map-auto" was duplicated in the ila_csum_name2mode function. This commit removes the redundant check to improve code readability and maintainability.

Triggers found by static analyzer Svace.

---
 ip/ila_common.h | 2 --
 1 file changed, 2 deletions(-)
ip/ila_common.h +0 -2
index f99c2672..cd3524d5 100644
@@ -31,8 +31,6 @@ static inline int ila_csum_name2mode(char *name)
 		return ILA_CSUM_NEUTRAL_MAP_AUTO;
 	else if (strcmp(name, "no-action") == 0)
 		return ILA_CSUM_NO_ACTION;
-	else if (strcmp(name, "neutral-map-auto") == 0)
-		return ILA_CSUM_NEUTRAL_MAP_AUTO;
 	else
 		return -1;
 }
Signature · 1 line
2.30.2
#3 lore raw

PATCH 1/1 lib: remove redundant checks in get_u64 and get_s64

In reply to Anton Moryakov

Static analyzer reported:
1. if (res > 0xFFFFFFFFFFFFFFFFULL)
Expression 'res > 0xFFFFFFFFFFFFFFFFULL' is always false , which may be caused by a logical error:
'res' has a type 'unsigned long long' with minimum value '0' and a maximum value '18446744073709551615'

2. if (res > INT64_MAX || res < INT64_MIN)
Expression 'res > INT64_MAX' is always false , which may be caused by a logical error: 'res' has a type 'long long' with minimum value '-9223372036854775808' and a maximum value '9223372036854775807'
Expression 'res < INT64_MIN' is always false , which may be caused by a logical error: 'res' has a type 'long long' with minimum value '-9223372036854775808' and a maximum value '9223372036854775807'

Corrections explained:
- Removed redundant check res > 0xFFFFFFFFFFFFFFFFULL in get_u64, as res cannot exceed this value due to its type.
- Removed redundant checks res > INT64_MAX and res < INT64_MIN in get_s64, as res cannot exceed the range of long long.

Triggers found by static analyzer Svace.

---
 lib/utils.c | 6 ------
 1 file changed, 6 deletions(-)
lib/utils.c +0 -6
index be2ce0fe..706e93c3 100644
@@ -304,10 +304,6 @@ int get_u64(__u64 *val, const char *arg, int base)
 	if (res == ULLONG_MAX && errno == ERANGE)
 		return -1;
 
-	/* in case ULL is 128 bits */
-	if (res > 0xFFFFFFFFFFFFFFFFULL)
-		return -1;
-
 	*val = res;
 	return 0;
 }
@@ -399,8 +395,6 @@ int get_s64(__s64 *val, const char *arg, int base)
 		return -1;
 	if ((res == LLONG_MIN || res == LLONG_MAX) && errno == ERANGE)
 		return -1;
-	if (res > INT64_MAX || res < INT64_MIN)
-		return -1;
 
 	*val = res;
 	return 0;
Signature · 1 line
2.30.2
#4 lore raw
In reply to Anton Moryakov
Quoted 23 lines · Anton Moryakov

Static analyzer reported:
Return value of function 'iproute_flush_cache', called at iproute.c:1732,
is not checked. The return value is obtained from function 'open64' and possibly contains an error code.

Corrections explained:
The function iproute_flush_cache() may return an error code, which was previously ignored. This could lead to unexpected behavior if the cache flush fails. Added error handling to ensure the function fails gracefully when iproute_flush_cache() returns an error.

Triggers found by static analyzer Svace.

---
 ip/iproute.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
ip/iproute.c +0 -0
index e1fe26ce..64e7d77e 100644
@@ -1729,7 +1729,10 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)
 

ip/iproute.c static int iproute_flush(int family, rtnl_filter_t filter_fn) · line 1738

Quoted 10 lines
ip/iproute.c +4 -1
 	if (filter.cloned) {
 		if (family != AF_INET6) {
-			iproute_flush_cache();
+			ret = iproute_flush_cache();
+			if(ret < 0)
+				return ret;
+				
 			if (show_stats)
 				printf("*** IPv4 routing cache is flushed.\n");
 		}

applied all 4 after fixups on the first 2. This patch has style errors (extra tabs on the newline, space between if(.

patch 2 did not need brackets and the comment was not useful information.