From 3ce28c06792e0473a9764d714d0dd415e5b3e404 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Mon, 2 Aug 2021 16:08:05 +0800 Subject: [PATCH] [TD-5505]: return error if there're additional illegal characters after timezeone str --- src/os/src/detail/osTime.c | 19 ++++++++++++++----- src/query/tests/unitTest.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/os/src/detail/osTime.c b/src/os/src/detail/osTime.c index 696867e586..9b5a86cbbb 100644 --- a/src/os/src/detail/osTime.c +++ b/src/os/src/detail/osTime.c @@ -72,12 +72,12 @@ int64_t user_mktime64(const unsigned int year0, const unsigned int mon0, // ==== mktime() kernel code =================// static int64_t m_deltaUtc = 0; -void deltaToUtcInitOnce() { +void deltaToUtcInitOnce() { struct tm tm = {0}; - + (void)strptime("1970-01-01 00:00:00", (const char *)("%Y-%m-%d %H:%M:%S"), &tm); m_deltaUtc = (int64_t)mktime(&tm); - //printf("====delta:%lld\n\n", seconds); + //printf("====delta:%lld\n\n", seconds); return; } @@ -90,7 +90,7 @@ static char* forwardToTimeStringEnd(char* str); static int32_t (*parseLocaltimeFp[]) (char* timestr, int64_t* time, int32_t timePrec) = { parseLocaltime, parseLocaltimeWithDst -}; +}; int32_t taosGetTimestampSec() { return (int32_t)time(NULL); } @@ -194,6 +194,13 @@ int32_t parseTimezone(char* str, int64_t* tzOffset) { i += 2; } + //return error if there're illegal charaters after min(2 Digits) + char *minStr = &str[i]; + if (minStr[1] != '\0' && minStr[2] != '\0') { + return -1; + } + + int64_t minute = strnatoi(&str[i], 2); if (minute > 59) { return -1; @@ -252,7 +259,7 @@ int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec, char del int64_t fraction = 0; str = forwardToTimeStringEnd(timestr); - if (str[0] == 'Z' || str[0] == 'z') { + if ((str[0] == 'Z' || str[0] == 'z') && str[1] == '\0') { /* utc time, no millisecond, return directly*/ *time = seconds * factor; } else if (str[0] == '.') { @@ -266,6 +273,8 @@ int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec, char del char seg = str[0]; if (seg != 'Z' && seg != 'z' && seg != '+' && seg != '-') { return -1; + } else if ((seg == 'Z' || seg == 'z') && str[1] != '\0') { + return -1; } else if (seg == '+' || seg == '-') { // parse the timezone if (parseTimezone(str, &tzOffset) == -1) { diff --git a/src/query/tests/unitTest.cpp b/src/query/tests/unitTest.cpp index 51afca9532..e898992a40 100644 --- a/src/query/tests/unitTest.cpp +++ b/src/query/tests/unitTest.cpp @@ -462,6 +462,42 @@ TEST(testCase, parse_time) { char t23[] = "2017-12-31T9:0:0.1+13:1"; EXPECT_EQ(taosParseTime(t23, &time, strlen(t23), TSDB_TIME_PRECISION_MILLI, 0), 0); + + char t24[] = "2017-12-31T9:0:0.1+13:001"; + EXPECT_EQ(taosParseTime(t24, &time, strlen(t24), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t25[] = "2017-12-31T9:0:0.1+13:00abc"; + EXPECT_EQ(taosParseTime(t25, &time, strlen(t25), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t26[] = "2017-12-31T9:0:0.1+13001"; + EXPECT_EQ(taosParseTime(t26, &time, strlen(t26), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t27[] = "2017-12-31T9:0:0.1+1300abc"; + EXPECT_EQ(taosParseTime(t27, &time, strlen(t27), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t28[] = "2017-12-31T9:0:0Z+12:00"; + EXPECT_EQ(taosParseTime(t28, &time, strlen(t28), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t29[] = "2017-12-31T9:0:0.123Z+12:00"; + EXPECT_EQ(taosParseTime(t29, &time, strlen(t29), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t65[] = "2017-12-31 9:0:0.1+13:001"; + EXPECT_EQ(taosParseTime(t65, &time, strlen(t65), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t66[] = "2017-12-31 9:0:0.1+13:00abc"; + EXPECT_EQ(taosParseTime(t66, &time, strlen(t66), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t67[] = "2017-12-31 9:0:0.1+13001"; + EXPECT_EQ(taosParseTime(t67, &time, strlen(t67), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t68[] = "2017-12-31 9:0:0.1+1300abc"; + EXPECT_EQ(taosParseTime(t68, &time, strlen(t68), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t69[] = "2017-12-31 9:0:0Z+12:00"; + EXPECT_EQ(taosParseTime(t69, &time, strlen(t69), TSDB_TIME_PRECISION_MILLI, 0), -1); + + char t70[] = "2017-12-31 9:0:0.123Z+12:00"; + EXPECT_EQ(taosParseTime(t70, &time, strlen(t70), TSDB_TIME_PRECISION_MILLI, 0), -1); } TEST(testCase, tvariant_convert) { -- GitLab