From e124a54d509e2ba258544458c996d476bada157a Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Mon, 4 Nov 2019 21:39:01 +0800 Subject: [PATCH] The length of submit message used to insert data to server is incorrect #673 --- src/client/src/tscServer.c | 9 +++++++-- src/client/src/tscUtil.c | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 1c838d22ae..ec48430453 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -217,14 +217,19 @@ int tscSendMsgToServer(SSqlObj *pSql) { int32_t totalMsgLen = pSql->cmd.payloadLen + tsRpcHeadSize + sizeof(STaosDigest); // the memory will be released by taosProcessResponse, so no memory leak here - char* buf = malloc(totalMsgLen); - memcpy(buf, pSql->cmd.payload, totalMsgLen); + char *buf = malloc(totalMsgLen); + if (NULL == buf) { + tscError("%p msg:%s malloc fail", pSql, taosMsg[pSql->cmd.msgType]); + return TSDB_CODE_CLI_OUT_OF_MEMORY; + } + memcpy(buf, pSql->cmd.payload, (size_t) totalMsgLen); tscTrace("%p msg:%s is sent to server", pSql, taosMsg[pSql->cmd.msgType]); char *pStart = taosBuildReqHeader(pSql->thandle, pSql->cmd.msgType, buf); if (pStart) { if (tscUpdateVnodeMsg[pSql->cmd.command]) (*tscUpdateVnodeMsg[pSql->cmd.command])(pSql, buf); int ret = taosSendMsgToPeerH(pSql->thandle, pStart, pSql->cmd.payloadLen, pSql); + if (ret >= 0) code = 0; tscTrace("%p send msg ret:%d code:%d sig:%p", pSql, ret, code, pSql->signature); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index a8ea4a820d..4fb6b655af 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -384,11 +384,22 @@ int32_t tscCopyDataBlockToPayload(SSqlObj* pSql, STableDataBlocks* pDataBlock) { pCmd->count = pDataBlock->numOfMeters; strncpy(pCmd->name, pDataBlock->meterId, TSDB_METER_ID_LEN); - tscAllocPayloadWithSize(pCmd, pDataBlock->nAllocSize); + /* + * the submit message consists of : [RPC header|message body|digest] + * the dataBlock only includes the RPC Header buffer and actual submit messsage body, space for digest needs + * additional space. + */ + int ret = tscAllocPayloadWithSize(pCmd, pDataBlock->nAllocSize + sizeof(STaosDigest)); + if (TSDB_CODE_SUCCESS != ret) return ret; memcpy(pCmd->payload, pDataBlock->pData, pDataBlock->nAllocSize); - // set the message length - pCmd->payloadLen = pDataBlock->nAllocSize; + /* + * the payloadLen should be actual message body size + * the old value of payloadLen is the allocated payload size + */ + pCmd->payloadLen = pDataBlock->nAllocSize - tsRpcHeadSize; + + assert(pCmd->allocSize >= pCmd->payloadLen + tsRpcHeadSize + sizeof(STaosDigest)); return tscGetMeterMeta(pSql, pCmd->name); } -- GitLab