]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip: handle NULL return from localtime in strxf_time in
authorAnton Moryakov <ant.v.moryakov@gmail.com>
Mon, 17 Feb 2025 16:21:51 +0000 (19:21 +0300)
committerDavid Ahern <dsahern@kernel.org>
Wed, 19 Feb 2025 15:46:15 +0000 (15:46 +0000)
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 <ant.v.moryakov@gmail.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
ip/ipxfrm.c

index 90d25aac08b8363065c076f3ddf9b9f4538fa81e..b15cc0002e5bdf7ac01a1b7c3949da84db3a1df6 100644 (file)
@@ -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;