Parameter validation

This commit is contained in:
xsren 2024-11-07 13:36:53 +08:00
parent 60da49c09e
commit 04565b23ef
7 changed files with 45 additions and 14 deletions

View File

@ -62,7 +62,8 @@ static FORCE_INLINE int64_t taosGetTimestampToday(int32_t precision) {
int64_t factor = (precision == TSDB_TIME_PRECISION_MILLI) ? 1000 int64_t factor = (precision == TSDB_TIME_PRECISION_MILLI) ? 1000
: (precision == TSDB_TIME_PRECISION_MICRO) ? 1000000 : (precision == TSDB_TIME_PRECISION_MICRO) ? 1000000
: 1000000000; : 1000000000;
time_t t = taosTime(NULL); time_t t;
(void) taosTime(&t);
struct tm tm; struct tm tm;
(void) taosLocalTime(&t, &tm, NULL, 0); (void) taosLocalTime(&t, &tm, NULL, 0);
tm.tm_hour = 0; tm.tm_hour = 0;

View File

@ -93,7 +93,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, char *buf, int32_t bufSize); struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf, int32_t bufSize);
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); int32_t taosTime(time_t *t);
time_t taosMktime(struct tm *timep); time_t taosMktime(struct tm *timep);
int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t day, const uint32_t hour, int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t day, const uint32_t hour,
const uint32_t min, const uint32_t sec, int64_t time_zone); const uint32_t min, const uint32_t sec, int64_t time_zone);

View File

@ -30,7 +30,7 @@ static int64_t m_deltaUtc = 0;
void deltaToUtcInitOnce() { void deltaToUtcInitOnce() {
struct tm tm = {0}; struct tm tm = {0};
if (taosStrpTime("1970-01-01 00:00:00", (const char*)("%Y-%m-%d %H:%M:%S"), &tm) != 0) { if (taosStrpTime("1970-01-01 00:00:00", (const char*)("%Y-%m-%d %H:%M:%S"), &tm) == NULL) {
uError("failed to parse time string"); uError("failed to parse time string");
} }
m_deltaUtc = (int64_t)taosMktime(&tm); m_deltaUtc = (int64_t)taosMktime(&tm);

View File

@ -188,7 +188,11 @@ static int32_t countTrailingSpaces(const SValueNode* pVal, bool isLtrim) {
static int32_t addTimezoneParam(SNodeList* pList) { static int32_t addTimezoneParam(SNodeList* pList) {
char buf[TD_TIME_STR_LEN] = {0}; char buf[TD_TIME_STR_LEN] = {0};
time_t t = taosTime(NULL); time_t t;
int32_t code = taosTime(&t);
if (code != 0) {
return code;
}
struct tm tmInfo; struct tm tmInfo;
if (taosLocalTime(&t, &tmInfo, buf, sizeof(buf)) != NULL) { if (taosLocalTime(&t, &tmInfo, buf, sizeof(buf)) != NULL) {
(void)strftime(buf, sizeof(buf), "%z", &tmInfo); (void)strftime(buf, sizeof(buf), "%z", &tmInfo);
@ -196,7 +200,7 @@ static int32_t addTimezoneParam(SNodeList* pList) {
int32_t len = (int32_t)strlen(buf); int32_t len = (int32_t)strlen(buf);
SValueNode* pVal = NULL; SValueNode* pVal = NULL;
int32_t code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal); code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal);
if (pVal == NULL) { if (pVal == NULL) {
return code; return code;
} }

View File

@ -246,7 +246,7 @@ static int32_t parseBoundColumns(SInsertParseContext* pCxt, const char** pSql, E
return code; return code;
} }
static int parseTimestampOrInterval(const char** end, SToken* pToken, int16_t timePrec, int64_t* ts, int64_t* interval, static int32_t parseTimestampOrInterval(const char** end, SToken* pToken, int16_t timePrec, int64_t* ts, int64_t* interval,
SMsgBuf* pMsgBuf, bool* isTs) { SMsgBuf* pMsgBuf, bool* isTs) {
if (pToken->type == TK_NOW) { if (pToken->type == TK_NOW) {
*isTs = true; *isTs = true;

View File

@ -81,6 +81,7 @@ static const char *am_pm[2] = {"AM", "PM"};
#endif #endif
char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) { char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) {
if (!buf || !fmt || !tm) return NULL;
#ifdef WINDOWS #ifdef WINDOWS
char c; char c;
const char *bp; const char *bp;
@ -345,6 +346,9 @@ char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) {
} }
int32_t taosGetTimeOfDay(struct timeval *tv) { int32_t taosGetTimeOfDay(struct timeval *tv) {
if (tv == NULL) {
return TSDB_CODE_INVALID_PARA;
}
int32_t code = 0; int32_t code = 0;
#ifdef WINDOWS #ifdef WINDOWS
LARGE_INTEGER t; LARGE_INTEGER t;
@ -365,12 +369,15 @@ int32_t taosGetTimeOfDay(struct timeval *tv) {
#endif #endif
} }
time_t taosTime(time_t *t) { int32_t taosTime(time_t *t) {
if (t == NULL) {
return TSDB_CODE_INVALID_PARA;
}
time_t r = time(t); time_t r = time(t);
if (r == (time_t)-1) { if (r == (time_t)-1) {
terrno = TAOS_SYSTEM_ERROR(errno); return TAOS_SYSTEM_ERROR(errno);
} }
return r; return 0;
} }
/* /*

View File

@ -154,16 +154,26 @@ static int32_t taosStartLog() {
return 0; return 0;
} }
static void getDay(char *buf, int32_t bufSize) { static int32_t getDay(char *buf, int32_t bufSize) {
time_t t = taosTime(NULL); time_t t;
int32_t code = taosTime(&t);
if(code != 0) {
return code;
}
struct tm tmInfo; struct tm tmInfo;
if (taosLocalTime(&t, &tmInfo, buf, bufSize) != NULL) { if (taosLocalTime(&t, &tmInfo, buf, bufSize) != NULL) {
TAOS_UNUSED(strftime(buf, bufSize, "%Y-%m-%d", &tmInfo)); TAOS_UNUSED(strftime(buf, bufSize, "%Y-%m-%d", &tmInfo));
} }
return 0;
} }
static int64_t getTimestampToday() { static int64_t getTimestampToday() {
time_t t = taosTime(NULL); time_t t;
int32_t code = taosTime(&t);
if (code != 0) {
uError("failed to get time, reason:%s", tstrerror(code));
return 0;
}
struct tm tm; struct tm tm;
if (taosLocalTime(&t, &tm, NULL, 0) == NULL) { if (taosLocalTime(&t, &tm, NULL, 0) == NULL) {
return 0; return 0;
@ -203,7 +213,11 @@ int32_t taosInitSlowLog() {
char name[PATH_MAX + TD_TIME_STR_LEN] = {0}; char name[PATH_MAX + TD_TIME_STR_LEN] = {0};
char day[TD_TIME_STR_LEN] = {0}; char day[TD_TIME_STR_LEN] = {0};
getDay(day, sizeof(day)); int32_t code = getDay(day, sizeof(day));
if (code != 0) {
(void)printf("failed to get day, reason:%s\n", tstrerror(code));
return code;
}
(void)snprintf(name, PATH_MAX + TD_TIME_STR_LEN, "%s.%s", tsLogObj.slowLogName, day); (void)snprintf(name, PATH_MAX + TD_TIME_STR_LEN, "%s.%s", tsLogObj.slowLogName, day);
tsLogObj.timestampToday = getTimestampToday(); tsLogObj.timestampToday = getTimestampToday();
@ -434,7 +448,12 @@ static void taosOpenNewSlowLogFile() {
atomic_store_32(&tsLogObj.slowHandle->lock, 0); atomic_store_32(&tsLogObj.slowHandle->lock, 0);
char day[TD_TIME_STR_LEN] = {0}; char day[TD_TIME_STR_LEN] = {0};
getDay(day, sizeof(day)); int32_t code = getDay(day, sizeof(day));
if (code != 0) {
uError("failed to get day, reason:%s", tstrerror(code));
(void)taosThreadMutexUnlock(&tsLogObj.logMutex);
return;
}
TdFilePtr pFile = NULL; TdFilePtr pFile = NULL;
char name[PATH_MAX + TD_TIME_STR_LEN] = {0}; char name[PATH_MAX + TD_TIME_STR_LEN] = {0};
(void)snprintf(name, PATH_MAX + TD_TIME_STR_LEN, "%s.%s", tsLogObj.slowLogName, day); (void)snprintf(name, PATH_MAX + TD_TIME_STR_LEN, "%s.%s", tsLogObj.slowLogName, day);