diff options
| author | Ron Yorston <rmy@pobox.com> | 2021-09-18 09:33:21 +0100 |
|---|---|---|
| committer | Ron Yorston <rmy@pobox.com> | 2021-09-18 09:33:21 +0100 |
| commit | 48ddce5e9a063d89689ffe4be1680767186e13ee (patch) | |
| tree | d5b0a4dc2b988c5802620e6a48afca01427aaf5c | |
| parent | 5f652529e4e62e9b98ccded179e773c5ae2fac55 (diff) | |
| download | busybox-w32-48ddce5e9a063d89689ffe4be1680767186e13ee.tar.gz busybox-w32-48ddce5e9a063d89689ffe4be1680767186e13ee.tar.bz2 busybox-w32-48ddce5e9a063d89689ffe4be1680767186e13ee.zip | |
win32: limited version of timegm(3)
timegm(3) from musl checks that the calculated time_t value can
be broken out into a struct tm without overflow. The limiting
factor is that tm_year is an int.
Our only use of timegm(3) is followed by a call to localtime(3).
The Microsoft version of this only works for time_t values between
0 and INT_MAX (32-bit) or 0 and 32535215999 (64-bit).
Enforce these limits in timegm(3), thus avoiding the use of musl's
__secs_to_tm() and saving 624 bytes.
| -rw-r--r-- | win32/timegm.c | 97 |
1 files changed, 13 insertions, 84 deletions
diff --git a/win32/timegm.c b/win32/timegm.c index abba2579f..8bfa041c4 100644 --- a/win32/timegm.c +++ b/win32/timegm.c | |||
| @@ -111,95 +111,24 @@ static long long __tm_to_secs(const struct tm *tm) | |||
| 111 | return t; | 111 | return t; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | /* 2000-03-01 (mod 400 year, immediately after feb29 */ | 114 | /* |
| 115 | #define LEAPOCH (946684800LL + 86400*(31+29)) | 115 | * Restricted version of timegm: |
| 116 | 116 | * | |
| 117 | #define DAYS_PER_400Y (365*400 + 97) | 117 | * it doesn't normalise its argument |
| 118 | #define DAYS_PER_100Y (365*100 + 24) | 118 | * its return value is limited to the range Microsoft supports |
| 119 | #define DAYS_PER_4Y (365*4 + 1) | 119 | */ |
| 120 | |||
| 121 | static int __secs_to_tm(long long t, struct tm *tm) | ||
| 122 | { | ||
| 123 | long long days, secs, years; | ||
| 124 | int remdays, remsecs, remyears; | ||
| 125 | int qc_cycles, c_cycles, q_cycles; | ||
| 126 | int months; | ||
| 127 | int wday, yday, leap; | ||
| 128 | static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29}; | ||
| 129 | |||
| 130 | /* Reject time_t values whose year would overflow int */ | ||
| 131 | if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) | ||
| 132 | return -1; | ||
| 133 | |||
| 134 | secs = t - LEAPOCH; | ||
| 135 | days = secs / 86400; | ||
| 136 | remsecs = secs % 86400; | ||
| 137 | if (remsecs < 0) { | ||
| 138 | remsecs += 86400; | ||
| 139 | days--; | ||
| 140 | } | ||
| 141 | |||
| 142 | wday = (3+days)%7; | ||
| 143 | if (wday < 0) wday += 7; | ||
| 144 | |||
| 145 | qc_cycles = days / DAYS_PER_400Y; | ||
| 146 | remdays = days % DAYS_PER_400Y; | ||
| 147 | if (remdays < 0) { | ||
| 148 | remdays += DAYS_PER_400Y; | ||
| 149 | qc_cycles--; | ||
| 150 | } | ||
| 151 | |||
| 152 | c_cycles = remdays / DAYS_PER_100Y; | ||
| 153 | if (c_cycles == 4) c_cycles--; | ||
| 154 | remdays -= c_cycles * DAYS_PER_100Y; | ||
| 155 | |||
| 156 | q_cycles = remdays / DAYS_PER_4Y; | ||
| 157 | if (q_cycles == 25) q_cycles--; | ||
| 158 | remdays -= q_cycles * DAYS_PER_4Y; | ||
| 159 | |||
| 160 | remyears = remdays / 365; | ||
| 161 | if (remyears == 4) remyears--; | ||
| 162 | remdays -= remyears * 365; | ||
| 163 | |||
| 164 | leap = !remyears && (q_cycles || !c_cycles); | ||
| 165 | yday = remdays + 31 + 28 + leap; | ||
| 166 | if (yday >= 365+leap) yday -= 365+leap; | ||
| 167 | |||
| 168 | years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles; | ||
| 169 | |||
| 170 | for (months=0; days_in_month[months] <= remdays; months++) | ||
| 171 | remdays -= days_in_month[months]; | ||
| 172 | |||
| 173 | if (months >= 10) { | ||
| 174 | months -= 12; | ||
| 175 | years++; | ||
| 176 | } | ||
| 177 | |||
| 178 | if (years+100 > INT_MAX || years+100 < INT_MIN) | ||
| 179 | return -1; | ||
| 180 | |||
| 181 | tm->tm_year = years + 100; | ||
| 182 | tm->tm_mon = months + 2; | ||
| 183 | tm->tm_mday = remdays + 1; | ||
| 184 | tm->tm_wday = wday; | ||
| 185 | tm->tm_yday = yday; | ||
| 186 | |||
| 187 | tm->tm_hour = remsecs / 3600; | ||
| 188 | tm->tm_min = remsecs / 60 % 60; | ||
| 189 | tm->tm_sec = remsecs % 60; | ||
| 190 | |||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | |||
| 194 | time_t timegm(struct tm *tm) | 120 | time_t timegm(struct tm *tm) |
| 195 | { | 121 | { |
| 196 | struct tm new; | ||
| 197 | long long t = __tm_to_secs(tm); | 122 | long long t = __tm_to_secs(tm); |
| 198 | if (__secs_to_tm(t, &new) < 0) { | 123 | if (t < 0 || |
| 124 | #ifdef _USE_32BIT_TIME_T | ||
| 125 | t > INT_MAX /* 2038-01-19 03:14:07Z */ | ||
| 126 | #else | ||
| 127 | t > 32535215999 /* 3000-12-31 23:59:59Z */ | ||
| 128 | #endif | ||
| 129 | ) { | ||
| 199 | errno = EOVERFLOW; | 130 | errno = EOVERFLOW; |
| 200 | return -1; | 131 | return -1; |
| 201 | } | 132 | } |
| 202 | *tm = new; | ||
| 203 | tm->tm_isdst = 0; | ||
| 204 | return t; | 133 | return t; |
| 205 | } | 134 | } |
