diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 53be1fccf4..3b755c2921 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -197,7 +197,12 @@ static int32_t generateWriteSlowLog(STscObj *pTscObj, SRequestObj *pRequest, int ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(""))); } - char *value = cJSON_PrintUnformatted(json); + char *value = cJSON_PrintUnformatted(json); + if (value == NULL) { + tscError("failed to print json"); + code = TSDB_CODE_FAILED; + goto _end; + } MonitorSlowLogData data = {0}; data.clusterId = pTscObj->pAppInfo->clusterId; data.type = SLOW_LOG_WRITE; diff --git a/source/client/src/clientMonitor.c b/source/client/src/clientMonitor.c index f424e4d1a2..6667c4c741 100644 --- a/source/client/src/clientMonitor.c +++ b/source/client/src/clientMonitor.c @@ -447,20 +447,19 @@ static char* readFile(TdFilePtr pFile, int64_t* offset, int64_t size) { char* pCont = NULL; int64_t totalSize = 0; if (size - *offset >= SLOW_LOG_SEND_SIZE_MAX) { - pCont = taosMemoryCalloc(1, 4 + SLOW_LOG_SEND_SIZE_MAX); // 4 reserved for [] totalSize = 4 + SLOW_LOG_SEND_SIZE_MAX; } else { - pCont = taosMemoryCalloc(1, 4 + (size - *offset)); totalSize = 4 + (size - *offset); } + pCont = taosMemoryCalloc(1, totalSize); // 4 reserved for [] if (pCont == NULL) { tscError("failed to allocate memory for slow log, size:%" PRId64, totalSize); return NULL; } char* buf = pCont; (void)strcat(buf++, "["); - int64_t readSize = taosReadFile(pFile, buf, SLOW_LOG_SEND_SIZE_MAX); + int64_t readSize = taosReadFile(pFile, buf, totalSize - 4); // 4 reserved for [] if (readSize <= 0) { if (readSize < 0) { tscError("failed to read len from file:%p since %s", pFile, terrstr()); diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index 40f48af266..ef8c1eb860 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -426,16 +426,19 @@ int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) { return terrno; } + int64_t res = 0; DWORD bytesRead; if (!ReadFile(pFile->hFile, buf, count, &bytesRead, NULL)) { DWORD errCode = GetLastError(); terrno = TAOS_SYSTEM_WINAPI_ERROR(errCode); - bytesRead = -1; + res = -1; + } else { + res = bytesRead; } #if FILE_WITH_LOCK (void)taosThreadRwlockUnlock(&(pFile->rwlock)); #endif - return bytesRead; + return res; } int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {