ip: handle NULL return from localtime in strxf_time in
Subsystems (2)
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
@@ -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
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 functionlocaltime()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.
Signed-off-by Anton Moryakov <[email protected]>
Seems like you are creating dead code. Unless glibc is broken this can never happen.