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 <ant.v.moryakov@gmail.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
This commit is contained in:
Anton Moryakov
2025-02-17 19:21:51 +03:00
committed by David Ahern
parent 84da3f37e2
commit 55446264e6

View 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;