Merge pull request #20742 from taosdata/fix/TD-23392

fix: fix tsim crash on windows due to invalid input to strftime
This commit is contained in:
dapan1121 2023-04-04 19:48:49 +08:00 committed by GitHub
commit 1261f9a24f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 70 additions and 31 deletions

View File

@ -64,7 +64,7 @@ static FORCE_INLINE int64_t taosGetTimestampToday(int32_t precision) {
: 1000000000; : 1000000000;
time_t t = taosTime(NULL); time_t t = taosTime(NULL);
struct tm tm; struct tm tm;
taosLocalTime(&t, &tm); taosLocalTime(&t, &tm, NULL);
tm.tm_hour = 0; tm.tm_hour = 0;
tm.tm_min = 0; tm.tm_min = 0;
tm.tm_sec = 0; tm.tm_sec = 0;

View File

@ -91,7 +91,7 @@ static FORCE_INLINE int64_t taosGetMonoTimestampMs() {
} }
char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm); char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm);
struct tm *taosLocalTime(const time_t *timep, struct tm *result); struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf);
struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst); struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst);
time_t taosTime(time_t *t); time_t taosTime(time_t *t);
time_t taosMktime(struct tm *timep); time_t taosMktime(struct tm *timep);

View File

@ -1864,7 +1864,9 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) {
} }
} }
struct tm ptm = {0}; struct tm ptm = {0};
taosLocalTime(&tt, &ptm); if (taosLocalTime(&tt, &ptm, buf) == NULL) {
return buf;
}
size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
if (precision == TSDB_TIME_PRECISION_NANO) { if (precision == TSDB_TIME_PRECISION_NANO) {

View File

@ -727,7 +727,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) {
struct tm tm; struct tm tm;
time_t tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision)); time_t tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision));
taosLocalTime(&tt, &tm); taosLocalTime(&tt, &tm, NULL);
int32_t mon = tm.tm_year * 12 + tm.tm_mon + (int32_t)numOfMonth; int32_t mon = tm.tm_year * 12 + tm.tm_mon + (int32_t)numOfMonth;
tm.tm_year = mon / 12; tm.tm_year = mon / 12;
tm.tm_mon = mon % 12; tm.tm_mon = mon % 12;
@ -750,11 +750,11 @@ int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char
struct tm tm; struct tm tm;
time_t t = (time_t)skey; time_t t = (time_t)skey;
taosLocalTime(&t, &tm); taosLocalTime(&t, &tm, NULL);
int32_t smon = tm.tm_year * 12 + tm.tm_mon; int32_t smon = tm.tm_year * 12 + tm.tm_mon;
t = (time_t)ekey; t = (time_t)ekey;
taosLocalTime(&t, &tm); taosLocalTime(&t, &tm, NULL);
int32_t emon = tm.tm_year * 12 + tm.tm_mon; int32_t emon = tm.tm_year * 12 + tm.tm_mon;
if (unit == 'y') { if (unit == 'y') {
@ -774,7 +774,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio
start /= (int64_t)(TSDB_TICK_PER_SECOND(precision)); start /= (int64_t)(TSDB_TICK_PER_SECOND(precision));
struct tm tm; struct tm tm;
time_t tt = (time_t)start; time_t tt = (time_t)start;
taosLocalTime(&tt, &tm); taosLocalTime(&tt, &tm, NULL);
tm.tm_sec = 0; tm.tm_sec = 0;
tm.tm_min = 0; tm.tm_min = 0;
tm.tm_hour = 0; tm.tm_hour = 0;
@ -867,13 +867,17 @@ const char* fmtts(int64_t ts) {
if (ts > -62135625943 && ts < 32503651200) { if (ts > -62135625943 && ts < 32503651200) {
time_t t = (time_t)ts; time_t t = (time_t)ts;
taosLocalTime(&t, &tm); if (taosLocalTime(&t, &tm, buf) == NULL) {
return buf;
}
pos += strftime(buf + pos, sizeof(buf), "s=%Y-%m-%d %H:%M:%S", &tm); pos += strftime(buf + pos, sizeof(buf), "s=%Y-%m-%d %H:%M:%S", &tm);
} }
if (ts > -62135625943000 && ts < 32503651200000) { if (ts > -62135625943000 && ts < 32503651200000) {
time_t t = (time_t)(ts / 1000); time_t t = (time_t)(ts / 1000);
taosLocalTime(&t, &tm); if (taosLocalTime(&t, &tm, buf) == NULL) {
return buf;
}
if (pos > 0) { if (pos > 0) {
buf[pos++] = ' '; buf[pos++] = ' ';
buf[pos++] = '|'; buf[pos++] = '|';
@ -885,7 +889,9 @@ const char* fmtts(int64_t ts) {
{ {
time_t t = (time_t)(ts / 1000000); time_t t = (time_t)(ts / 1000000);
taosLocalTime(&t, &tm); if (taosLocalTime(&t, &tm, buf) == NULL) {
return buf;
}
if (pos > 0) { if (pos > 0) {
buf[pos++] = ' '; buf[pos++] = ' ';
buf[pos++] = '|'; buf[pos++] = '|';
@ -937,7 +943,9 @@ void taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precision)
ASSERT(false); ASSERT(false);
} }
taosLocalTime(&quot, &ptm); if (taosLocalTime(&quot, &ptm, buf) == NULL) {
return;
}
int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", &ptm); int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", &ptm);
length += snprintf(ts + length, fractionLen, format, mod); length += snprintf(ts + length, fractionLen, format, mod);
length += (int32_t)strftime(ts + length, 40 - length, "%z", &ptm); length += (int32_t)strftime(ts + length, 40 - length, "%z", &ptm);

View File

@ -88,7 +88,7 @@ static void getNextTimeWindow(SInterval* pInterval, STimeWindow* tw, int32_t ord
struct tm tm; struct tm tm;
time_t t = (time_t)key; time_t t = (time_t)key;
taosLocalTime(&t, &tm); taosLocalTime(&t, &tm, NULL);
int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor); int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor);
tm.tm_year = mon / 12; tm.tm_year = mon / 12;

View File

@ -281,7 +281,7 @@ static void getNextTimeWindow(SInterval* pInterval, int32_t precision, int32_t o
struct tm tm; struct tm tm;
time_t t = (time_t)key; time_t t = (time_t)key;
taosLocalTime(&t, &tm); taosLocalTime(&t, &tm, NULL);
int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor); int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor);
tm.tm_year = mon / 12; tm.tm_year = mon / 12;

View File

@ -213,8 +213,9 @@ static int32_t addTimezoneParam(SNodeList* pList) {
char buf[6] = {0}; char buf[6] = {0};
time_t t = taosTime(NULL); time_t t = taosTime(NULL);
struct tm tmInfo; struct tm tmInfo;
taosLocalTime(&t, &tmInfo); if (taosLocalTime(&t, &tmInfo, buf) != NULL) {
strftime(buf, sizeof(buf), "%z", &tmInfo); strftime(buf, sizeof(buf), "%z", &tmInfo);
}
int32_t len = (int32_t)strlen(buf); int32_t len = (int32_t)strlen(buf);
SValueNode* pVal = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE); SValueNode* pVal = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);

View File

@ -1067,9 +1067,15 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *
} }
struct tm tmInfo; struct tm tmInfo;
taosLocalTime((const time_t *)&timeVal, &tmInfo); int32_t len = 0;
if (taosLocalTime((const time_t *)&timeVal, &tmInfo, buf) == NULL) {
len = (int32_t)strlen(buf);
goto _end;
}
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tmInfo); strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tmInfo);
int32_t len = (int32_t)strlen(buf); len = (int32_t)strlen(buf);
// add timezone string // add timezone string
if (tzLen > 0) { if (tzLen > 0) {
@ -1103,6 +1109,7 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *
len += fracLen; len += fracLen;
} }
_end:
memmove(buf + VARSTR_HEADER_SIZE, buf, len); memmove(buf + VARSTR_HEADER_SIZE, buf, len);
varDataSetLen(buf, len); varDataSetLen(buf, len);

View File

@ -407,12 +407,21 @@ time_t taosMktime(struct tm *timep) {
#endif #endif
} }
struct tm *taosLocalTime(const time_t *timep, struct tm *result) { struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) {
struct tm *res = NULL;
if (result == NULL) { if (result == NULL) {
return localtime(timep); res = localtime(timep);
if (res == NULL && buf != NULL) {
sprintf(buf, "NaN");
}
return res;
} }
#ifdef WINDOWS #ifdef WINDOWS
if (*timep < 0) { if (*timep < 0) {
if (buf != NULL) {
sprintf(buf, "NaN");
}
return NULL; return NULL;
// TODO: bugs in following code // TODO: bugs in following code
SYSTEMTIME ss, s; SYSTEMTIME ss, s;
@ -421,6 +430,9 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result) {
struct tm tm1; struct tm tm1;
time_t tt = 0; time_t tt = 0;
if (localtime_s(&tm1, &tt) != 0 ) { if (localtime_s(&tm1, &tt) != 0 ) {
if (buf != NULL) {
sprintf(buf, "NaN");
}
return NULL; return NULL;
} }
ss.wYear = tm1.tm_year + 1900; ss.wYear = tm1.tm_year + 1900;
@ -449,11 +461,17 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result) {
result->tm_isdst = 0; result->tm_isdst = 0;
} else { } else {
if (localtime_s(result, timep) != 0) { if (localtime_s(result, timep) != 0) {
if (buf != NULL) {
sprintf(buf, "NaN");
}
return NULL; return NULL;
} }
} }
#else #else
localtime_r(timep, result); res = localtime_r(timep, result);
if (res == NULL && buf != NULL) {
sprintf(buf, "NaN");
}
#endif #endif
return result; return result;
} }

View File

@ -893,7 +893,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/ */
time_t tx1 = taosGetTimestampSec(); time_t tx1 = taosGetTimestampSec();
struct tm tm1; struct tm tm1;
taosLocalTime(&tx1, &tm1); taosLocalTime(&tx1, &tm1, NULL);
daylight = tm1.tm_isdst; daylight = tm1.tm_isdst;
/* /*
@ -921,7 +921,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/ */
time_t tx1 = taosGetTimestampSec(); time_t tx1 = taosGetTimestampSec();
struct tm tm1; struct tm tm1;
taosLocalTime(&tx1, &tm1); taosLocalTime(&tx1, &tm1, NULL);
/* load time zone string from /etc/timezone */ /* load time zone string from /etc/timezone */
// FILE *f = fopen("/etc/timezone", "r"); // FILE *f = fopen("/etc/timezone", "r");
errno = 0; errno = 0;
@ -1008,7 +1008,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/ */
time_t tx1 = taosGetTimestampSec(); time_t tx1 = taosGetTimestampSec();
struct tm tm1; struct tm tm1;
taosLocalTime(&tx1, &tm1); taosLocalTime(&tx1, &tm1, NULL);
/* /*
* format example: * format example:

View File

@ -121,7 +121,7 @@ static FORCE_INLINE void taosUpdateDaylight() {
struct timeval timeSecs; struct timeval timeSecs;
taosGetTimeOfDay(&timeSecs); taosGetTimeOfDay(&timeSecs);
time_t curTime = timeSecs.tv_sec; time_t curTime = timeSecs.tv_sec;
ptm = taosLocalTime(&curTime, &Tm); ptm = taosLocalTime(&curTime, &Tm, NULL);
tsDaylightActive = ptm->tm_isdst; tsDaylightActive = ptm->tm_isdst;
} }
static FORCE_INLINE int32_t taosGetDaylight() { return tsDaylightActive; } static FORCE_INLINE int32_t taosGetDaylight() { return tsDaylightActive; }
@ -437,7 +437,7 @@ static inline int32_t taosBuildLogHead(char *buffer, const char *flags) {
taosGetTimeOfDay(&timeSecs); taosGetTimeOfDay(&timeSecs);
time_t curTime = timeSecs.tv_sec; time_t curTime = timeSecs.tv_sec;
ptm = taosLocalTime(&curTime, &Tm); ptm = taosLocalTime(&curTime, &Tm, NULL);
return sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " %s", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, return sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " %s", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour,
ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId(), flags); ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId(), flags);

View File

@ -291,8 +291,7 @@ char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) {
} }
struct tm ptm = {0}; struct tm ptm = {0};
if (taosLocalTime(&tt, &ptm) == NULL) { if (taosLocalTime(&tt, &ptm, buf) == NULL) {
sprintf(buf, "NaN");
return buf; return buf;
} }
size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);

View File

@ -597,7 +597,7 @@ void printParaIntoFile() {
time_t tTime = taosGetTimestampSec(); time_t tTime = taosGetTimestampSec();
struct tm tm; struct tm tm;
taosLocalTime(&tTime, &tm); taosLocalTime(&tTime, &tm, NULL);
taosFprintfFile(pFile, "###################################################################\n"); taosFprintfFile(pFile, "###################################################################\n");
taosFprintfFile(pFile, "# configDir: %s\n", configDir); taosFprintfFile(pFile, "# configDir: %s\n", configDir);

View File

@ -166,7 +166,7 @@ static void printHelp() {
char* getCurrentTimeString(char* timeString) { char* getCurrentTimeString(char* timeString) {
time_t tTime = taosGetTimestampSec(); time_t tTime = taosGetTimestampSec();
struct tm tm; struct tm tm;
taosLocalTime(&tTime, &tm); taosLocalTime(&tTime, &tm, NULL);
sprintf(timeString, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, sprintf(timeString, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
tm.tm_min, tm.tm_sec); tm.tm_min, tm.tm_sec);
@ -472,7 +472,9 @@ static char* shellFormatTimestamp(char* buf, int64_t val, int32_t precision) {
} }
struct tm ptm; struct tm ptm;
taosLocalTime(&tt, &ptm); if (taosLocalTime(&tt, &ptm, buf) == NULL) {
return buf;
}
size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm);
if (precision == TSDB_TIME_PRECISION_NANO) { if (precision == TSDB_TIME_PRECISION_NANO) {

View File

@ -772,7 +772,9 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) {
tt = (*(int64_t *)row[i]) / 1000000000; tt = (*(int64_t *)row[i]) / 1000000000;
} }
taosLocalTime(&tt, &tp); if (taosLocalTime(&tt, &tp, timeStr) == NULL) {
break;
}
strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", &tp); strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", &tp);
if (precision == TSDB_TIME_PRECISION_MILLI) { if (precision == TSDB_TIME_PRECISION_MILLI) {
sprintf(value, "%s.%03d", timeStr, (int32_t)(*((int64_t *)row[i]) % 1000)); sprintf(value, "%s.%03d", timeStr, (int32_t)(*((int64_t *)row[i]) % 1000));