getservbyport_r: fix wrong result if getnameinfo fails with EAI_OVERFLOW
[musl] / src / time / mktime.c
index 858cd50..bad3f07 100644 (file)
@@ -1,24 +1,28 @@
-#include <time.h>
-
-#include "__time.h"
+#include "time_impl.h"
+#include <errno.h>
 
 time_t mktime(struct tm *tm)
 {
-       int isdst = tm->tm_isdst;
-       time_t t, lt;
+       struct tm new;
+       long opp;
+       long long t = __tm_to_secs(tm);
+
+       __secs_to_zone(t, 1, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);
 
-       __tzset();
+       if (tm->tm_isdst>=0 && new.tm_isdst!=tm->tm_isdst)
+               t -= opp - new.__tm_gmtoff;
 
-       tm->tm_sec += __timezone;
-       if (isdst > 0) tm->tm_sec += __dst_offset;
+       t -= new.__tm_gmtoff;
+       if ((time_t)t != t) goto error;
 
-       t = __tm_to_time(tm);
-       
-       lt = t - __timezone;
-       if (isdst > 0) lt -= __dst_offset;
-       __time_to_tm(lt, tm);
+       __secs_to_zone(t, 0, &new.tm_isdst, &new.__tm_gmtoff, &opp, &new.__tm_zone);
 
-       __dst_adjust(tm);
-       
+       if (__secs_to_tm(t + new.__tm_gmtoff, &new) < 0) goto error;
+
+       *tm = new;
        return t;
+
+error:
+       errno = EOVERFLOW;
+       return -1;
 }