public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ip: handle NULL return from localtime in strxf_time in
@ 2025-02-16  2:25 Anton Moryakov
  2025-02-16  4:41 ` Stephen Hemminger
  0 siblings, 1 reply; 2+ messages in thread
From: Anton Moryakov @ 2025-02-16  2:25 UTC (permalink / raw)
  To: netdev; +Cc: Anton Moryakov

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>

---
 ip/ipxfrm.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/ip/ipxfrm.c b/ip/ipxfrm.c
index 90d25aac..9bfd96ab 100644
--- a/ip/ipxfrm.c
+++ b/ip/ipxfrm.c
@@ -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;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ip: handle NULL return from localtime in strxf_time in
  2025-02-16  2:25 [PATCH] ip: handle NULL return from localtime in strxf_time in Anton Moryakov
@ 2025-02-16  4:41 ` Stephen Hemminger
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Hemminger @ 2025-02-16  4:41 UTC (permalink / raw)
  To: Anton Moryakov; +Cc: netdev

On Sun, 16 Feb 2025 05:25:23 +0300
Anton Moryakov <ant.v.moryakov@gmail.com> wrote:

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

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-02-16  4:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-16  2:25 [PATCH] ip: handle NULL return from localtime in strxf_time in Anton Moryakov
2025-02-16  4:41 ` Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox