handle localtime errors in ctime
authorRich Felker <dalias@aerifal.cx>
Thu, 15 Jun 2017 16:58:08 +0000 (12:58 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 15 Jun 2017 16:58:08 +0000 (12:58 -0400)
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.

based on patch by Omer Anson.

src/time/ctime.c

index 185ec55..3602931 100644 (file)
@@ -2,5 +2,7 @@
 
 char *ctime(const time_t *t)
 {
-       return asctime(localtime(t));
+       struct tm *tm = localtime(t);
+       if (!tm) return 0;
+       return asctime(tm);
 }