From 3b742596079f0913ab20bc861b578610e739de40 Mon Sep 17 00:00:00 2001 From: l00518380 Date: Fri, 11 Sep 2020 15:34:22 +0800 Subject: [PATCH 1/2] modify utils strlen to strnlen --- file/src/file_impl_hal/file.c | 10 ++--- hals/file/hal_file.h | 8 ++-- include/utils_file.h | 8 ++-- .../filekit/include/nativeapi_fs_impl.h | 4 +- js/builtin/filekit/src/nativeapi_fs.cpp | 7 ++-- js/builtin/filekit/src/nativeapi_fs_impl.c | 41 +++++++++++-------- js/builtin/kvstorekit/include/nativeapi_kv.h | 8 ++-- js/builtin/kvstorekit/src/nativeapi_kv.cpp | 12 ++++-- js/builtin/kvstorekit/src/nativeapi_kv_impl.c | 12 ++++-- kal/timer/include/kal.h | 2 +- kal/timer/src/kal.c | 16 ++++---- kv_store/src/kvstore_common/kvstore_common.c | 36 ++++++++-------- kv_store/src/kvstore_impl_posix/kv_store.c | 4 +- timer_task/include/nativeapi_timer_task.h | 2 +- timer_task/src/nativeapi_timer_task.c | 2 +- 15 files changed, 95 insertions(+), 77 deletions(-) diff --git a/file/src/file_impl_hal/file.c b/file/src/file_impl_hal/file.c index 2d20ade..980b50c 100755 --- a/file/src/file_impl_hal/file.c +++ b/file/src/file_impl_hal/file.c @@ -31,22 +31,22 @@ int UtilsFileClose(int fd) return HalFileClose(fd); } -int UtilsFileRead(int fd, char *buf, unsigned int len) +int UtilsFileRead(int fd, char* buf, unsigned int len) { return HalFileRead(fd, buf, len); } -int UtilsFileWrite(int fd, const char *buf, unsigned int len) +int UtilsFileWrite(int fd, const char* buf, unsigned int len) { return HalFileWrite(fd, buf, len); } -int UtilsFileDelete(const char *path) +int UtilsFileDelete(const char* path) { return HalFileDelete(path); } -int UtilsFileStat(const char *path, unsigned int *fileSize) +int UtilsFileStat(const char* path, unsigned int* fileSize) { return HalFileStat(path, fileSize); } @@ -70,7 +70,7 @@ int UtilsFileCopy(const char* src, const char* dest) UtilsFileClose(fpSrc); return fpDest; } - char *dataBuf = (char *)malloc(BUFFER_SIZE); + char* dataBuf = (char *)malloc(BUFFER_SIZE); if (dataBuf == NULL) { UtilsFileClose(fpSrc); UtilsFileClose(fpDest); diff --git a/hals/file/hal_file.h b/hals/file/hal_file.h index 2146449..bfcf3a2 100755 --- a/hals/file/hal_file.h +++ b/hals/file/hal_file.h @@ -26,13 +26,13 @@ int HalFileOpen(const char* path, int oflag, int mode); int HalFileClose(int fd); -int HalFileRead(int fd, char *buf, unsigned int len); +int HalFileRead(int fd, char* buf, unsigned int len); -int HalFileWrite(int fd, const char *buf, unsigned int len); +int HalFileWrite(int fd, const char* buf, unsigned int len); -int HalFileDelete(const char *path); +int HalFileDelete(const char* path); -int HalFileStat(const char *path, unsigned int *fileSize); +int HalFileStat(const char* path, unsigned int* fileSize); int HalFileSeek(int fd, int offset, unsigned int whence); diff --git a/include/utils_file.h b/include/utils_file.h index f459211..109dff2 100755 --- a/include/utils_file.h +++ b/include/utils_file.h @@ -182,7 +182,7 @@ int UtilsFileClose(int fd); * @since 1.0 * @version 1.0 */ -int UtilsFileRead(int fd, char *buf, unsigned int len); +int UtilsFileRead(int fd, char* buf, unsigned int len); /** * @brief Writes a specified length of data into a file with the specified file descriptor. @@ -194,7 +194,7 @@ int UtilsFileRead(int fd, char *buf, unsigned int len); * @since 1.0 * @version 1.0 */ -int UtilsFileWrite(int fd, const char *buf, unsigned int len); +int UtilsFileWrite(int fd, const char* buf, unsigned int len); /** * @brief Deletes a specified file. @@ -206,7 +206,7 @@ int UtilsFileWrite(int fd, const char *buf, unsigned int len); * @since 1.0 * @version 1.0 */ -int UtilsFileDelete(const char *path); +int UtilsFileDelete(const char* path); /** * @brief Obtains the file size. @@ -217,7 +217,7 @@ int UtilsFileDelete(const char *path); * @since 1.0 * @version 1.0 */ -int UtilsFileStat(const char *path, unsigned int *fileSize); +int UtilsFileStat(const char* path, unsigned int* fileSize); /** * @brief Adjusts the read and write position offset in a file. diff --git a/js/builtin/filekit/include/nativeapi_fs_impl.h b/js/builtin/filekit/include/nativeapi_fs_impl.h index 800e7df..8a2b9dd 100755 --- a/js/builtin/filekit/include/nativeapi_fs_impl.h +++ b/js/builtin/filekit/include/nativeapi_fs_impl.h @@ -34,10 +34,10 @@ typedef struct { mode_t fileMode; } FileMetaInfo; -int StatImpl(const char *path, struct stat *buf); +int StatImpl(const char* path, struct stat* buf); int DeleteFileImpl(const char* src); int CopyFileImpl(const char* src, const char* dest); -int WriteTextFile(const char *fileName, const void *buf, size_t len, bool append); +int WriteTextFile(const char* fileName, const void* buf, size_t len, bool append); int WriteArrayFile(const char* fileName, const void* buf, size_t len, unsigned int position, bool append); int ReadFileImpl(const char* fileName, void* text, size_t len, unsigned int position, size_t* actualLen); int GetFileListImpl(const char* dirName, FileMetaInfo* fileList, unsigned int listNum); diff --git a/js/builtin/filekit/src/nativeapi_fs.cpp b/js/builtin/filekit/src/nativeapi_fs.cpp index e8e2e55..9249b9e 100755 --- a/js/builtin/filekit/src/nativeapi_fs.cpp +++ b/js/builtin/filekit/src/nativeapi_fs.cpp @@ -28,10 +28,11 @@ const unsigned int PREFIX_LEN = strlen(FILE_PREFIX); bool IsValidPath(const char* path) { - if ((path == nullptr) || (strlen(path) > URI_NAME_MAX_LEN)) { + size_t pathLen = strnlen(path, URI_NAME_MAX_LEN + 1); + if ((path == nullptr) || (pathLen > URI_NAME_MAX_LEN)) { return false; } - if ((strlen(path) < PREFIX_LEN) || (strncmp(path, FILE_PREFIX, PREFIX_LEN) != 0)) { + if ((pathLen < PREFIX_LEN) || (strncmp(path, FILE_PREFIX, PREFIX_LEN) != 0)) { return false; } if ((strstr(path, "/./") != nullptr) || (strstr(path, "/../") != nullptr)) { @@ -161,7 +162,7 @@ int GetFileListInner(const char* path, const char* key, JSIValue& result) if (fileNum <= 0) { return fileNum; } - FileMetaInfo *fileList = reinterpret_cast(malloc(fileNum * sizeof(FileMetaInfo))); + FileMetaInfo* fileList = reinterpret_cast(malloc(fileNum * sizeof(FileMetaInfo))); if (fileList == nullptr) { return ERROR_CODE_GENERAL; } diff --git a/js/builtin/filekit/src/nativeapi_fs_impl.c b/js/builtin/filekit/src/nativeapi_fs_impl.c index be08125..9fa91e8 100755 --- a/js/builtin/filekit/src/nativeapi_fs_impl.c +++ b/js/builtin/filekit/src/nativeapi_fs_impl.c @@ -27,9 +27,13 @@ #define BUFFER_SIZE 512 -static bool IsValidPath(const char *path) +static bool IsValidPath(const char* path) { - if ((path == NULL) || !strlen(path) || (strlen(path) > FILE_NAME_MAX_LEN)) { + if (path == NULL) { + return false; + } + size_t pathLen = strnlen(path, FILE_NAME_MAX_LEN + 1); + if ((pathLen == 0) || (pathLen > FILE_NAME_MAX_LEN)) { return false; } if (strpbrk(path, "\"*+,:;<=>\?[]|\x7F")) { @@ -57,11 +61,11 @@ static int RmdirRecursive(const char* fileName) return ERROR_CODE_PARAM; } int ret = ERROR_CODE_GENERAL; - DIR *fileDir = opendir(fileName); + DIR* fileDir = opendir(fileName); if (fileDir == NULL) { return ret; } - struct dirent *dir = readdir(fileDir); + struct dirent* dir = readdir(fileDir); struct stat info = { 0 }; char* fullPath = (char *)malloc(FILE_NAME_MAX_LEN + 1); if (fullPath == NULL) { @@ -103,17 +107,18 @@ static int RmdirRecursive(const char* fileName) static int MakeParent(const char* path, char* firstPath, size_t fPathLen, int* dirNum) { - char* fullPath = (char *)malloc(strlen(path) + 1); + size_t pathLen = strlen(path) + 1; + char* fullPath = (char *)malloc(pathLen); if (fullPath == NULL) { return ERROR_CODE_GENERAL; } - if (strcpy_s(fullPath, strlen(path) + 1, path) != EOK) { + if (strcpy_s(fullPath, pathLen, path) != EOK) { free(fullPath); return ERROR_CODE_GENERAL; } int ret = NATIVE_SUCCESS; if (AccessImpl(fullPath) != NATIVE_SUCCESS) { - char *sep = strrchr(fullPath, '/'); + char* sep = strrchr(fullPath, '/'); if (sep != NULL) { *sep = 0; MakeParent(fullPath, firstPath, fPathLen, dirNum); @@ -157,7 +162,7 @@ static int MkdirRecursive(const char* path) static int DoCopyFile(int fdSrc, int fdDest) { - char *dataBuf = (char *)malloc(BUFFER_SIZE); + char* dataBuf = (char *)malloc(BUFFER_SIZE); if (dataBuf == NULL) { return ERROR_CODE_GENERAL; } @@ -176,7 +181,7 @@ static int DoCopyFile(int fdSrc, int fdDest) return NATIVE_SUCCESS; } -int StatImpl(const char *path, struct stat *buf) +int StatImpl(const char* path, struct stat* buf) { if (!IsValidPath(path) || (buf == NULL)) { return ERROR_CODE_PARAM; @@ -200,7 +205,7 @@ int DeleteFileImpl(const char* src) return NATIVE_SUCCESS; } -int CopyFileImpl(const char *src, const char *dest) +int CopyFileImpl(const char* src, const char* dest) { if (!IsValidPath(src) || !IsValidPath(dest)) { return ERROR_CODE_PARAM; @@ -245,7 +250,7 @@ int CopyFileImpl(const char *src, const char *dest) return ret; } -int WriteTextFile(const char *fileName, const void *buf, size_t len, bool append) +int WriteTextFile(const char* fileName, const void* buf, size_t len, bool append) { if (!IsValidPath(fileName) || (buf == NULL)) { return ERROR_CODE_PARAM; @@ -276,7 +281,7 @@ int WriteTextFile(const char *fileName, const void *buf, size_t len, bool append return NATIVE_SUCCESS; } -int WriteArrayFile(const char *fileName, const void *buf, size_t len, unsigned int position, bool append) +int WriteArrayFile(const char* fileName, const void* buf, size_t len, unsigned int position, bool append) { if (!IsValidPath(fileName) || (buf == NULL)) { return ERROR_CODE_PARAM; @@ -317,7 +322,7 @@ int WriteArrayFile(const char *fileName, const void *buf, size_t len, unsigned i return NATIVE_SUCCESS; } -int ReadFileImpl(const char *fileName, void* text, size_t len, unsigned int position, size_t* actualLen) +int ReadFileImpl(const char* fileName, void* text, size_t len, unsigned int position, size_t* actualLen) { if (!IsValidPath(fileName) || (text == NULL)) { return ERROR_CODE_PARAM; @@ -368,14 +373,14 @@ int GetFileListImpl(const char* dirName, FileMetaInfo* fileList, unsigned int li return ERROR_CODE_PARAM; } int ret = ERROR_CODE_GENERAL; - DIR *fileDir = opendir(dirName); + DIR* fileDir = opendir(dirName); if (fileDir == NULL) { return ret; } int fileIndex = 0; - struct dirent *dir = readdir(fileDir); + struct dirent* dir = readdir(fileDir); struct stat fileStat = { 0 }; - char *fullFileName = (char *)malloc(FILE_NAME_MAX_LEN + 1); + char* fullFileName = (char *)malloc(FILE_NAME_MAX_LEN + 1); if (fullFileName == NULL) { goto EXIT; } @@ -411,11 +416,11 @@ int GetFileNum(const char* dirName) if (ret != NATIVE_SUCCESS) { return ret; } - DIR *fileDir = opendir(dirName); + DIR* fileDir = opendir(dirName); if (fileDir == NULL) { return ERROR_CODE_PARAM; } - struct dirent *dir = readdir(fileDir); + struct dirent* dir = readdir(fileDir); int sum = 0; while (dir != NULL) { sum++; diff --git a/js/builtin/kvstorekit/include/nativeapi_kv.h b/js/builtin/kvstorekit/include/nativeapi_kv.h index 3b544e8..29ac835 100755 --- a/js/builtin/kvstorekit/include/nativeapi_kv.h +++ b/js/builtin/kvstorekit/include/nativeapi_kv.h @@ -24,10 +24,10 @@ class NativeapiKv { public: NativeapiKv() {} ~NativeapiKv() {} - static JSIValue Get(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); - static JSIValue Set(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); - static JSIValue Delete(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); - static JSIValue Clear(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue Get(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); + static JSIValue Set(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); + static JSIValue Delete(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); + static JSIValue Clear(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); }; } // ACELite } // OHOS diff --git a/js/builtin/kvstorekit/src/nativeapi_kv.cpp b/js/builtin/kvstorekit/src/nativeapi_kv.cpp index 2541d8c..fdf1ae1 100755 --- a/js/builtin/kvstorekit/src/nativeapi_kv.cpp +++ b/js/builtin/kvstorekit/src/nativeapi_kv.cpp @@ -26,9 +26,13 @@ namespace ACELite { namespace { char g_kvFullPath[FILE_NAME_MAX_LEN + 1] = {0}; -bool IsValidKey(const char *key) +bool IsValidKey(const char* key) { - if ((key == nullptr) || !strlen(key) || (strlen(key) > KEY_MAX_LEN)) { + if (key == nullptr) { + return false; + } + size_t keyLen = strnlen(key, KEY_MAX_LEN + 1); + if ((keyLen == 0) || (keyLen > KEY_MAX_LEN)) { return false; } if (strpbrk(key, "/\\\"*+,:;<=>\?[]|\x7F")) { @@ -53,7 +57,7 @@ int GetFullPath(const char* dataPath, const char* key) void GetDefault(const JSIValue thisVal, const JSIValue args) { - char *defaultValue = JSI::GetStringProperty(args, DEFAULT); + char* defaultValue = JSI::GetStringProperty(args, DEFAULT); JSIValue result; if (defaultValue == nullptr) { result = JSI::CreateString(""); @@ -125,7 +129,7 @@ void ExecuteGet(void* data) const char* dataPath = GetDataPath(); JSIValue result = JSI::CreateUndefined(); int ret = ERROR_CODE_GENERAL; - char *value = reinterpret_cast(malloc(VALUE_MAX_LEN + 1)); + char* value = reinterpret_cast(malloc(VALUE_MAX_LEN + 1)); if (value == nullptr) { NativeapiCommon::FailCallBack(thisVal, args, ret); goto EXIT; diff --git a/js/builtin/kvstorekit/src/nativeapi_kv_impl.c b/js/builtin/kvstorekit/src/nativeapi_kv_impl.c index 4794e4e..94ec960 100755 --- a/js/builtin/kvstorekit/src/nativeapi_kv_impl.c +++ b/js/builtin/kvstorekit/src/nativeapi_kv_impl.c @@ -27,9 +27,13 @@ static char g_kvFolder[FILE_NAME_MAX_LEN + 1] = {0}; -static bool IsValidValue(const char *value) +static bool IsValidValue(const char* value) { - if ((value == NULL) || !strlen(value) || (strlen(value) > VALUE_MAX_LEN)) { + if (value == NULL) { + return false; + } + size_t valueLen = strnlen(value, VALUE_MAX_LEN + 1); + if ((valueLen == 0) || (valueLen > VALUE_MAX_LEN)) { return false; } return true; @@ -152,11 +156,11 @@ int ClearKVStore(const char* dataPath) return ret; } ret = ERROR_CODE_GENERAL; - DIR *fileDir = opendir(g_kvFolder); + DIR* fileDir = opendir(g_kvFolder); if (fileDir == NULL) { return ret; } - struct dirent *dir = readdir(fileDir); + struct dirent* dir = readdir(fileDir); char* fullPath = (char *)malloc(FILE_NAME_MAX_LEN + 1); if (fullPath == NULL) { goto EXIT; diff --git a/kal/timer/include/kal.h b/kal/timer/include/kal.h index d25729e..6b21cde 100755 --- a/kal/timer/include/kal.h +++ b/kal/timer/include/kal.h @@ -36,7 +36,7 @@ typedef enum { KAL_ERR_TIMER_STATE = 0x100, } KalErrCode; -KalTimerId KalTimerCreate(KalTimerProc func, KalTimerType type, void *arg, unsigned int millisec); +KalTimerId KalTimerCreate(KalTimerProc func, KalTimerType type, void* arg, unsigned int millisec); KalErrCode KalTimerStart(KalTimerId timerId); KalErrCode KalTimerChange(KalTimerId timerId, unsigned int millisec); KalErrCode KalTimerStop(KalTimerId timerId); diff --git a/kal/timer/src/kal.c b/kal/timer/src/kal.c index 1896fa3..b99e2d0 100755 --- a/kal/timer/src/kal.c +++ b/kal/timer/src/kal.c @@ -33,28 +33,28 @@ typedef struct { static void KalFunction(union sigval kalTimer) { - KalTimer *tmpPtr = (KalTimer *)(kalTimer.sival_ptr); + KalTimer* tmpPtr = (KalTimer *)(kalTimer.sival_ptr); if (tmpPtr->type == KAL_TIMER_ONCE) { tmpPtr->isRunning = 0; } tmpPtr->func(tmpPtr->arg); } -static void KalMs2TimeSpec(struct timespec *tp, unsigned int ms) +static void KalMs2TimeSpec(struct timespec* tp, unsigned int ms) { tp->tv_sec = ms / LOSCFG_BASE_CORE_MS_PER_SECOND; ms -= tp->tv_sec * LOSCFG_BASE_CORE_MS_PER_SECOND; tp->tv_nsec = (long)(((unsigned long long)ms * OS_SYS_NS_PER_SECOND) / LOSCFG_BASE_CORE_MS_PER_SECOND); } -KalTimerId KalTimerCreate(KalTimerProc func, KalTimerType type, void *arg, unsigned int millisec) +KalTimerId KalTimerCreate(KalTimerProc func, KalTimerType type, void* arg, unsigned int millisec) { struct sigevent evp = {0}; timer_t timer; if ((func == NULL) || ((type != KAL_TIMER_ONCE) && (type != KAL_TIMER_PERIODIC))) { return NULL; } - KalTimer *kalTimer = (KalTimer *)malloc(sizeof(KalTimer)); + KalTimer* kalTimer = (KalTimer *)malloc(sizeof(KalTimer)); if (kalTimer == NULL) { return NULL; } @@ -81,7 +81,7 @@ KalErrCode KalTimerStart(KalTimerId timerId) return KAL_ERR_PARA; } struct itimerspec ts = {0}; - KalTimer *tmpPtr = (KalTimer *)timerId; + KalTimer* tmpPtr = (KalTimer *)timerId; KalMs2TimeSpec(&ts.it_value, tmpPtr->millisec); if (tmpPtr->type == KAL_TIMER_PERIODIC) { KalMs2TimeSpec(&ts.it_interval, tmpPtr->millisec); @@ -101,7 +101,7 @@ KalErrCode KalTimerChange(KalTimerId timerId, unsigned int millisec) if (timerId == NULL) { return KAL_ERR_PARA; } - KalTimer *tmpPtr = (KalTimer *)timerId; + KalTimer* tmpPtr = (KalTimer *)timerId; struct itimerspec ts = {0}; tmpPtr->millisec = millisec; if (tmpPtr->isRunning == 1) { @@ -126,7 +126,7 @@ KalErrCode KalTimerStop(KalTimerId timerId) if (timerId == NULL) { return KAL_ERR_PARA; } - KalTimer *tmpPtr = (KalTimer *)timerId; + KalTimer* tmpPtr = (KalTimer *)timerId; struct itimerspec ts = {0}; int ret = timer_settime(tmpPtr->timerPtr, 0, &ts, NULL); if (ret != 0) { @@ -141,7 +141,7 @@ KalErrCode KalTimerDelete(KalTimerId timerId) if (timerId == NULL) { return KAL_ERR_PARA; } - KalTimer *tmpPtr = (KalTimer *)timerId; + KalTimer* tmpPtr = (KalTimer *)timerId; int ret = timer_delete(tmpPtr->timerPtr); if (ret != 0) { free(timerId); diff --git a/kv_store/src/kvstore_common/kvstore_common.c b/kv_store/src/kvstore_common/kvstore_common.c index 079e41c..5dce515 100755 --- a/kv_store/src/kvstore_common/kvstore_common.c +++ b/kv_store/src/kvstore_common/kvstore_common.c @@ -38,12 +38,10 @@ boolean IsValidValue(const char* value, unsigned int len) if (value == NULL) { return FALSE; } - size_t valueLen = strnlen(value, MAX_VALUE_LEN); - if (valueLen <= 0 || valueLen >= MAX_VALUE_LEN || valueLen >= len) { + if ((valueLen == 0) || (valueLen >= MAX_VALUE_LEN) || (valueLen >= len)) { return FALSE; } - return TRUE; } @@ -52,9 +50,8 @@ boolean IsValidKey(const char* key) if (!IsValidValue(key, MAX_KEY_LEN)) { return FALSE; } - - int keyLen = strnlen(key, MAX_KEY_LEN); - for (int i = 0; i < keyLen; i++) { + size_t keyLen = strnlen(key, MAX_KEY_LEN); + for (size_t i = 0; i < keyLen; i++) { if (!IsValidChar(key[i])) { return FALSE; } @@ -70,14 +67,11 @@ static void FreeItem(KvItem* item) } if (item->key != NULL) { free(item->key); - item->key = NULL; } if (item->value != NULL) { free(item->value); - item->value = NULL; } free(item); - item = NULL; } void DeleteKVCache(const char* key) @@ -86,7 +80,7 @@ void DeleteKVCache(const char* key) return; } KvItem* item = g_itemHeader; - while (strcmp(key, item->key) != 0) { + while (strncmp(key, item->key, strlen(key)) != 0) { item = item->next; if (item == NULL) { return; @@ -118,14 +112,20 @@ void AddKVCache(const char* key, const char* value, boolean isNew) if (item == NULL) { return; } - item->key = (char *)malloc(strlen(key) + 1); - item->value = (char *)malloc(strlen(value) + 1); + size_t keyLen = strnlen(key, MAX_KEY_LEN); + size_t valueLen = strnlen(value, MAX_VALUE_LEN); + if ((keyLen >= MAX_KEY_LEN) || (valueLen >= MAX_VALUE_LEN)) { + FreeItem(item); + return; + } + item->key = (char *)malloc(keyLen + 1); + item->value = (char *)malloc(valueLen + 1); if ((item->key == NULL) || (item->value == NULL)) { FreeItem(item); return; } - if ((strcpy_s(item->key, strlen(key) + 1, key) != EOK) || - (strcpy_s(item->value, strlen(value) + 1, value) != EOK)) { + if ((strcpy_s(item->key, keyLen + 1, key) != EOK) || + (strcpy_s(item->value, valueLen + 1, value) != EOK)) { FreeItem(item); return; } @@ -156,13 +156,17 @@ int GetValueByCache(const char* key, char* value, unsigned int maxLen) return EC_FAILURE; } KvItem* item = g_itemHeader; - while (strcmp(key, item->key) != 0) { + while (strncmp(key, item->key, strlen(key)) != 0) { item = item->next; if (item == NULL) { return EC_FAILURE; } } - if ((maxLen <= strlen(item->value)) || (strcpy_s(value, maxLen, item->value) != EOK)) { + size_t valueLen = strnlen(item->value, MAX_VALUE_LEN); + if (valueLen >= MAX_VALUE_LEN) { + return EC_FAILURE; + } + if ((valueLen >= maxLen) || (strcpy_s(value, maxLen, item->value) != EOK)) { return EC_FAILURE; } return EC_SUCCESS; diff --git a/kv_store/src/kvstore_impl_posix/kv_store.c b/kv_store/src/kvstore_impl_posix/kv_store.c index 3fa060b..bfa7cdc 100755 --- a/kv_store/src/kvstore_impl_posix/kv_store.c +++ b/kv_store/src/kvstore_impl_posix/kv_store.c @@ -166,11 +166,11 @@ static int GetCurrentItem(const char* dataPath) if (sprintf_s(kvPath, MAX_KEY_PATH + 1, "%s/%s", dataPath, KVSTORE_PATH) < 0) { return EC_FAILURE; } - DIR *fileDir = opendir(kvPath); + DIR* fileDir = opendir(kvPath); if (fileDir == NULL) { return EC_FAILURE; } - struct dirent *dir = readdir(fileDir); + struct dirent* dir = readdir(fileDir); int sum = 0; while (dir != NULL) { char fullPath[MAX_KEY_PATH + 1] = {0}; diff --git a/timer_task/include/nativeapi_timer_task.h b/timer_task/include/nativeapi_timer_task.h index f0a2d18..ba598e9 100755 --- a/timer_task/include/nativeapi_timer_task.h +++ b/timer_task/include/nativeapi_timer_task.h @@ -28,7 +28,7 @@ typedef void *timerHandle_t; int InitTimerTask(); int StartTimerTask(bool isPeriodic, const unsigned int delay, void* userCallback, - void* userContext, timerHandle_t *timerHandle); + void* userContext, timerHandle_t* timerHandle); int StopTimerTask(const timerHandle_t timerHandle); #ifdef __cplusplus diff --git a/timer_task/src/nativeapi_timer_task.c b/timer_task/src/nativeapi_timer_task.c index e6418bf..a47f3c8 100755 --- a/timer_task/src/nativeapi_timer_task.c +++ b/timer_task/src/nativeapi_timer_task.c @@ -24,7 +24,7 @@ int InitTimerTask() } int StartTimerTask(bool isPeriodic, const unsigned int delay, void* userCallback, - void* userContext, timerHandle_t *timerHandle) + void* userContext, timerHandle_t* timerHandle) { if (userCallback == NULL) { return EC_FAILURE; -- Gitee From f9de0c782e975426e38d1f9fa50dc79c233d6606 Mon Sep 17 00:00:00 2001 From: liubb <1653617791@qq.com> Date: Sat, 12 Sep 2020 12:50:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?update=20kv=5Fstore/src/kvstore=5Fcommon/kv?= =?UTF-8?q?store=5Fcommon.c.=20utils=20=E5=8A=9F=E8=83=BD=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kv_store/src/kvstore_common/kvstore_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kv_store/src/kvstore_common/kvstore_common.c b/kv_store/src/kvstore_common/kvstore_common.c index 5dce515..3244269 100755 --- a/kv_store/src/kvstore_common/kvstore_common.c +++ b/kv_store/src/kvstore_common/kvstore_common.c @@ -80,7 +80,7 @@ void DeleteKVCache(const char* key) return; } KvItem* item = g_itemHeader; - while (strncmp(key, item->key, strlen(key)) != 0) { + while (strcmp(key, item->key) != 0) { item = item->next; if (item == NULL) { return; @@ -156,7 +156,7 @@ int GetValueByCache(const char* key, char* value, unsigned int maxLen) return EC_FAILURE; } KvItem* item = g_itemHeader; - while (strncmp(key, item->key, strlen(key)) != 0) { + while (strcmp(key, item->key) != 0) { item = item->next; if (item == NULL) { return EC_FAILURE; -- Gitee