From: Anton Moryakov Date: Mon, 17 Feb 2025 16:21:51 +0000 (+0300) Subject: ip: handle NULL return from localtime in strxf_time in X-Git-Tag: v6.15.0~6^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55446264e60622e3f1ee9d51f5acce5536a648bf;p=thirdparty%2Fiproute2.git 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. Signed-off-by: Anton Moryakov Signed-off-by: David Ahern --- diff --git a/ip/ipxfrm.c b/ip/ipxfrm.c index 90d25aac..b15cc000 100644 --- a/ip/ipxfrm.c +++ b/ip/ipxfrm.c @@ -351,7 +351,10 @@ static const char *strxf_time(__u64 time) t = (long)time; tp = localtime(&t); - strftime(str, sizeof(str), "%Y-%m-%d %T", tp); + if (!tp) + strcpy(str, "invalid-time"); + else + strftime(str, sizeof(str), "%Y-%m-%d %T", tp); } return str;