Skip to contentSkip to conversation

ip: handle NULL return from localtime in strxf_time in

PATCH In review Anton Moryakov 2 participants 2 messages Feb 16, 2025

Subsystems (2)
lore
Share & tools
lore
#0 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
#1 lore raw
Quoted 10 lines · On Sun, 16 Feb 2025 05:25:23 +0300 Anton Moryakov <[email protected]>…

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

Quoted 3 lines

Triggers found by static analyzer Svace.

Seems like you are creating dead code. Unless glibc is broken this can never happen.