mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] Handle localtime errors in ctime
@ 2017-06-15 16:43 Omer Anson
  2017-06-15 16:54 ` Rich Felker
  0 siblings, 1 reply; 11+ messages in thread
From: Omer Anson @ 2017-06-15 16:43 UTC (permalink / raw)
  To: musl; +Cc: Omer Anson

ctime passes the result from localtime directly to asctime. But in case
of error, localtime returns 0. This causes an error (NULL pointer
dereference) in asctime.

According to the man pages [1], ctime should also return 0 upon error.
Therefore, if localtime fails (return 0), ctime also returns 0.

[1] https://linux.die.net/man/3/ctime
---
 src/time/ctime.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/time/ctime.c b/src/time/ctime.c
index 185ec55..6955f2d 100644
--- a/src/time/ctime.c
+++ b/src/time/ctime.c
@@ -2,5 +2,9 @@
 
 char *ctime(const time_t *t)
 {
-	return asctime(localtime(t));
+	struct tm * tm = localtime(t);
+	if (!tm) {
+		return 0;
+	}
+	return asctime(tm);
 }
-- 
2.4.11



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

end of thread, other threads:[~2017-06-21 14:47 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-15 16:43 [PATCH] Handle localtime errors in ctime Omer Anson
2017-06-15 16:54 ` Rich Felker
2017-06-15 17:03   ` Alexander Monakov
2017-06-15 17:06     ` Rich Felker
2017-06-15 17:19       ` Alexander Monakov
2017-06-15 17:26         ` Rich Felker
2017-06-15 17:31           ` Rich Felker
2017-06-15 17:46             ` Alexander Monakov
2017-06-21  0:34               ` Rich Felker
2017-06-21  7:18                 ` Alexander Monakov
2017-06-21 14:47                   ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).