diff --git a/include/os/os.h b/include/os/os.h index e3808065dd..9a1544aeb4 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -137,6 +137,13 @@ extern threadlocal bool tsEnableRandErr; terrno = _code; \ } +#define OS_PARAM_CHECK(_o) \ + do { \ + if ((_o) == NULL) { \ + return TSDB_CODE_INVALID_PARA; \ + } \ + } while(0) + #ifdef __cplusplus } #endif diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 84de563cda..777c6a9216 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -116,7 +116,10 @@ void taosRemoveDir(const char *dirname) { return; } -bool taosDirExist(const char *dirname) { return taosCheckExistFile(dirname); } +bool taosDirExist(const char *dirname) { + if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return false; + return taosCheckExistFile(dirname); +} int32_t taosMkDir(const char *dirname) { if (taosDirExist(dirname)) return 0; @@ -333,6 +336,8 @@ void taosRemoveOldFiles(const char *dirname, int32_t keepDays) { } int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) { + OS_PARAM_CHECK(dirname); + OS_PARAM_CHECK(outname); wordexp_t full_path; int32_t code = wordexp(dirname, &full_path, 0); switch (code) { @@ -355,6 +360,8 @@ int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) { } int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen) { + OS_PARAM_CHECK(dirname); + OS_PARAM_CHECK(realPath); char tmp[PATH_MAX] = {0}; #ifdef WINDOWS if (_fullpath(tmp, dirname, maxlen) != NULL) { @@ -386,6 +393,10 @@ bool taosIsDir(const char *dirname) { } char *taosDirName(char *name) { + if(name == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } #ifdef WINDOWS char Drive1[MAX_PATH], Dir1[MAX_PATH]; _splitpath(name, Drive1, Dir1, NULL, NULL); @@ -412,12 +423,16 @@ char *taosDirName(char *name) { } char *taosDirEntryBaseName(char *name) { + if(name == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } #ifdef WINDOWS char Filename1[MAX_PATH], Ext1[MAX_PATH]; _splitpath(name, NULL, NULL, Filename1, Ext1); return name + (strlen(name) - strlen(Filename1) - strlen(Ext1)); #else - if (name == NULL || (name[0] == '/' && name[1] == '\0')) return name; + if ((name[0] == '/' && name[1] == '\0')) return name; char *pPoint = strrchr(name, '/'); if (pPoint != NULL) { if (*(pPoint + 1) == '\0') { @@ -515,9 +530,9 @@ bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) { } char *taosGetDirEntryName(TdDirEntryPtr pDirEntry) { - /*if (pDirEntry == NULL) {*/ - /*return NULL;*/ - /*}*/ + if (pDirEntry == NULL) { + return NULL; + } #ifdef WINDOWS return pDirEntry->findFileData.cFileName; #else