Skip to content
Myr

util.c: fix _WIN32 port of strptime (#3071)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In windows, time_t is a signed 32-bit integer type, so TIME_MAX needs to be declared as INT32_MAX instead of INT64_MAX. Also bump NetBSD's strptime to revision 1.65 from 1.63 to fix undefined behaviour (signed integer overflow) bugs. Related NetBSD problem report: https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=58041 Noticed thanks to a compiler warning in the windows build CI. I declared the d variable as time_t instead of as unsigned to ensure that the signedness/size of TIME_MAX-d in the sse > TIME_MAX-d check is always correct, and to prevent -Wsign-compare warnings from triggering in the windows build.

authored by Emanuele Torre · committed by GitHub · parent 1411ce6 · Browse files · 1 file+31 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−24 removed

  • other:+31 added−24 removed

1 file+31 added−24 removed

modified:src/util.c+31 added-24 removed
816 unmodified lines
817 bp = conv_num(bp, &tm->tm_sec, 0, 61);818 LEGAL_ALT(ALT_O);819 continue;820821#ifndef TIME_MAX822#define TIME_MAX INT64_MAX821 case 's': { /* seconds since the epoch */822#ifdef _WIN32823 const time_t TIME_MAX = INT32_MAX;824#else825 const time_t TIME_MAX = INT64_MAX;826#endif824 case 's': /* seconds since the epoch */825 {826 time_t sse = 0;827 uint64_t rulim = TIME_MAX;827 time_t sse;828 time_t d;829829 if (*bp < '0' || *bp > '9') {830 if (*bp < '0' || *bp > '9') {831 bp = NULL;832 continue;833 }834835 sse = *bp++ - '0';836 while (*bp >= '0' && *bp <= '9') {837 d = *bp++ - '0';838 if (sse > TIME_MAX/10) {839 bp = NULL;831 continue;840 break;841 }833834 do {835 sse *= 10;836 sse += *bp++ - '0';837 rulim /= 10;838 } while ((sse * 10 <= TIME_MAX) &&839 rulim && *bp >= '0' && *bp <= '9');840841 if (sse < 0 || (uint64_t)sse > TIME_MAX) {842 sse *= 10;843 if (sse > TIME_MAX - d) {844 bp = NULL;843 continue;845 break;846 }847 sse += d;848 }849 if (bp == NULL)850 continue;851852#ifdef _WIN32846 if (localtime_s(tm, &sse) == 0)853 if (localtime_s(tm, &sse))854#else848 if (localtime_r(&sse, tm))855 if (localtime_r(&sse, tm) == NULL)856#endif850 state |= S_YDAY | S_WDAY | S_MON | S_MDAY | S_YEAR;851 else852 bp = NULL;853 }857 bp = NULL;858 else859 state |= S_YDAY | S_WDAY | S_MON | S_MDAY | S_YEAR;860 continue;861 }862863 case 'U': /* The week of year, beginning on sunday. */864 case 'W': /* The week of year, beginning on monday. */865 /*
400 unmodified lines