From 1d331d89a8d220cab22685afe425b230f5e87746 Mon Sep 17 00:00:00 2001 From: facetosea <25808407@qq.com> Date: Mon, 11 Nov 2024 16:26:42 +0800 Subject: [PATCH] os file --- source/os/src/osEnv.c | 8 ++++- source/os/src/osFile.c | 71 ++++++++++++++++++++++++++++++++----- source/os/src/osLocale.c | 6 ++++ source/os/src/osSemaphore.c | 48 +++++++++++++++++++++---- 4 files changed, 118 insertions(+), 15 deletions(-) diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 05c9936c2e..1dfad1b937 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -127,8 +127,14 @@ bool osTempSpaceSufficient() { return tsTempSpace.size.avail > tsTempSpace.reser int32_t osSetTimezone(const char *tz) { return taosSetSystemTimezone(tz, tsTimezoneStr, &tsDaylight, &tsTimezone); } void osSetSystemLocale(const char *inLocale, const char *inCharSet) { + if (inLocale == NULL || inCharSet == NULL) return; (void)memcpy(tsLocale, inLocale, strlen(inLocale) + 1); (void)memcpy(tsCharset, inCharSet, strlen(inCharSet) + 1); } -void osSetProcPath(int32_t argc, char **argv) { tsProcPath = argv[0]; } +void osSetProcPath(int32_t argc, char **argv) { + if (argv == NULL || argc < 1) { + return; // no command line arguments + } + tsProcPath = argv[0]; +} diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 2f18c6e697..c2484860ad 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -80,6 +80,7 @@ typedef struct TdFile { #endif void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) { + if (inputTmpDir == NULL || fileNamePrefix == NULL) return; #ifdef WINDOWS char tmpPath[PATH_MAX]; @@ -120,6 +121,10 @@ void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, cha } int64_t taosCopyFile(const char *from, const char *to) { + if (from == NULL || to == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return -1; + } #ifdef WINDOWS if (CopyFile(from, to, 0)) { return 1; @@ -133,15 +138,15 @@ int64_t taosCopyFile(const char *from, const char *to) { int64_t bytes; int32_t code = TSDB_CODE_SUCCESS; - // fidfrom = open(from, O_RDONLY); - TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ); + TdFilePtr pFileFrom = NULL; + TdFilePtr pFileTo = NULL; + pFileFrom = taosOpenFile(from, TD_FILE_READ); if (pFileFrom == NULL) { code = terrno; goto _err; } - // fidto = open(to, O_WRONLY | O_CREAT | O_EXCL, 0755); - TdFilePtr pFileTo = taosOpenFile(to, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_EXCL); + pFileTo = taosOpenFile(to, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_EXCL); if (pFileTo == NULL) { code = terrno; goto _err; @@ -193,6 +198,10 @@ _err: } TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) { + if(path == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } TdFilePtr fp = taosOpenFile(path, tdFileOptions); if (!fp) { if (terrno == TAOS_SYSTEM_ERROR(ENOENT)) { @@ -213,6 +222,7 @@ TdFilePtr taosCreateFile(const char *path, int32_t tdFileOptions) { } int32_t taosRemoveFile(const char *path) { + OS_PARAM_CHECK(path); int32_t code = remove(path); if (-1 == code) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -222,6 +232,8 @@ int32_t taosRemoveFile(const char *path) { } int32_t taosRenameFile(const char *oldName, const char *newName) { + OS_PARAM_CHECK(oldName); + OS_PARAM_CHECK(newName); #ifdef WINDOWS bool finished = false; @@ -262,6 +274,7 @@ int32_t taosRenameFile(const char *oldName, const char *newName) { } int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime, int32_t *atime) { + OS_PARAM_CHECK(path); #ifdef WINDOWS struct _stati64 fileStat; int32_t code = _stati64(path, &fileStat); @@ -335,6 +348,10 @@ int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) { } FILE *taosOpenFileForStream(const char *path, int32_t tdFileOptions) { + if (path == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } char *mode = NULL; if (tdFileOptions & TD_FILE_APPEND) { mode = (tdFileOptions & TD_FILE_TEXT) ? "at+" : "ab+"; @@ -358,6 +375,10 @@ FILE *taosOpenFileForStream(const char *path, int32_t tdFileOptions) { #ifdef WINDOWS HANDLE taosOpenFileNotStream(const char *path, int32_t tdFileOptions) { + if (path == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return INVALID_HANDLE_VALUE; + } DWORD openMode = 0; DWORD access = 0; DWORD fileFlag = FILE_ATTRIBUTE_NORMAL; @@ -408,6 +429,10 @@ HANDLE taosOpenFileNotStream(const char *path, int32_t tdFileOptions) { } int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { + if (pFile == NULL || buf == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return terrno; + } #if FILE_WITH_LOCK (void)taosThreadRwlockRdlock(&(pFile->rwlock)); #endif @@ -435,7 +460,7 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { } int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { - if (pFile == NULL || pFile->hFile == NULL) { + if (pFile == NULL || pFile->hFile == NULL || buf == NULL) { terrno = TSDB_CODE_INVALID_PARA; return 0; } @@ -457,7 +482,7 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { } int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t offset) { - if (pFile == NULL) { + if (pFile == NULL || buf == NULL) { terrno = TSDB_CODE_INVALID_PARA; return 0; } @@ -719,6 +744,10 @@ bool lastErrorIsFileNotExist() { #else int taosOpenFileNotStream(const char *path, int32_t tdFileOptions) { + if (path == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return -1; + } int access = O_BINARY; access |= (tdFileOptions & TD_FILE_CREATE) ? O_CREAT : 0; if ((tdFileOptions & TD_FILE_WRITE) && (tdFileOptions & TD_FILE_READ)) { @@ -742,6 +771,10 @@ int taosOpenFileNotStream(const char *path, int32_t tdFileOptions) { } int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { + if (pFile == NULL || buf == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return -1; + } STUB_RAND_IO_ERR(terrno) #if FILE_WITH_LOCK (void)taosThreadRwlockRdlock(&(pFile->rwlock)); @@ -797,7 +830,7 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { STUB_RAND_IO_ERR(terrno) - if (pFile == NULL) { + if (pFile == NULL || buf == NULL) { terrno = TSDB_CODE_INVALID_PARA; return 0; } @@ -843,7 +876,7 @@ int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) { int64_t taosPWriteFile(TdFilePtr pFile, const void *buf, int64_t count, int64_t offset) { STUB_RAND_IO_ERR(terrno) - if (pFile == NULL) { + if (pFile == NULL || buf == NULL) { terrno = TSDB_CODE_INVALID_PARA; return 0; } @@ -1050,6 +1083,10 @@ bool lastErrorIsFileNotExist() { return terrno == TAOS_SYSTEM_ERROR(ENOENT); } #endif // WINDOWS TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) { + if (path == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } STUB_RAND_IO_ERR(NULL) FILE *fp = NULL; #ifdef WINDOWS @@ -1399,6 +1436,10 @@ int32_t taosEOFFile(TdFilePtr pFile) { } bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) { + if (pathname == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return false; // invalid parameter + } int flags = 0; if (tdFileAccessOptions & TD_FILE_ACCESS_EXIST_OK) { @@ -1422,6 +1463,8 @@ bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) { bool taosCheckExistFile(const char *pathname) { return taosCheckAccessFile(pathname, TD_FILE_ACCESS_EXIST_OK); }; int32_t taosCompressFile(char *srcFileName, char *destFileName) { + OS_PARAM_CHECK(srcFileName); + OS_PARAM_CHECK(destFileName); int32_t compressSize = 163840; int32_t ret = 0; int32_t len = 0; @@ -1516,6 +1559,10 @@ int32_t taosLinkFile(char *src, char *dst) { } FILE *taosOpenCFile(const char *filename, const char *mode) { + if (filename == NULL || mode == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return NULL; + } STUB_RAND_IO_ERR(NULL) FILE *f = fopen(filename, mode); if (NULL == f) { @@ -1525,6 +1572,10 @@ FILE *taosOpenCFile(const char *filename, const char *mode) { } int taosSeekCFile(FILE *file, int64_t offset, int whence) { + if(NULL == file) { + terrno = TSDB_CODE_INVALID_PARA; + return terrno; + } #ifdef WINDOWS return _fseeki64(file, offset, whence); #else @@ -1538,6 +1589,10 @@ int taosSeekCFile(FILE *file, int64_t offset, int whence) { } size_t taosReadFromCFile(void *buffer, size_t size, size_t count, FILE *stream) { + if (buffer == NULL || stream == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return 0; + } STUB_RAND_IO_ERR(terrno) return fread(buffer, size, count, stream); } diff --git a/source/os/src/osLocale.c b/source/os/src/osLocale.c index becf0d5a70..21f781c7e4 100644 --- a/source/os/src/osLocale.c +++ b/source/os/src/osLocale.c @@ -52,6 +52,9 @@ typedef struct CharsetPair { } CharsetPair; char *taosCharsetReplace(char *charsetstr) { + if (charsetstr == NULL) { + return NULL; + } CharsetPair charsetRep[] = { {"utf8", "UTF-8"}, {"936", "CP936"}, @@ -76,6 +79,8 @@ char *taosCharsetReplace(char *charsetstr) { * In case that the setLocale failed to be executed, the right charset needs to be set. */ int32_t taosSetSystemLocale(const char *inLocale, const char *inCharSet) { + OS_PARAM_CHECK(inLocale); + OS_PARAM_CHECK(inCharSet); if (!taosValidateEncodec(inCharSet)) { return terrno; } @@ -90,6 +95,7 @@ int32_t taosSetSystemLocale(const char *inLocale, const char *inCharSet) { } void taosGetSystemLocale(char *outLocale, char *outCharset) { + if (outLocale == NULL || outCharset == NULL) return; #ifdef WINDOWS char *locale = setlocale(LC_CTYPE, "en_US.UTF-8"); if (locale != NULL) { diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index e61fd627eb..9327b33f79 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -29,7 +29,11 @@ bool taosCheckPthreadValid(TdThread thread) { return thread.p != NULL; } -void taosResetPthread(TdThread* thread) { thread->p = 0; } +void taosResetPthread(TdThread* thread) { + if (thread != NULL) { + thread->p = NULL; + } +} int64_t taosGetPthreadId(TdThread thread) { #ifdef PTW32_VERSION @@ -46,6 +50,7 @@ bool taosComparePthread(TdThread first, TdThread second) { return first.p == sec int32_t taosGetPId() { return GetCurrentProcessId(); } int32_t taosGetAppName(char* name, int32_t* len) { + OS_PARAM_CHECK(name); char filepath[1024] = {0}; if (GetModuleFileName(NULL, filepath, MAX_PATH) == 0) { @@ -75,6 +80,8 @@ int32_t taosGetAppName(char* name, int32_t* len) { int32_t taosGetPIdByName(const char* name, int32_t* pPId) { return -1;} int32_t tsem_wait(tsem_t* sem) { + OS_PARAM_CHECK(sem); + OS_PARAM_CHECK(*sem); DWORD ret = WaitForSingleObject(*sem, INFINITE); if (ret == WAIT_OBJECT_0) { return 0; @@ -84,6 +91,8 @@ int32_t tsem_wait(tsem_t* sem) { } int32_t tsem_timewait(tsem_t* sem, int64_t timeout_ms) { + OS_PARAM_CHECK(sem); + OS_PARAM_CHECK(*sem); DWORD result = WaitForSingleObject(*sem, timeout_ms); if (result == WAIT_OBJECT_0) { return 0; // Semaphore acquired @@ -96,16 +105,21 @@ int32_t tsem_timewait(tsem_t* sem, int64_t timeout_ms) { // Inter-process sharing is not currently supported. The pshared parameter is invalid. int32_t tsem_init(tsem_t* sem, int pshared, unsigned int value) { + OS_PARAM_CHECK(sem); *sem = CreateSemaphore(NULL, value, LONG_MAX, NULL); return (*sem != NULL) ? 0 : TAOS_SYSTEM_WINAPI_ERROR(GetLastError()); } int32_t tsem_post(tsem_t* sem) { + OS_PARAM_CHECK(sem); + OS_PARAM_CHECK(*sem); if (ReleaseSemaphore(*sem, 1, NULL)) return 0; return TAOS_SYSTEM_WINAPI_ERROR(GetLastError()); } int32_t tsem_destroy(tsem_t* sem) { + OS_PARAM_CHECK(sem); + OS_PARAM_CHECK(*sem); if (CloseHandle(*sem)) return 0; return TAOS_SYSTEM_WINAPI_ERROR(GetLastError()); } @@ -115,6 +129,7 @@ int32_t tsem_destroy(tsem_t* sem) { #include int32_t tsem_init(tsem_t *psem, int flags, unsigned int count) { + OS_PARAM_CHECK(psem); *psem = dispatch_semaphore_create(count); if (*psem == NULL) return TAOS_SYSTEM_ERROR(errno); return 0; @@ -128,19 +143,19 @@ int32_t tsem_destroy(tsem_t *psem) { } int32_t tsem_post(tsem_t *psem) { - if (psem == NULL || *psem == NULL) return -1; + if (psem == NULL || *psem == NULL) return TSDB_CODE_INVALID_PARA; (void)dispatch_semaphore_signal(*psem); return 0; } int32_t tsem_wait(tsem_t *psem) { - if (psem == NULL || *psem == NULL) return -1; + if (psem == NULL || *psem == NULL) return TSDB_CODE_INVALID_PARA; dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER); return 0; } int32_t tsem_timewait(tsem_t *psem, int64_t milis) { - if (psem == NULL || *psem == NULL) return -1; + if (psem == NULL || *psem == NULL) return TSDB_CODE_INVALID_PARA; dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(milis * USEC_PER_SEC)); if (dispatch_semaphore_wait(*psem, time) == 0) { return 0; @@ -158,13 +173,18 @@ int64_t taosGetSelfPthreadId() { int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; } -void taosResetPthread(TdThread *thread) { *thread = NULL; } +void taosResetPthread(TdThread *thread) { + if (thread) { + *thread = NULL; + } +} bool taosComparePthread(TdThread first, TdThread second) { return taosThreadEqual(first, second) ? true : false; } int32_t taosGetPId() { return (int32_t)getpid(); } int32_t taosGetAppName(char *name, int32_t *len) { + OS_PARAM_CHECK(name); char buf[PATH_MAX + 1]; buf[0] = '\0'; proc_name(getpid(), buf, sizeof(buf) - 1); @@ -196,7 +216,11 @@ int64_t taosGetSelfPthreadId() { } int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; } -void taosResetPthread(TdThread* thread) { *thread = 0; } +void taosResetPthread(TdThread* thread) { + if (thread) { + *thread = 0; + } +} bool taosComparePthread(TdThread first, TdThread second) { return first == second; } int32_t taosGetPId() { @@ -207,6 +231,7 @@ int32_t taosGetPId() { } int32_t taosGetAppName(char* name, int32_t* len) { + OS_PARAM_CHECK(name); const char* self = "/proc/self/exe"; char path[PATH_MAX] = {0}; @@ -233,6 +258,8 @@ int32_t taosGetAppName(char* name, int32_t* len) { } int32_t taosGetPIdByName(const char* name, int32_t* pPId) { + OS_PARAM_CHECK(name); + OS_PARAM_CHECK(pPId); DIR* dir = NULL; struct dirent* ptr = NULL; FILE* fp = NULL; @@ -294,6 +321,7 @@ int32_t tsem_init(tsem_t* psem, int flags, unsigned int count) { } int32_t tsem_timewait(tsem_t* sem, int64_t ms) { + OS_PARAM_CHECK(sem); int ret = 0; struct timespec ts = {0}; @@ -322,6 +350,7 @@ int32_t tsem_timewait(tsem_t* sem, int64_t ms) { } int32_t tsem_wait(tsem_t* sem) { + OS_PARAM_CHECK(sem); int ret = 0; do { ret = sem_wait(sem); @@ -336,6 +365,7 @@ int32_t tsem_wait(tsem_t* sem) { } int tsem2_init(tsem2_t* sem, int pshared, unsigned int value) { + OS_PARAM_CHECK(sem); int ret = taosThreadMutexInit(&sem->mutex, NULL); if (ret != 0) return ret; @@ -365,6 +395,7 @@ int tsem2_init(tsem2_t* sem, int pshared, unsigned int value) { } int32_t tsem_post(tsem_t* psem) { + OS_PARAM_CHECK(psem); if (sem_post(psem) == 0) { return 0; } else { @@ -373,6 +404,7 @@ int32_t tsem_post(tsem_t* psem) { } int32_t tsem_destroy(tsem_t* sem) { + OS_PARAM_CHECK(sem); if (sem_destroy(sem) == 0) { return 0; } else { @@ -381,6 +413,7 @@ int32_t tsem_destroy(tsem_t* sem) { } int tsem2_post(tsem2_t* sem) { + OS_PARAM_CHECK(sem); int32_t code = taosThreadMutexLock(&sem->mutex); if (code) { return code; @@ -401,6 +434,7 @@ int tsem2_post(tsem2_t* sem) { } int tsem2_destroy(tsem2_t* sem) { + OS_PARAM_CHECK(sem); (void)taosThreadMutexDestroy(&sem->mutex); (void)taosThreadCondDestroy(&sem->cond); (void)taosThreadCondAttrDestroy(&sem->attr); @@ -409,6 +443,7 @@ int tsem2_destroy(tsem2_t* sem) { } int32_t tsem2_wait(tsem2_t* sem) { + OS_PARAM_CHECK(sem); int32_t code = taosThreadMutexLock(&sem->mutex); if (code) { return code; @@ -434,6 +469,7 @@ int32_t tsem2_wait(tsem2_t* sem) { } int32_t tsem2_timewait(tsem2_t* sem, int64_t ms) { + OS_PARAM_CHECK(sem); int32_t code = 0; code = taosThreadMutexLock(&sem->mutex);