diff --git a/include/libs/qcom/query.h b/include/libs/qcom/query.h index 2078455f1d..c0f6b23c77 100644 --- a/include/libs/qcom/query.h +++ b/include/libs/qcom/query.h @@ -334,7 +334,7 @@ SSchema createSchema(int8_t type, int32_t bytes, col_id_t colId, const char* nam void destroyQueryExecRes(SExecResult* pRes); int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t* len); -char* parseTagDatatoJson(void* p); +void parseTagDatatoJson(void* p, char** jsonStr); int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst); void getColumnTypeFromMeta(STableMeta* pMeta, char* pName, ETableColumnType* pType); int32_t cloneDbVgInfo(SDBVgInfo* pSrc, SDBVgInfo** pDst); diff --git a/include/os/osString.h b/include/os/osString.h index ac7dd7eda8..8672da616d 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -55,7 +55,7 @@ typedef enum { M2C = 0, C2M } ConvType; #define tstrncpy(dst, src, size) \ do { \ - strncpy((dst), (src), (size)); \ + (void)strncpy((dst), (src), (size)); \ (dst)[(size)-1] = 0; \ } while (0) diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index f8a817dc46..7f8bcb8613 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -2007,7 +2007,8 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int sprintf(varDataVal(dst), "%s", TSDB_DATA_NULL_STR_L); varDataSetLen(dst, strlen(varDataVal(dst))); } else if (tTagIsJson(data)) { - char* jsonString = parseTagDatatoJson(data); + char* jsonString = NULL; + parseTagDatatoJson(data, &jsonString); STR_TO_VARSTR(dst, jsonString); taosMemoryFree(jsonString); } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) { // value -> "value" diff --git a/source/client/src/clientRawBlockWrite.c b/source/client/src/clientRawBlockWrite.c index ce404092b0..bc76cb5da9 100644 --- a/source/client/src/clientRawBlockWrite.c +++ b/source/client/src/clientRawBlockWrite.c @@ -23,6 +23,31 @@ #include "tglobal.h" #include "tmsgtype.h" +#define RAW_NULL_CHECK(c) \ + do { \ + if (c == NULL) { \ + code = TSDB_CODE_OUT_OF_MEMORY; \ + goto end; \ + } \ + } while (0) + +#define RAW_FALSE_CHECK(c) \ + do { \ + if (!c) { \ + code = TSDB_CODE_INVALID_PARA; \ + goto end; \ + } \ + } while (0) + +#define RAW_RETURN_CHECK(c) \ + do { \ + code == c; \ + if (code != 0) { \ + goto end; \ + } \ + } while (0) + + #define LOG_ID_TAG "connId:0x%" PRIx64 ",reqId:0x%" PRIx64 #define LOG_ID_VALUE *(int64_t*)taos, pRequest->requestId @@ -32,8 +57,9 @@ static int32_t tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, int32_t metaLen static tb_uid_t processSuid(tb_uid_t suid, char* db) { return suid + MurmurHash3_32(db, strlen(db)); } -static cJSON* buildCreateTableJson(SSchemaWrapper* schemaRow, SSchemaWrapper* schemaTag, char* name, int64_t id, - int8_t t, SColCmprWrapper* pColCmprRow) { +static void buildCreateTableJson(SSchemaWrapper* schemaRow, SSchemaWrapper* schemaTag, char* name, int64_t id, + int8_t t, SColCmprWrapper* pColCmprRow, cJSON** pJson) { + int32_t code = TSDB_CODE_SUCCESS; int8_t buildDefaultCompress = 0; if (pColCmprRow->nCols <= 0) { buildDefaultCompress = 1; @@ -41,43 +67,45 @@ static cJSON* buildCreateTableJson(SSchemaWrapper* schemaRow, SSchemaWrapper* sc char* string = NULL; cJSON* json = cJSON_CreateObject(); - if (json == NULL) { - uError("create json object failed") return NULL; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("create"); - cJSON_AddItemToObject(json, "type", type); + RAW_NULL_CHECK(type); - // char uid[32] = {0}; - // sprintf(uid, "%"PRIi64, id); - // cJSON* id_ = cJSON_CreateString(uid); - // cJSON_AddItemToObject(json, "id", id_); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); cJSON* tableType = cJSON_CreateString(t == TSDB_NORMAL_TABLE ? "normal" : "super"); - cJSON_AddItemToObject(json, "tableType", tableType); + RAW_NULL_CHECK(tableType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType)); cJSON* tableName = cJSON_CreateString(name); - cJSON_AddItemToObject(json, "tableName", tableName); - // cJSON* version = cJSON_CreateNumber(1); - // cJSON_AddItemToObject(json, "version", version); + RAW_NULL_CHECK(tableName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName)); cJSON* columns = cJSON_CreateArray(); + RAW_NULL_CHECK(columns); for (int i = 0; i < schemaRow->nCols; i++) { cJSON* column = cJSON_CreateObject(); + RAW_NULL_CHECK(column); SSchema* s = schemaRow->pSchema + i; cJSON* cname = cJSON_CreateString(s->name); - cJSON_AddItemToObject(column, "name", cname); + RAW_NULL_CHECK(cname); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "name", cname)); cJSON* ctype = cJSON_CreateNumber(s->type); - cJSON_AddItemToObject(column, "type", ctype); + RAW_NULL_CHECK(ctype); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "type", ctype)); if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = s->bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(column, "length", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "length", cbytes)); } else if (s->type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(column, "length", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "length", cbytes)); } cJSON* isPk = cJSON_CreateBool(s->flags & COL_IS_KEY); - cJSON_AddItemToObject(column, "isPrimarykey", isPk); - cJSON_AddItemToArray(columns, column); + RAW_NULL_CHECK(isPk); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "isPrimarykey", isPk)); + RAW_FALSE_CHECK(cJSON_AddItemToArray(columns, column)); if (pColCmprRow == NULL) { continue; @@ -91,177 +119,222 @@ static cJSON* buildCreateTableJson(SSchemaWrapper* schemaRow, SSchemaWrapper* sc alg = pColCmpr->alg; } const char* encode = columnEncodeStr(COMPRESS_L1_TYPE_U32(alg)); + RAW_NULL_CHECK(encode); const char* compress = columnCompressStr(COMPRESS_L2_TYPE_U32(alg)); + RAW_NULL_CHECK(compress); const char* level = columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(alg)); + RAW_NULL_CHECK(level); cJSON* encodeJson = cJSON_CreateString(encode); - cJSON_AddItemToObject(column, "encode", encodeJson); + RAW_NULL_CHECK(encodeJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "encode", encodeJson)); cJSON* compressJson = cJSON_CreateString(compress); - cJSON_AddItemToObject(column, "compress", compressJson); + RAW_NULL_CHECK(compressJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "compress", compressJson)); cJSON* levelJson = cJSON_CreateString(level); - cJSON_AddItemToObject(column, "level", levelJson); + RAW_NULL_CHECK(levelJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(column, "level", levelJson)); } - cJSON_AddItemToObject(json, "columns", columns); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "columns", columns)); cJSON* tags = cJSON_CreateArray(); + RAW_NULL_CHECK(tags); for (int i = 0; schemaTag && i < schemaTag->nCols; i++) { cJSON* tag = cJSON_CreateObject(); + RAW_NULL_CHECK(tag); SSchema* s = schemaTag->pSchema + i; cJSON* tname = cJSON_CreateString(s->name); - cJSON_AddItemToObject(tag, "name", tname); + RAW_NULL_CHECK(tname); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "name", tname)); cJSON* ttype = cJSON_CreateNumber(s->type); - cJSON_AddItemToObject(tag, "type", ttype); + RAW_NULL_CHECK(ttype); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "type", ttype)); if (s->type == TSDB_DATA_TYPE_BINARY || s->type == TSDB_DATA_TYPE_VARBINARY || s->type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = s->bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(tag, "length", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "length", cbytes)); } else if (s->type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (s->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(tag, "length", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "length", cbytes)); } - cJSON_AddItemToArray(tags, tag); + RAW_FALSE_CHECK(cJSON_AddItemToArray(tags, tag)); } - cJSON_AddItemToObject(json, "tags", tags); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tags", tags)); - return json; +end: + *pJson = json; } static int32_t setCompressOption(cJSON* json, uint32_t para) { uint8_t encode = COMPRESS_L1_TYPE_U32(para); + int32_t code = 0; if (encode != 0) { const char* encodeStr = columnEncodeStr(encode); + RAW_NULL_CHECK(encodeStr); cJSON* encodeJson = cJSON_CreateString(encodeStr); - cJSON_AddItemToObject(json, "encode", encodeJson); - return 0; + RAW_NULL_CHECK(encodeJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "encode", encodeJson)); + return code; } uint8_t compress = COMPRESS_L2_TYPE_U32(para); if (compress != 0) { const char* compressStr = columnCompressStr(compress); + RAW_NULL_CHECK(compressStr); cJSON* compressJson = cJSON_CreateString(compressStr); - cJSON_AddItemToObject(json, "compress", compressJson); - return 0; + RAW_NULL_CHECK(compressJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "compress", compressJson)); + return code; } uint8_t level = COMPRESS_L2_TYPE_LEVEL_U32(para); if (level != 0) { const char* levelStr = columnLevelStr(level); + RAW_NULL_CHECK(levelStr); cJSON* levelJson = cJSON_CreateString(levelStr); - cJSON_AddItemToObject(json, "level", levelJson); - return 0; + RAW_NULL_CHECK(levelJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "level", levelJson)); + return code } - return 0; + +end: + return code; } -static cJSON* buildAlterSTableJson(void* alterData, int32_t alterDataLen) { +static void buildAlterSTableJson(void* alterData, int32_t alterDataLen, cJSON** pJson) { SMAlterStbReq req = {0}; cJSON* json = NULL; char* string = NULL; + int32_t code = 0; if (tDeserializeSMAlterStbReq(alterData, alterDataLen, &req) != 0) { goto end; } json = cJSON_CreateObject(); - if (json == NULL) { - uError("create json object failed"); - goto end; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("alter"); - cJSON_AddItemToObject(json, "type", type); - // cJSON* uid = cJSON_CreateNumber(id); - // cJSON_AddItemToObject(json, "uid", uid); + RAW_NULL_CHECK(type); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); SName name = {0}; - tNameFromString(&name, req.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); + RAW_RETURN_CHECK(tNameFromString(&name, req.name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE)); cJSON* tableType = cJSON_CreateString("super"); - cJSON_AddItemToObject(json, "tableType", tableType); + RAW_NULL_CHECK(tableType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType)); cJSON* tableName = cJSON_CreateString(name.tname); - cJSON_AddItemToObject(json, "tableName", tableName); + RAW_NULL_CHECK(tableName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName)); cJSON* alterType = cJSON_CreateNumber(req.alterType); - cJSON_AddItemToObject(json, "alterType", alterType); + RAW_NULL_CHECK(alterType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "alterType", alterType)); switch (req.alterType) { case TSDB_ALTER_TABLE_ADD_TAG: case TSDB_ALTER_TABLE_ADD_COLUMN: { TAOS_FIELD* field = taosArrayGet(req.pFields, 0); + RAW_NULL_CHECK(field); cJSON* colName = cJSON_CreateString(field->name); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colType = cJSON_CreateNumber(field->type); - cJSON_AddItemToObject(json, "colType", colType); + RAW_NULL_CHECK(colType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType)); if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY || field->type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = field->bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } else if (field->type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } break; } case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: { SFieldWithOptions* field = taosArrayGet(req.pFields, 0); + RAW_NULL_CHECK(field); cJSON* colName = cJSON_CreateString(field->name); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colType = cJSON_CreateNumber(field->type); - cJSON_AddItemToObject(json, "colType", colType); + RAW_NULL_CHECK(colType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType)); if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY || field->type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = field->bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } else if (field->type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } - setCompressOption(json, field->compress); + RAW_RETURN_CHECK(setCompressOption(json, field->compress)); break; } case TSDB_ALTER_TABLE_DROP_TAG: case TSDB_ALTER_TABLE_DROP_COLUMN: { TAOS_FIELD* field = taosArrayGet(req.pFields, 0); + RAW_NULL_CHECK(field); cJSON* colName = cJSON_CreateString(field->name); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); break; } case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES: case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: { TAOS_FIELD* field = taosArrayGet(req.pFields, 0); + RAW_NULL_CHECK(field); cJSON* colName = cJSON_CreateString(field->name); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colType = cJSON_CreateNumber(field->type); - cJSON_AddItemToObject(json, "colType", colType); + RAW_NULL_CHECK(colType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType)); if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY || field->type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = field->bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } else if (field->type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (field->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } break; } case TSDB_ALTER_TABLE_UPDATE_TAG_NAME: case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: { TAOS_FIELD* oldField = taosArrayGet(req.pFields, 0); + RAW_NULL_CHECK(oldField); TAOS_FIELD* newField = taosArrayGet(req.pFields, 1); + RAW_NULL_CHECK(newField); cJSON* colName = cJSON_CreateString(oldField->name); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colNewName = cJSON_CreateString(newField->name); - cJSON_AddItemToObject(json, "colNewName", colNewName); + RAW_NULL_CHECK(colNewName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colNewName", colNewName)); break; } case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: { TAOS_FIELD* field = taosArrayGet(req.pFields, 0); + RAW_NULL_CHECK(field); cJSON* colName = cJSON_CreateString(field->name); - cJSON_AddItemToObject(json, "colName", colName); - setCompressOption(json, field->bytes); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); + RAW_RETURN_CHECK(setCompressOption(json, field->bytes)); break; } default: @@ -270,13 +343,12 @@ static cJSON* buildAlterSTableJson(void* alterData, int32_t alterDataLen) { end: tFreeSMAltertbReq(&req); - return json; + *pJson = json; } -static cJSON* processCreateStb(SMqMetaRsp* metaRsp) { +static void processCreateStb(SMqMetaRsp* metaRsp, cJSON** pJson) { SVCreateStbReq req = {0}; SDecoder coder; - cJSON* pJson = NULL; uDebug("create stable data:%p", metaRsp); // decode and process req @@ -285,19 +357,18 @@ static cJSON* processCreateStb(SMqMetaRsp* metaRsp) { tDecoderInit(&coder, data, len); if (tDecodeSVCreateStbReq(&coder, &req) < 0) { - goto _err; + goto end; } - pJson = buildCreateTableJson(&req.schemaRow, &req.schemaTag, req.name, req.suid, TSDB_SUPER_TABLE, &req.colCmpr); -_err: - uDebug("create stable return, sql json:%s", cJSON_PrintUnformatted(pJson)); + buildCreateTableJson(&req.schemaRow, &req.schemaTag, req.name, req.suid, TSDB_SUPER_TABLE, &req.colCmpr, pJson); + +end: + uDebug("create stable return, sql json:%s", cJSON_PrintUnformatted(*pJson)); tDecoderClear(&coder); - return pJson; } -static cJSON* processAlterStb(SMqMetaRsp* metaRsp) { +static void processAlterStb(SMqMetaRsp* metaRsp, cJSON** pJson) { SVCreateStbReq req = {0}; - SDecoder coder; - cJSON* pJson = NULL; + SDecoder coder = {0}; uDebug("alter stable data:%p", metaRsp); // decode and process req @@ -306,13 +377,13 @@ static cJSON* processAlterStb(SMqMetaRsp* metaRsp) { tDecoderInit(&coder, data, len); if (tDecodeSVCreateStbReq(&coder, &req) < 0) { - goto _err; + goto end; } - pJson = buildAlterSTableJson(req.alterOriData, req.alterOriDataLen); -_err: - uDebug("alter stable return, sql json:%s", cJSON_PrintUnformatted(pJson)); + buildAlterSTableJson(req.alterOriData, req.alterOriDataLen, pJson); + +end: + uDebug("alter stable return, sql json:%s", cJSON_PrintUnformatted(*pJson)); tDecoderClear(&coder); - return pJson; } static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) { @@ -322,23 +393,22 @@ static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) { SArray* tagName = pCreateReq->ctb.tagName; int64_t id = pCreateReq->uid; uint8_t tagNum = pCreateReq->ctb.tagNum; + int32_t code = 0; cJSON* tableName = cJSON_CreateString(name); - cJSON_AddItemToObject(json, "tableName", tableName); + RAW_NULL_CHECK(tableName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName)); cJSON* using = cJSON_CreateString(sname); - cJSON_AddItemToObject(json, "using", using); + RAW_NULL_CHECK(using); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "using", using)); cJSON* tagNumJson = cJSON_CreateNumber(tagNum); - cJSON_AddItemToObject(json, "tagNum", tagNumJson); - // cJSON* version = cJSON_CreateNumber(1); - // cJSON_AddItemToObject(json, "version", version); + RAW_NULL_CHECK(tagNumJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tagNum", tagNumJson)); cJSON* tags = cJSON_CreateArray(); + RAW_NULL_CHECK(tags); SArray* pTagVals = NULL; - int32_t code = tTagToValArray(pTag, &pTagVals); - if (code) { - uError("tTagToValArray failed code:%d", code); - goto end; - } + RAW_RETURN_CHECK(tTagToValArray(pTag, &pTagVals)); if (tTagIsJson(pTag)) { STag* p = (STag*)pTag; @@ -346,36 +416,41 @@ static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) { uError("p->nTag == 0"); goto end; } - char* pJson = parseTagDatatoJson(pTag); + char* pJson = NULL; + parseTagDatatoJson(pTag, &pJson); cJSON* tag = cJSON_CreateObject(); + RAW_NULL_CHECK(tag); STagVal* pTagVal = taosArrayGet(pTagVals, 0); - + RAW_NULL_CHECK(pTagVal); char* ptname = taosArrayGet(tagName, 0); + RAW_NULL_CHECK(ptname); cJSON* tname = cJSON_CreateString(ptname); - cJSON_AddItemToObject(tag, "name", tname); - // cJSON* cid_ = cJSON_CreateString(""); - // cJSON_AddItemToObject(tag, "cid", cid_); + RAW_NULL_CHECK(tname); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "name", tname)); cJSON* ttype = cJSON_CreateNumber(TSDB_DATA_TYPE_JSON); - cJSON_AddItemToObject(tag, "type", ttype); + RAW_NULL_CHECK(ttype); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "type", ttype)); cJSON* tvalue = cJSON_CreateString(pJson); - cJSON_AddItemToObject(tag, "value", tvalue); - cJSON_AddItemToArray(tags, tag); + RAW_NULL_CHECK(tvalue); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "value", tvalue)); + RAW_FALSE_CHECK(cJSON_AddItemToArray(tags, tag)); taosMemoryFree(pJson); goto end; } for (int i = 0; i < taosArrayGetSize(pTagVals); i++) { STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, i); - + RAW_NULL_CHECK(pTagVal); cJSON* tag = cJSON_CreateObject(); - + RAW_NULL_CHECK(tag); char* ptname = taosArrayGet(tagName, i); + RAW_NULL_CHECK(ptname); cJSON* tname = cJSON_CreateString(ptname); - cJSON_AddItemToObject(tag, "name", tname); - // cJSON* cid = cJSON_CreateNumber(pTagVal->cid); - // cJSON_AddItemToObject(tag, "cid", cid); + RAW_NULL_CHECK(tname); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "name", tname)); cJSON* ttype = cJSON_CreateNumber(pTagVal->type); - cJSON_AddItemToObject(tag, "type", ttype); + RAW_NULL_CHECK(ttype); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "type", ttype)); cJSON* tvalue = NULL; if (IS_VAR_DATA_TYPE(pTagVal->type)) { @@ -385,127 +460,132 @@ static void buildChildElement(cJSON* json, SVCreateTbReq* pCreateReq) { } else { buf = taosMemoryCalloc(pTagVal->nData + 3, 1); } - - if (!buf) goto end; - dataConverToStr(buf, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL); + RAW_NULL_CHECK(buf); + RAW_RETURN_CHECK(dataConverToStr(buf, pTagVal->type, pTagVal->pData, pTagVal->nData, NULL)); tvalue = cJSON_CreateString(buf); + RAW_NULL_CHECK(tvalue); taosMemoryFree(buf); } else { double val = 0; GET_TYPED_DATA(val, double, pTagVal->type, &pTagVal->i64); tvalue = cJSON_CreateNumber(val); + RAW_NULL_CHECK(tvalue); } - cJSON_AddItemToObject(tag, "value", tvalue); - cJSON_AddItemToArray(tags, tag); + RAW_FALSE_CHECK(cJSON_AddItemToObject(tag, "value", tvalue)); + RAW_FALSE_CHECK(cJSON_AddItemToArray(tags, tag)); } end: - cJSON_AddItemToObject(json, "tags", tags); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tags", tags)); taosArrayDestroy(pTagVals); } -static cJSON* buildCreateCTableJson(SVCreateTbReq* pCreateReq, int32_t nReqs) { +static void buildCreateCTableJson(SVCreateTbReq* pCreateReq, int32_t nReqs, cJSON** pJson) { + int32_t code = 0; char* string = NULL; cJSON* json = cJSON_CreateObject(); - if (json == NULL) { - uError("create json object failed"); - return NULL; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("create"); - cJSON_AddItemToObject(json, "type", type); - // char cid[32] = {0}; - // sprintf(cid, "%"PRIi64, id); - // cJSON* cid_ = cJSON_CreateString(cid); - // cJSON_AddItemToObject(json, "id", cid_); + RAW_NULL_CHECK(type); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); cJSON* tableType = cJSON_CreateString("child"); - cJSON_AddItemToObject(json, "tableType", tableType); + RAW_NULL_CHECK(tableType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType)); buildChildElement(json, pCreateReq); cJSON* createList = cJSON_CreateArray(); + RAW_NULL_CHECK(createList); for (int i = 0; nReqs > 1 && i < nReqs; i++) { cJSON* create = cJSON_CreateObject(); + RAW_NULL_CHECK(create); buildChildElement(create, pCreateReq + i); - cJSON_AddItemToArray(createList, create); + RAW_FALSE_CHECK(cJSON_AddItemToArray(createList, create)); } - cJSON_AddItemToObject(json, "createList", createList); - return json; + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "createList", createList)); + +end: + *pJson = json; } -static cJSON* processCreateTable(SMqMetaRsp* metaRsp) { +static void processCreateTable(SMqMetaRsp* metaRsp, cJSON** pJson) { SDecoder decoder = {0}; SVCreateTbBatchReq req = {0}; SVCreateTbReq* pCreateReq; - cJSON* pJson = NULL; // decode uDebug("create table data:%p", metaRsp); void* data = POINTER_SHIFT(metaRsp->metaRsp, sizeof(SMsgHead)); int32_t len = metaRsp->metaRspLen - sizeof(SMsgHead); tDecoderInit(&decoder, data, len); if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) { - goto _exit; + goto end; } // loop to create table if (req.nReqs > 0) { pCreateReq = req.pReqs; if (pCreateReq->type == TSDB_CHILD_TABLE) { - pJson = buildCreateCTableJson(req.pReqs, req.nReqs); + buildCreateCTableJson(req.pReqs, req.nReqs, pJson); } else if (pCreateReq->type == TSDB_NORMAL_TABLE) { - pJson = buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->name, pCreateReq->uid, - TSDB_NORMAL_TABLE, &pCreateReq->colCmpr); + buildCreateTableJson(&pCreateReq->ntb.schemaRow, NULL, pCreateReq->name, pCreateReq->uid, + TSDB_NORMAL_TABLE, &pCreateReq->colCmpr, pJson); } } -_exit: - uDebug("create table return, sql json:%s", cJSON_PrintUnformatted(pJson)); +end: + uDebug("create table return, sql json:%s", cJSON_PrintUnformatted(*pJson)); tDeleteSVCreateTbBatchReq(&req); tDecoderClear(&decoder); - return pJson; } -static char* processAutoCreateTable(STaosxRsp* rsp) { +static void processAutoCreateTable(STaosxRsp* rsp, char** string) { + SDecoder* decoder = NULL; + SVCreateTbReq* pCreateReq = NULL; + int32_t code = 0; uDebug("auto create table data:%p", rsp); if (rsp->createTableNum <= 0) { uError("processAutoCreateTable rsp->createTableNum <= 0"); - goto _exit; + goto end; } - SDecoder* decoder = taosMemoryCalloc(rsp->createTableNum, sizeof(SDecoder)); - SVCreateTbReq* pCreateReq = taosMemoryCalloc(rsp->createTableNum, sizeof(SVCreateTbReq)); - char* string = NULL; + decoder = taosMemoryCalloc(rsp->createTableNum, sizeof(SDecoder)); + RAW_NULL_CHECK(decoder); + pCreateReq = taosMemoryCalloc(rsp->createTableNum, sizeof(SVCreateTbReq)); + RAW_NULL_CHECK(pCreateReq); // loop to create table for (int32_t iReq = 0; iReq < rsp->createTableNum; iReq++) { // decode void** data = taosArrayGet(rsp->createTableReq, iReq); + RAW_NULL_CHECK(data); int32_t* len = taosArrayGet(rsp->createTableLen, iReq); + RAW_NULL_CHECK(len); tDecoderInit(&decoder[iReq], *data, *len); if (tDecodeSVCreateTbReq(&decoder[iReq], pCreateReq + iReq) < 0) { - goto _exit; + goto end; } if (pCreateReq[iReq].type != TSDB_CHILD_TABLE) { uError("processAutoCreateTable pCreateReq[iReq].type != TSDB_CHILD_TABLE"); - goto _exit; + goto end; } } cJSON* pJson = buildCreateCTableJson(pCreateReq, rsp->createTableNum); - string = cJSON_PrintUnformatted(pJson); + *string = cJSON_PrintUnformatted(pJson); cJSON_Delete(pJson); -_exit: - uDebug("auto created table return, sql json:%s", string); - for (int i = 0; i < rsp->createTableNum; i++) { + +end: + uDebug("auto created table return, sql json:%s", *string); + for (int i = 0; decoder && pCreateReq && i < rsp->createTableNum; i++) { tDecoderClear(&decoder[i]); taosMemoryFreeClear(pCreateReq[i].comment); if (pCreateReq[i].type == TSDB_CHILD_TABLE) { - taosArrayDestroy(pCreateReq[i].ctb.tagName); + (void)taosArrayDestroy(pCreateReq[i].ctb.tagName); } } taosMemoryFree(decoder); taosMemoryFree(pCreateReq); - return string; } static cJSON* processAlterTable(SMqMetaRsp* metaRsp) { @@ -513,6 +593,7 @@ static cJSON* processAlterTable(SMqMetaRsp* metaRsp) { SVAlterTbReq vAlterTbReq = {0}; char* string = NULL; cJSON* json = NULL; + int32_t code = 0; uDebug("alter table data:%p", metaRsp); // decode @@ -521,95 +602,110 @@ static cJSON* processAlterTable(SMqMetaRsp* metaRsp) { tDecoderInit(&decoder, data, len); if (tDecodeSVAlterTbReq(&decoder, &vAlterTbReq) < 0) { uError("tDecodeSVAlterTbReq error"); - goto _exit; + goto end; } json = cJSON_CreateObject(); - if (json == NULL) { - uError("create json object failed"); - goto _exit; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("alter"); - cJSON_AddItemToObject(json, "type", type); - // cJSON* uid = cJSON_CreateNumber(id); - // cJSON_AddItemToObject(json, "uid", uid); + RAW_NULL_CHECK(type); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); cJSON* tableType = cJSON_CreateString(vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ? "child" : "normal"); - cJSON_AddItemToObject(json, "tableType", tableType); + RAW_NULL_CHECK(tableType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType)); cJSON* tableName = cJSON_CreateString(vAlterTbReq.tbName); - cJSON_AddItemToObject(json, "tableName", tableName); + RAW_NULL_CHECK(tableName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName)); cJSON* alterType = cJSON_CreateNumber(vAlterTbReq.action); - cJSON_AddItemToObject(json, "alterType", alterType); + RAW_NULL_CHECK(alterType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "alterType", alterType)); switch (vAlterTbReq.action) { case TSDB_ALTER_TABLE_ADD_COLUMN: { cJSON* colName = cJSON_CreateString(vAlterTbReq.colName); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type); - cJSON_AddItemToObject(json, "colType", colType); + RAW_NULL_CHECK(colType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType)); if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY || vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } break; } case TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION: { cJSON* colName = cJSON_CreateString(vAlterTbReq.colName); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colType = cJSON_CreateNumber(vAlterTbReq.type); - cJSON_AddItemToObject(json, "colType", colType); + RAW_NULL_CHECK(colType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType)); if (vAlterTbReq.type == TSDB_DATA_TYPE_BINARY || vAlterTbReq.type == TSDB_DATA_TYPE_VARBINARY || vAlterTbReq.type == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = vAlterTbReq.bytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } else if (vAlterTbReq.type == TSDB_DATA_TYPE_NCHAR) { int32_t length = (vAlterTbReq.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } - setCompressOption(json, vAlterTbReq.compress); + RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress)); break; } case TSDB_ALTER_TABLE_DROP_COLUMN: { cJSON* colName = cJSON_CreateString(vAlterTbReq.colName); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); break; } case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: { cJSON* colName = cJSON_CreateString(vAlterTbReq.colName); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colType = cJSON_CreateNumber(vAlterTbReq.colModType); - cJSON_AddItemToObject(json, "colType", colType); + RAW_NULL_CHECK(colType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colType", colType)); if (vAlterTbReq.colModType == TSDB_DATA_TYPE_BINARY || vAlterTbReq.colModType == TSDB_DATA_TYPE_VARBINARY || vAlterTbReq.colModType == TSDB_DATA_TYPE_GEOMETRY) { int32_t length = vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } else if (vAlterTbReq.colModType == TSDB_DATA_TYPE_NCHAR) { int32_t length = (vAlterTbReq.colModBytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE; cJSON* cbytes = cJSON_CreateNumber(length); - cJSON_AddItemToObject(json, "colLength", cbytes); + RAW_NULL_CHECK(cbytes); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colLength", cbytes)); } break; } case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: { cJSON* colName = cJSON_CreateString(vAlterTbReq.colName); - cJSON_AddItemToObject(json, "colName", colName); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); cJSON* colNewName = cJSON_CreateString(vAlterTbReq.colNewName); - cJSON_AddItemToObject(json, "colNewName", colNewName); + RAW_NULL_CHECK(colNewName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colNewName", colNewName)); break; } case TSDB_ALTER_TABLE_UPDATE_TAG_VAL: { cJSON* tagName = cJSON_CreateString(vAlterTbReq.tagName); - cJSON_AddItemToObject(json, "colName", tagName); + RAW_NULL_CHECK(tagName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", tagName)); bool isNull = vAlterTbReq.isNull; if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) { @@ -622,47 +718,53 @@ static cJSON* processAlterTable(SMqMetaRsp* metaRsp) { if (vAlterTbReq.tagType == TSDB_DATA_TYPE_JSON) { if (!tTagIsJson(vAlterTbReq.pTagVal)) { uError("processAlterTable isJson false"); - goto _exit; + goto end; } - buf = parseTagDatatoJson(vAlterTbReq.pTagVal); + parseTagDatatoJson(vAlterTbReq.pTagVal, &buf); } else { if (vAlterTbReq.tagType == TSDB_DATA_TYPE_VARBINARY) { buf = taosMemoryCalloc(vAlterTbReq.nTagVal * 2 + 2 + 3, 1); } else { buf = taosMemoryCalloc(vAlterTbReq.nTagVal + 3, 1); } - dataConverToStr(buf, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL); + RAW_NULL_CHECK(buf); + RAW_RETURN_CHECK(dataConverToStr(buf, vAlterTbReq.tagType, vAlterTbReq.pTagVal, vAlterTbReq.nTagVal, NULL)); } cJSON* colValue = cJSON_CreateString(buf); - cJSON_AddItemToObject(json, "colValue", colValue); + RAW_NULL_CHECK(colValue); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colValue", colValue)); taosMemoryFree(buf); } cJSON* isNullCJson = cJSON_CreateBool(isNull); - cJSON_AddItemToObject(json, "colValueNull", isNullCJson); + RAW_NULL_CHECK(isNullCJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colValueNull", isNullCJson)); break; } case TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS: { cJSON* colName = cJSON_CreateString(vAlterTbReq.colName); - cJSON_AddItemToObject(json, "colName", colName); - setCompressOption(json, vAlterTbReq.compress); + RAW_NULL_CHECK(colName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "colName", colName)); + RAW_RETURN_CHECK(setCompressOption(json, vAlterTbReq.compress)); break; } default: break; } -_exit: +end: uDebug("alter table return, sql json:%s", cJSON_PrintUnformatted(json)); tDecoderClear(&decoder); return json; } -static cJSON* processDropSTable(SMqMetaRsp* metaRsp) { +static void processDropSTable(SMqMetaRsp* metaRsp, cJSON** pJson) { SDecoder decoder = {0}; SVDropStbReq req = {0}; cJSON* json = NULL; + int32_t code = 0; + uDebug("processDropSTable data:%p", metaRsp); // decode @@ -671,30 +773,31 @@ static cJSON* processDropSTable(SMqMetaRsp* metaRsp) { tDecoderInit(&decoder, data, len); if (tDecodeSVDropStbReq(&decoder, &req) < 0) { uError("tDecodeSVDropStbReq failed"); - goto _exit; + goto end; } json = cJSON_CreateObject(); - if (json == NULL) { - uError("create json object failed"); - goto _exit; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("drop"); - cJSON_AddItemToObject(json, "type", type); + RAW_NULL_CHECK(type); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); cJSON* tableType = cJSON_CreateString("super"); - cJSON_AddItemToObject(json, "tableType", tableType); + RAW_NULL_CHECK(tableType); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableType", tableType)); cJSON* tableName = cJSON_CreateString(req.name); - cJSON_AddItemToObject(json, "tableName", tableName); + RAW_NULL_CHECK(tableName); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableName", tableName)); -_exit: +end: uDebug("processDropSTable return, sql json:%s", cJSON_PrintUnformatted(json)); tDecoderClear(&decoder); - return json; + *pJson = json; } -static cJSON* processDeleteTable(SMqMetaRsp* metaRsp) { +static void processDeleteTable(SMqMetaRsp* metaRsp, cJSON** pJson) { SDeleteRes req = {0}; SDecoder coder = {0}; cJSON* json = NULL; + int32_t code = 0; uDebug("processDeleteTable data:%p", metaRsp); // decode and process req @@ -704,34 +807,34 @@ static cJSON* processDeleteTable(SMqMetaRsp* metaRsp) { tDecoderInit(&coder, data, len); if (tDecodeDeleteRes(&coder, &req) < 0) { uError("tDecodeDeleteRes failed"); - goto _exit; + goto end; } // getTbName(req.tableFName); char sql[256] = {0}; - snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName, + (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName, req.tsColName, req.skey, req.tsColName, req.ekey); json = cJSON_CreateObject(); - if (json == NULL) { - uError("creaet json object failed"); - goto _exit; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("delete"); - cJSON_AddItemToObject(json, "type", type); + RAW_NULL_CHECK(type); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); cJSON* sqlJson = cJSON_CreateString(sql); - cJSON_AddItemToObject(json, "sql", sqlJson); + RAW_NULL_CHECK(sqlJson); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "sql", sqlJson)); -_exit: +end: uDebug("processDeleteTable return, sql json:%s", cJSON_PrintUnformatted(json)); tDecoderClear(&coder); - return json; + *pJson = json; } -static cJSON* processDropTable(SMqMetaRsp* metaRsp) { +static void processDropTable(SMqMetaRsp* metaRsp, cJSON** pJson) { SDecoder decoder = {0}; SVDropTbBatchReq req = {0}; cJSON* json = NULL; + int32_t code = 0; uDebug("processDropTable data:%p", metaRsp); // decode @@ -740,40 +843,33 @@ static cJSON* processDropTable(SMqMetaRsp* metaRsp) { tDecoderInit(&decoder, data, len); if (tDecodeSVDropTbBatchReq(&decoder, &req) < 0) { uError("tDecodeSVDropTbBatchReq failed"); - goto _exit; + goto end; } json = cJSON_CreateObject(); - if (json == NULL) { - uError("create json object failed"); - goto _exit; - } + RAW_NULL_CHECK(json); cJSON* type = cJSON_CreateString("drop"); - cJSON_AddItemToObject(json, "type", type); - // cJSON* uid = cJSON_CreateNumber(id); - // cJSON_AddItemToObject(json, "uid", uid); - // cJSON* tableType = cJSON_CreateString("normal"); - // cJSON_AddItemToObject(json, "tableType", tableType); - + RAW_NULL_CHECK(type); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "type", type)); cJSON* tableNameList = cJSON_CreateArray(); + RAW_NULL_CHECK(tableNameList); for (int32_t iReq = 0; iReq < req.nReqs; iReq++) { SVDropTbReq* pDropTbReq = req.pReqs + iReq; - cJSON* tableName = cJSON_CreateString(pDropTbReq->name); - cJSON_AddItemToArray(tableNameList, tableName); + RAW_NULL_CHECK(tableName); + RAW_FALSE_CHECK(cJSON_AddItemToArray(tableNameList, tableName)); } - cJSON_AddItemToObject(json, "tableNameList", tableNameList); + RAW_FALSE_CHECK(cJSON_AddItemToObject(json, "tableNameList", tableNameList)); -_exit: +end: uDebug("processDropTable return, json sql:%s", cJSON_PrintUnformatted(json)); tDecoderClear(&decoder); - return json; + *pJson = json; } static int32_t taosCreateStb(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SVCreateStbReq req = {0}; SDecoder coder; @@ -781,11 +877,7 @@ static int32_t taosCreateStb(TAOS* taos, void* meta, int32_t metaLen) { int32_t code = TSDB_CODE_SUCCESS; SRequestObj* pRequest = NULL; - code = buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - return code; - } + RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0)); uDebug(LOG_ID_TAG " create stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen); pRequest->syncQuery = true; if (!pRequest->pDb) { @@ -808,25 +900,27 @@ static int32_t taosCreateStb(TAOS* taos, void* meta, int32_t metaLen) { } // build create stable pReq.pColumns = taosArrayInit(req.schemaRow.nCols, sizeof(SFieldWithOptions)); + RAW_NULL_CHECK (pReq.pColumns); for (int32_t i = 0; i < req.schemaRow.nCols; i++) { SSchema* pSchema = req.schemaRow.pSchema + i; SFieldWithOptions field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes}; - strcpy(field.name, pSchema->name); + (void)strcpy(field.name, pSchema->name); if (createDefaultCompress) { field.compress = createDefaultColCmprByType(pSchema->type); } else { - SColCmpr* p = &req.colCmpr.pColCmpr[i]; - field.compress = p->alg; + SColCmpr* pCmp = &req.colCmpr.pColCmpr[i]; + field.compress = pCmp->alg; } - taosArrayPush(pReq.pColumns, &field); + RAW_NULL_CHECK(taosArrayPush(pReq.pColumns, &field)); } pReq.pTags = taosArrayInit(req.schemaTag.nCols, sizeof(SField)); + RAW_NULL_CHECK(pReq.pTags); for (int32_t i = 0; i < req.schemaTag.nCols; i++) { SSchema* pSchema = req.schemaTag.pSchema + i; SField field = {.type = pSchema->type, .flags = pSchema->flags, .bytes = pSchema->bytes}; - strcpy(field.name, pSchema->name); - taosArrayPush(pReq.pTags, &field); + (void)strcpy(field.name, pSchema->name); + RAW_NULL_CHECK(taosArrayPush(pReq.pTags, &field)); } pReq.colVer = req.schemaRow.version; @@ -841,19 +935,22 @@ static int32_t taosCreateStb(TAOS* taos, void* meta, int32_t metaLen) { uDebug(LOG_ID_TAG " create stable name:%s suid:%" PRId64 " processSuid:%" PRId64, LOG_ID_VALUE, req.name, req.suid, pReq.suid); STscObj* pTscObj = pRequest->pTscObj; - SName tableName; - tNameExtractFullName(toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName), pReq.name); - + SName tableName = {0}; + RAW_RETURN_CHECK(tNameExtractFullName(toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName), pReq.name)); SCmdMsgInfo pCmdMsg = {0}; pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp); pCmdMsg.msgType = TDMT_MND_CREATE_STB; pCmdMsg.msgLen = tSerializeSMCreateStbReq(NULL, 0, &pReq); - pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen); - if (NULL == pCmdMsg.pMsg) { - code = TSDB_CODE_OUT_OF_MEMORY; + if (pCmdMsg.msgLen <= 0) { + code = TSDB_CODE_INVALID_PARA; + goto end; + } + pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen); + RAW_NULL_CHECK(pCmdMsg.pMsg); + if (tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0){ + code = TSDB_CODE_INVALID_PARA; goto end; } - tSerializeSMCreateStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq); SQuery pQuery = {0}; pQuery.execMode = QUERY_EXEC_MODE_RPC; @@ -861,12 +958,13 @@ static int32_t taosCreateStb(TAOS* taos, void* meta, int32_t metaLen) { pQuery.msgType = pQuery.pCmdMsg->msgType; pQuery.stableQuery = true; - launchQueryImpl(pRequest, &pQuery, true, NULL); + (void)launchQueryImpl(pRequest, &pQuery, true, NULL); //ignore, because return value is pRequest if (pRequest->code == TSDB_CODE_SUCCESS) { SCatalog* pCatalog = NULL; - catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); - catalogRemoveTableMeta(pCatalog, &tableName); + // ignore the return value + (void)catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); + (void)catalogRemoveTableMeta(pCatalog, &tableName); } code = pRequest->code; @@ -877,14 +975,12 @@ end: destroyRequest(pRequest); tFreeSMCreateStbReq(&pReq); tDecoderClear(&coder); - terrno = code; return code; } static int32_t taosDropStb(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SVDropStbReq req = {0}; SDecoder coder = {0}; @@ -892,12 +988,7 @@ static int32_t taosDropStb(TAOS* taos, void* meta, int32_t metaLen) { int32_t code = TSDB_CODE_SUCCESS; SRequestObj* pRequest = NULL; - code = buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - return code; - } - + RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0)); uDebug(LOG_ID_TAG " drop stable, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen); pRequest->syncQuery = true; if (!pRequest->pDb) { @@ -914,16 +1005,13 @@ static int32_t taosDropStb(TAOS* taos, void* meta, int32_t metaLen) { } SCatalog* pCatalog = NULL; - code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog)); SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self, .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)}; SName pName = {0}; - toName(pRequest->pTscObj->acctId, pRequest->pDb, req.name, &pName); + (void)toName(pRequest->pTscObj->acctId, pRequest->pDb, req.name, &pName); // ignore the return value, always return pName STableMeta* pTableMeta = NULL; code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta); if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) { @@ -946,18 +1034,26 @@ static int32_t taosDropStb(TAOS* taos, void* meta, int32_t metaLen) { pReq.suid); STscObj* pTscObj = pRequest->pTscObj; SName tableName = {0}; - tNameExtractFullName(toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName), pReq.name); + if (tNameExtractFullName(toName(pTscObj->acctId, pRequest->pDb, req.name, &tableName), pReq.name) != 0) { + code = TSDB_CODE_INVALID_PARA; + goto end; + } SCmdMsgInfo pCmdMsg = {0}; pCmdMsg.epSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp); pCmdMsg.msgType = TDMT_MND_DROP_STB; pCmdMsg.msgLen = tSerializeSMDropStbReq(NULL, 0, &pReq); - pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen); - if (NULL == pCmdMsg.pMsg) { - code = TSDB_CODE_OUT_OF_MEMORY; + if (pCmdMsg.msgLen <= 0) { + code = TSDB_CODE_INVALID_PARA; goto end; } - tSerializeSMDropStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq); + pCmdMsg.pMsg = taosMemoryMalloc(pCmdMsg.msgLen); + RAW_NULL_CHECK(pCmdMsg.pMsg); + if (tSerializeSMDropStbReq(pCmdMsg.pMsg, pCmdMsg.msgLen, &pReq) <= 0){ + code = TSDB_CODE_INVALID_PARA; + goto end; + } + SQuery pQuery = {0}; pQuery.execMode = QUERY_EXEC_MODE_RPC; @@ -965,12 +1061,12 @@ static int32_t taosDropStb(TAOS* taos, void* meta, int32_t metaLen) { pQuery.msgType = pQuery.pCmdMsg->msgType; pQuery.stableQuery = true; - launchQueryImpl(pRequest, &pQuery, true, NULL); + (void)launchQueryImpl(pRequest, &pQuery, true, NULL); //ignore, because return value is pRequest if (pRequest->code == TSDB_CODE_SUCCESS) { - // SCatalog* pCatalog = NULL; - catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); - catalogRemoveTableMeta(pCatalog, &tableName); + // ignore the error code + (void)catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); + (void)catalogRemoveTableMeta(pCatalog, &tableName); } code = pRequest->code; @@ -980,7 +1076,6 @@ end: uDebug(LOG_ID_TAG " drop stable return, msg:%s", LOG_ID_VALUE, tstrerror(code)); destroyRequest(pRequest); tDecoderClear(&coder); - terrno = code; return code; } @@ -992,13 +1087,12 @@ typedef struct SVgroupCreateTableBatch { static void destroyCreateTbReqBatch(void* data) { SVgroupCreateTableBatch* pTbBatch = (SVgroupCreateTableBatch*)data; - taosArrayDestroy(pTbBatch->req.pArray); + (void)taosArrayDestroy(pTbBatch->req.pArray); } static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SVCreateTbBatchReq req = {0}; SDecoder coder = {0}; @@ -1007,12 +1101,7 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { SQuery* pQuery = NULL; SHashObj* pVgroupHashmap = NULL; - code = buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - return code; - } - + RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0)); uDebug(LOG_ID_TAG " create table, meta:%p, metaLen:%d", LOG_ID_VALUE, meta, metaLen); pRequest->syncQuery = true; @@ -1033,16 +1122,9 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { SVCreateTbReq* pCreateReq = NULL; SCatalog* pCatalog = NULL; - code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - + RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog)); pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); - if (NULL == pVgroupHashmap) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pVgroupHashmap); taosHashSetFreeFp(pVgroupHashmap, destroyCreateTbReqBatch); SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter, @@ -1051,13 +1133,14 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)}; pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName)); + RAW_NULL_CHECK(pRequest->tableList); // loop to create table for (int32_t iReq = 0; iReq < req.nReqs; iReq++) { pCreateReq = req.pReqs + iReq; SVgroupInfo pInfo = {0}; SName pName = {0}; - toName(pTscObj->acctId, pRequest->pDb, pCreateReq->name, &pName); + (void)toName(pTscObj->acctId, pRequest->pDb, pCreateReq->name, &pName); code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo); if (code != TSDB_CODE_SUCCESS) { goto end; @@ -1070,7 +1153,7 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { SName sName = {0}; tb_uid_t oldSuid = pCreateReq->ctb.suid; // pCreateReq->ctb.suid = processSuid(pCreateReq->ctb.suid, pRequest->pDb); - toName(pTscObj->acctId, pRequest->pDb, pCreateReq->ctb.stbName, &sName); + (void)toName(pTscObj->acctId, pRequest->pDb, pCreateReq->ctb.stbName, &sName); code = catalogGetTableMeta(pCatalog, &conn, &sName, &pTableMeta); if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) { code = TSDB_CODE_SUCCESS; @@ -1085,6 +1168,9 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { for (int32_t i = 0; i < taosArrayGetSize(pCreateReq->ctb.tagName); i++) { char* tName = taosArrayGet(pCreateReq->ctb.tagName, i); + if (tName == NULL) { + continue; + } for (int32_t j = pTableMeta->tableInfo.numOfColumns; j < pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags; j++) { SSchema* tag = &pTableMeta->schema[j]; @@ -1095,21 +1181,21 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { } taosMemoryFreeClear(pTableMeta); } - taosArrayPush(pRequest->tableList, &pName); + RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName)); SVgroupCreateTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId)); if (pTableBatch == NULL) { SVgroupCreateTableBatch tBatch = {0}; tBatch.info = pInfo; - strcpy(tBatch.dbName, pRequest->pDb); + (void)strcpy(tBatch.dbName, pRequest->pDb); tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq)); - taosArrayPush(tBatch.req.pArray, pCreateReq); + RAW_NULL_CHECK(tBatch.req.pArray); + RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pCreateReq)); tBatch.req.source = TD_REQ_FROM_TAOX; - - taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)); + RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch))); } else { // add to the correct vgroup - taosArrayPush(pTableBatch->req.pArray, pCreateReq); + RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pCreateReq)); } } @@ -1117,25 +1203,21 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { goto end; } SArray* pBufArray = serializeVgroupsCreateTableBatch(pVgroupHashmap); - if (NULL == pBufArray) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pBufArray); pQuery = (SQuery*)nodesMakeNode(QUERY_NODE_QUERY); + RAW_NULL_CHECK(pQuery); pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE; pQuery->msgType = TDMT_VND_CREATE_TABLE; pQuery->stableQuery = false; pQuery->pRoot = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT); + RAW_NULL_CHECK(pQuery->pRoot); - code = rewriteToVnodeModifyOpStmt(pQuery, pBufArray); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray)); - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); if (pRequest->code == TSDB_CODE_SUCCESS) { - removeMeta(pTscObj, pRequest->tableList, false); + (void)removeMeta(pTscObj, pRequest->tableList, false); } code = pRequest->code; @@ -1148,7 +1230,6 @@ end: destroyRequest(pRequest); tDecoderClear(&coder); qDestroyQuery(pQuery); - terrno = code; return code; } @@ -1160,13 +1241,12 @@ typedef struct SVgroupDropTableBatch { static void destroyDropTbReqBatch(void* data) { SVgroupDropTableBatch* pTbBatch = (SVgroupDropTableBatch*)data; - taosArrayDestroy(pTbBatch->req.pArray); + (void)taosArrayDestroy(pTbBatch->req.pArray); } static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SVDropTbBatchReq req = {0}; SDecoder coder = {0}; @@ -1175,11 +1255,7 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { SQuery* pQuery = NULL; SHashObj* pVgroupHashmap = NULL; - code = buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - return code; - } + RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0)); uDebug(LOG_ID_TAG " drop table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen); pRequest->syncQuery = true; @@ -1200,16 +1276,10 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { SVDropTbReq* pDropReq = NULL; SCatalog* pCatalog = NULL; - code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog)); pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); - if (NULL == pVgroupHashmap) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pVgroupHashmap); taosHashSetFreeFp(pVgroupHashmap, destroyDropTbReqBatch); SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter, @@ -1217,6 +1287,7 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { .requestObjRefId = pRequest->self, .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)}; pRequest->tableList = taosArrayInit(req.nReqs, sizeof(SName)); + RAW_NULL_CHECK(pRequest->tableList); // loop to create table for (int32_t iReq = 0; iReq < req.nReqs; iReq++) { pDropReq = req.pReqs + iReq; @@ -1225,11 +1296,8 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { SVgroupInfo pInfo = {0}; SName pName = {0}; - toName(pTscObj->acctId, pRequest->pDb, pDropReq->name, &pName); - code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + (void)toName(pTscObj->acctId, pRequest->pDb, pDropReq->name, &pName); + RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo)); STableMeta* pTableMeta = NULL; code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta); @@ -1247,17 +1315,17 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { uDebug(LOG_ID_TAG " drop table name:%s suid:%" PRId64 " new suid:%" PRId64, LOG_ID_VALUE, pDropReq->name, oldSuid, pDropReq->suid); - taosArrayPush(pRequest->tableList, &pName); + RAW_NULL_CHECK(taosArrayPush(pRequest->tableList, &pName)); SVgroupDropTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId)); if (pTableBatch == NULL) { SVgroupDropTableBatch tBatch = {0}; tBatch.info = pInfo; tBatch.req.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVDropTbReq)); - taosArrayPush(tBatch.req.pArray, pDropReq); - - taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch)); + RAW_NULL_CHECK(tBatch.req.pArray); + RAW_NULL_CHECK(taosArrayPush(tBatch.req.pArray, pDropReq)); + RAW_RETURN_CHECK(taosHashPut(pVgroupHashmap, &pInfo.vgId, sizeof(pInfo.vgId), &tBatch, sizeof(tBatch))); } else { // add to the correct vgroup - taosArrayPush(pTableBatch->req.pArray, pDropReq); + RAW_NULL_CHECK(taosArrayPush(pTableBatch->req.pArray, pDropReq)); } } @@ -1265,25 +1333,20 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { goto end; } SArray* pBufArray = serializeVgroupsDropTableBatch(pVgroupHashmap); - if (NULL == pBufArray) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pBufArray); pQuery = (SQuery*)nodesMakeNode(QUERY_NODE_QUERY); + RAW_NULL_CHECK(pQuery); pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE; pQuery->msgType = TDMT_VND_DROP_TABLE; pQuery->stableQuery = false; pQuery->pRoot = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT); + RAW_NULL_CHECK(pQuery->pRoot); + RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pBufArray)); - code = rewriteToVnodeModifyOpStmt(pQuery, pBufArray); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); if (pRequest->code == TSDB_CODE_SUCCESS) { - removeMeta(pTscObj, pRequest->tableList, false); + (void)removeMeta(pTscObj, pRequest->tableList, false); } code = pRequest->code; @@ -1293,46 +1356,12 @@ end: destroyRequest(pRequest); tDecoderClear(&coder); qDestroyQuery(pQuery); - terrno = code; return code; } -// delete from db.tabl where .. -> delete from tabl where .. -// delete from db .tabl where .. -> delete from tabl where .. -// static void getTbName(char *sql){ -// char *ch = sql; -// -// bool inBackQuote = false; -// int8_t dotIndex = 0; -// while(*ch != '\0'){ -// if(!inBackQuote && *ch == '`'){ -// inBackQuote = true; -// ch++; -// continue; -// } -// -// if(inBackQuote && *ch == '`'){ -// inBackQuote = false; -// ch++; -// -// continue; -// } -// -// if(!inBackQuote && *ch == '.'){ -// dotIndex ++; -// if(dotIndex == 2){ -// memmove(sql, ch + 1, strlen(ch + 1) + 1); -// break; -// } -// } -// ch++; -// } -//} - static int32_t taosDeleteData(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SDeleteRes req = {0}; SDecoder coder = {0}; @@ -1350,11 +1379,11 @@ static int32_t taosDeleteData(TAOS* taos, void* meta, int32_t metaLen) { goto end; } - // getTbName(req.tableFName); - snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName, + (void)snprintf(sql, sizeof(sql), "delete from `%s` where `%s` >= %" PRId64 " and `%s` <= %" PRId64, req.tableFName, req.tsColName, req.skey, req.tsColName, req.ekey); TAOS_RES* res = taosQueryImpl(taos, sql, false, TD_REQ_FROM_TAOX); + RAW_NULL_CHECK(res); SRequestObj* pRequest = (SRequestObj*)res; code = pRequest->code; if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_PAR_GET_META_ERROR) { @@ -1365,14 +1394,12 @@ static int32_t taosDeleteData(TAOS* taos, void* meta, int32_t metaLen) { end: uDebug("connId:0x%" PRIx64 " delete data sql:%s, code:%s", *(int64_t*)taos, sql, tstrerror(code)); tDecoderClear(&coder); - terrno = code; return code; } static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SVAlterTbReq req = {0}; SDecoder dcoder = {0}; @@ -1382,11 +1409,7 @@ static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { SArray* pArray = NULL; SVgDataBlocks* pVgData = NULL; - code = buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0); - if (code != TSDB_CODE_SUCCESS) { - terrno = code; - return code; - } + RAW_RETURN_CHECK(buildRequest(*(int64_t*)taos, "", 0, NULL, false, &pRequest, 0)); uDebug(LOG_ID_TAG " alter table, meta:%p, len:%d", LOG_ID_VALUE, meta, metaLen); pRequest->syncQuery = true; if (!pRequest->pDb) { @@ -1409,11 +1432,7 @@ static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { STscObj* pTscObj = pRequest->pTscObj; SCatalog* pCatalog = NULL; - code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - + RAW_RETURN_CHECK(catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog)); SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self, @@ -1421,23 +1440,13 @@ static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { SVgroupInfo pInfo = {0}; SName pName = {0}; - toName(pTscObj->acctId, pRequest->pDb, req.tbName, &pName); - code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - + (void)toName(pTscObj->acctId, pRequest->pDb, req.tbName, &pName); + RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &pInfo)); pArray = taosArrayInit(1, sizeof(void*)); - if (NULL == pArray) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pArray); pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks)); - if (NULL == pVgData) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pVgData); pVgData->vg = pInfo; int tlen = 0; @@ -1449,10 +1458,7 @@ static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { } tlen += sizeof(SMsgHead); void* pMsg = taosMemoryMalloc(tlen); - if (NULL == pMsg) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pMsg); ((SMsgHead*)pMsg)->vgId = htonl(pInfo.vgId); ((SMsgHead*)pMsg)->contLen = htonl(tlen); void* pBuf = POINTER_SHIFT(pMsg, sizeof(SMsgHead)); @@ -1470,24 +1476,19 @@ static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { pVgData->size = tlen; pVgData->numOfTables = 1; - taosArrayPush(pArray, &pVgData); + RAW_NULL_CHECK(taosArrayPush(pArray, &pVgData)); pQuery = (SQuery*)nodesMakeNode(QUERY_NODE_QUERY); - if (NULL == pQuery) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pQuery); pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE; pQuery->msgType = TDMT_VND_ALTER_TABLE; pQuery->stableQuery = false; pQuery->pRoot = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT); + RAW_NULL_CHECK(pQuery->pRoot); + RAW_RETURN_CHECK(rewriteToVnodeModifyOpStmt(pQuery, pArray)); - code = rewriteToVnodeModifyOpStmt(pQuery, pArray); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); pVgData = NULL; pArray = NULL; @@ -1504,13 +1505,12 @@ static int32_t taosAlterTable(TAOS* taos, void* meta, int32_t metaLen) { } end: uDebug(LOG_ID_TAG " alter table return, meta:%p, len:%d, msg:%s", LOG_ID_VALUE, meta, metaLen, tstrerror(code)); - taosArrayDestroy(pArray); + (void)taosArrayDestroy(pArray); if (pVgData) taosMemoryFreeClear(pVgData->pData); taosMemoryFreeClear(pVgData); destroyRequest(pRequest); tDecoderClear(&dcoder); qDestroyQuery(pQuery); - terrno = code; return code; } @@ -1522,8 +1522,7 @@ int taos_write_raw_block_with_fields(TAOS* taos, int rows, char* pData, const ch int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname, TAOS_FIELD* fields, int numFields, int64_t reqid) { if (!taos || !pData || !tbname) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } int32_t code = TSDB_CODE_SUCCESS; STableMeta* pTableMeta = NULL; @@ -1531,9 +1530,7 @@ int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pDat SHashObj* pVgHash = NULL; SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, reqid); - if (!pRequest) { - return terrno; - } + RAW_NULL_CHECK(pRequest); uDebug(LOG_ID_TAG " write raw block with field, rows:%d, pData:%p, tbname:%s, fields:%p, numFields:%d", LOG_ID_VALUE, rows, pData, tbname, fields, numFields); @@ -1549,10 +1546,7 @@ int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pDat tstrncpy(pName.tname, tbname, sizeof(pName.tname)); struct SCatalog* pCatalog = NULL; - code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog)); SRequestConnInfo conn = {0}; conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter; @@ -1561,36 +1555,18 @@ int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pDat conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp); SVgroupInfo vgData = {0}; - code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - // uError("td23101 0vgId:%d, vgId:%d, name:%s, uid:%"PRIu64, vgData.vgId, pTableMeta->vgId, tbname, pTableMeta->uid); + RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData)); + RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta)); pQuery = smlInitHandle(); - if (pQuery == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pQuery); pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); - taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)); + RAW_NULL_CHECK(pVgHash); + RAW_RETURN_CHECK(taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData))); + RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, fields, numFields, false, NULL, 0)); + RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash)); - code = rawBlockBindData(pQuery, pTableMeta, pData, NULL, fields, numFields, false, NULL, 0); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - code = smlBuildOutput(pQuery, pVgHash); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); code = pRequest->code; end: @@ -1599,7 +1575,6 @@ end: qDestroyQuery(pQuery); destroyRequest(pRequest); taosHashCleanup(pVgHash); - terrno = code; return code; } @@ -1609,8 +1584,7 @@ int taos_write_raw_block(TAOS* taos, int rows, char* pData, const char* tbname) int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const char* tbname, int64_t reqid) { if (!taos || !pData || !tbname) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } int32_t code = TSDB_CODE_SUCCESS; STableMeta* pTableMeta = NULL; @@ -1618,9 +1592,7 @@ int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const cha SHashObj* pVgHash = NULL; SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, reqid); - if (!pRequest) { - return terrno; - } + RAW_NULL_CHECK(pRequest); uDebug(LOG_ID_TAG " write raw block, rows:%d, pData:%p, tbname:%s", LOG_ID_VALUE, rows, pData, tbname); @@ -1635,10 +1607,7 @@ int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const cha tstrncpy(pName.tname, tbname, sizeof(pName.tname)); struct SCatalog* pCatalog = NULL; - code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog)); SRequestConnInfo conn = {0}; conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter; @@ -1647,34 +1616,17 @@ int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const cha conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp); SVgroupInfo vgData = {0}; - code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vgData)); + RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta)); pQuery = smlInitHandle(); - if (pQuery == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pRequest); pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); - taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData)); + RAW_NULL_CHECK(pVgHash); + RAW_RETURN_CHECK(taosHashPut(pVgHash, (const char*)&vgData.vgId, sizeof(vgData.vgId), (char*)&vgData, sizeof(vgData))); + RAW_RETURN_CHECK(rawBlockBindData(pQuery, pTableMeta, pData, NULL, NULL, 0, false, NULL, 0)); + RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash)); - code = rawBlockBindData(pQuery, pTableMeta, pData, NULL, NULL, 0, false, NULL, 0); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - code = smlBuildOutput(pQuery, pVgHash); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } - - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); code = pRequest->code; end: @@ -1683,7 +1635,6 @@ end: qDestroyQuery(pQuery); destroyRequest(pRequest); taosHashCleanup(pVgHash); - terrno = code; return code; } @@ -1701,9 +1652,8 @@ static void* getRawDataFromRes(void* pRetrieve) { static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) { if (taos == NULL || data == NULL) { - terrno = TSDB_CODE_INVALID_PARA; SET_ERROR_MSG("taos:%p or data:%p is NULL", taos, data); - return terrno; + return TSDB_CODE_INVALID_PARA; } int32_t code = TSDB_CODE_SUCCESS; SHashObj* pVgHash = NULL; @@ -1712,12 +1662,8 @@ static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) { SDecoder decoder = {0}; STableMeta* pTableMeta = NULL; - terrno = TSDB_CODE_SUCCESS; SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, 0); - if (!pRequest) { - SET_ERROR_MSG("pRequest is NULL"); - return terrno; - } + RAW_NULL_CHECK(pRequest); uDebug(LOG_ID_TAG " write raw data, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen); pRequest->syncQuery = true; @@ -1743,11 +1689,7 @@ static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) { } struct SCatalog* pCatalog = NULL; - code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("cata log get handle failed"); - goto end; - } + RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog)); SRequestConnInfo conn = {0}; conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter; @@ -1756,59 +1698,37 @@ static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) { conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp); pQuery = smlInitHandle(); - if (pQuery == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; - SET_ERROR_MSG("init sml handle failed"); - goto end; - } + RAW_NULL_CHECK(pQuery); pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); + RAW_NULL_CHECK(pVgHash); while (++rspObj.common.resIter < rspObj.rsp.common.blockNum) { void* pRetrieve = taosArrayGetP(rspObj.rsp.common.blockData, rspObj.common.resIter); + RAW_NULL_CHECK(pRetrieve); if (!rspObj.rsp.common.withSchema) { goto end; } const char* tbName = (const char*)taosArrayGetP(rspObj.rsp.common.blockTbName, rspObj.common.resIter); - if (!tbName) { - SET_ERROR_MSG("block tbname is null"); - code = TSDB_CODE_TMQ_INVALID_MSG; - goto end; - } + RAW_NULL_CHECK(tbName); SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}}; - strcpy(pName.dbname, pRequest->pDb); - strcpy(pName.tname, tbName); + (void)strcpy(pName.dbname, pRequest->pDb); + (void)strcpy(pName.tname, tbName); - code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta); - // if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) { - // uError("WriteRaw:catalogGetTableMeta table not exist. table name: %s", tbName); - // code = TSDB_CODE_SUCCESS; - // continue; - // } - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("cata log get table:%s meta failed", tbName); - goto end; - } + RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta)); - SVgroupInfo vg; - code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vg); - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("cata log get table:%s vgroup failed", tbName); - goto end; - } + SVgroupInfo vg = {0}; + RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vg)); void* hData = taosHashGet(pVgHash, &vg.vgId, sizeof(vg.vgId)); if (hData == NULL) { - taosHashPut(pVgHash, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg)); + RAW_RETURN_CHECK(taosHashPut(pVgHash, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg))); } SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.rsp.common.blockSchema, rspObj.common.resIter); + RAW_NULL_CHECK(pSW); TAOS_FIELD* fields = taosMemoryCalloc(pSW->nCols, sizeof(TAOS_FIELD)); - if (fields == NULL) { - SET_ERROR_MSG("calloc fields failed"); - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(fields); for (int i = 0; i < pSW->nCols; i++) { fields[i].type = pSW->pSchema[i].type; fields[i].bytes = pSW->pSchema[i].bytes; @@ -1825,13 +1745,9 @@ static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) { } } - code = smlBuildOutput(pQuery, pVgHash); - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("sml build output failed"); - goto end; - } + RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash)); - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); code = pRequest->code; end: @@ -1842,15 +1758,13 @@ end: destroyRequest(pRequest); taosHashCleanup(pVgHash); taosMemoryFreeClear(pTableMeta); - terrno = code; return code; } static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) { if (taos == NULL || data == NULL) { - terrno = TSDB_CODE_INVALID_PARA; SET_ERROR_MSG("taos:%p or data:%p is NULL", taos, data); - return terrno; + return TSDB_CODE_INVALID_PARA; } int32_t code = TSDB_CODE_SUCCESS; SHashObj* pVgHash = NULL; @@ -1860,12 +1774,9 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) STableMeta* pTableMeta = NULL; SVCreateTbReq* pCreateReqDst = NULL; - terrno = TSDB_CODE_SUCCESS; SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, 0); - if (!pRequest) { - SET_ERROR_MSG("pRequest is NULL"); - return terrno; - } + RAW_NULL_CHECK(pRequest); + uDebug(LOG_ID_TAG " write raw metadata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen); pRequest->syncQuery = true; rspObj.common.resIter = -1; @@ -1891,11 +1802,7 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) } struct SCatalog* pCatalog = NULL; - code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("cata log get handle failed"); - goto end; - } + RAW_RETURN_CHECK(catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog)); SRequestConnInfo conn = {0}; conn.pTrans = pRequest->pTscObj->pAppInfo->pTransporter; @@ -1904,16 +1811,14 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) conn.mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp); pQuery = smlInitHandle(); - if (pQuery == NULL) { - SET_ERROR_MSG("init sml handle failed"); - code = TSDB_CODE_OUT_OF_MEMORY; - goto end; - } + RAW_NULL_CHECK(pQuery); pVgHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); + RAW_NULL_CHECK(pVgHash); uDebug(LOG_ID_TAG " write raw metadata block num:%d", LOG_ID_VALUE, rspObj.rsp.common.blockNum); while (++rspObj.common.resIter < rspObj.rsp.common.blockNum) { void* pRetrieve = taosArrayGetP(rspObj.rsp.common.blockData, rspObj.common.resIter); + RAW_NULL_CHECK(pRetrieve); if (!rspObj.rsp.common.withSchema) { goto end; } @@ -1927,13 +1832,15 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) uDebug(LOG_ID_TAG " write raw metadata block tbname:%s", LOG_ID_VALUE, tbName); SName pName = {TSDB_TABLE_NAME_T, pRequest->pTscObj->acctId, {0}, {0}}; - strcpy(pName.dbname, pRequest->pDb); - strcpy(pName.tname, tbName); + (void)strcpy(pName.dbname, pRequest->pDb); + (void)strcpy(pName.tname, tbName); // find schema data info for (int j = 0; j < rspObj.rsp.createTableNum; j++) { void** dataTmp = taosArrayGet(rspObj.rsp.createTableReq, j); + RAW_NULL_CHECK(dataTmp); int32_t* lenTmp = taosArrayGet(rspObj.rsp.createTableLen, j); + RAW_NULL_CHECK(dataTmp); SDecoder decoderTmp = {0}; SVCreateTbReq pCreateReq = {0}; @@ -1954,8 +1861,7 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) goto end; } if (strcmp(tbName, pCreateReq.name) == 0) { - cloneSVreateTbReq(&pCreateReq, &pCreateReqDst); - // pCreateReqDst->ctb.suid = processSuid(pCreateReqDst->ctb.suid, pRequest->pDb); + RAW_RETURN_CHECK(cloneSVreateTbReq(&pCreateReq, &pCreateReqDst)); tDecoderClear(&decoderTmp); tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE); break; @@ -1964,26 +1870,12 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) tDestroySVCreateTbReq(&pCreateReq, TSDB_MSG_FLG_DECODE); } - SVgroupInfo vg; - code = catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vg); - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("cata log get table:%s vgroup failed", tbName); - goto end; - } - + SVgroupInfo vg = {0}; + RAW_RETURN_CHECK(catalogGetTableHashVgroup(pCatalog, &conn, &pName, &vg)); if (pCreateReqDst) { // change stable name to get meta - strcpy(pName.tname, pCreateReqDst->ctb.stbName); - } - code = catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta); - // if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) { - // uError("WriteRaw:catalogGetTableMeta table not exist. table name: %s", tbName); - // code = TSDB_CODE_SUCCESS; - // continue; - // } - if (code != TSDB_CODE_SUCCESS) { - SET_ERROR_MSG("cata log get table:%s meta failed", tbName); - goto end; + (void)strcpy(pName.tname, pCreateReqDst->ctb.stbName); } + RAW_RETURN_CHECK(catalogGetTableMeta(pCatalog, &conn, &pName, &pTableMeta)); if (pCreateReqDst) { pTableMeta->vgId = vg.vgId; @@ -1992,10 +1884,11 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) } void* hData = taosHashGet(pVgHash, &vg.vgId, sizeof(vg.vgId)); if (hData == NULL) { - taosHashPut(pVgHash, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg)); + RAW_RETURN_CHECK(taosHashPut(pVgHash, (const char*)&vg.vgId, sizeof(vg.vgId), (char*)&vg, sizeof(vg))); } SSchemaWrapper* pSW = (SSchemaWrapper*)taosArrayGetP(rspObj.rsp.common.blockSchema, rspObj.common.resIter); + RAW_NULL_CHECK(pSW); TAOS_FIELD* fields = taosMemoryCalloc(pSW->nCols, sizeof(TAOS_FIELD)); if (fields == NULL) { SET_ERROR_MSG("calloc fields failed"); @@ -2018,12 +1911,9 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) } } - code = smlBuildOutput(pQuery, pVgHash); - if (code != TSDB_CODE_SUCCESS) { - goto end; - } + RAW_RETURN_CHECK(smlBuildOutput(pQuery, pVgHash)); - launchQueryImpl(pRequest, pQuery, true, NULL); + (void)launchQueryImpl(pRequest, pQuery, true, NULL); code = pRequest->code; end: @@ -2038,67 +1928,70 @@ end: tdDestroySVCreateTbReq(pCreateReqDst); taosMemoryFree(pCreateReqDst); } - terrno = code; return code; } -static cJSON* processSimpleMeta(SMqMetaRsp* pMetaRsp) { +static void processSimpleMeta(SMqMetaRsp* pMetaRsp, cJSON** meta) { if (pMetaRsp->resMsgType == TDMT_VND_CREATE_STB) { - return processCreateStb(pMetaRsp); + processCreateStb(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_STB) { - return processAlterStb(pMetaRsp); + processAlterStb(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_STB) { - return processDropSTable(pMetaRsp); + processDropSTable(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_CREATE_TABLE) { - return processCreateTable(pMetaRsp); + processCreateTable(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_ALTER_TABLE) { - return processAlterTable(pMetaRsp); + processAlterTable(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) { - return processDropTable(pMetaRsp); + processDropTable(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_DROP_TABLE) { - return processDropTable(pMetaRsp); + processDropTable(pMetaRsp, meta); } else if (pMetaRsp->resMsgType == TDMT_VND_DELETE) { - return processDeleteTable(pMetaRsp); + processDeleteTable(pMetaRsp, meta); } - - return NULL; } -static char* processBatchMetaToJson(SMqBatchMetaRsp* pMsgRsp) { + +static void processBatchMetaToJson(SMqBatchMetaRsp* pMsgRsp, char** string) { SDecoder coder; SMqBatchMetaRsp rsp = {0}; + int32_t code = 0; tDecoderInit(&coder, pMsgRsp->pMetaBuff, pMsgRsp->metaBuffLen); if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) { - goto _end; + goto end; } cJSON* pJson = cJSON_CreateObject(); - cJSON_AddStringToObject(pJson, "tmq_meta_version", TMQ_META_VERSION); + RAW_NULL_CHECK(pJson); + RAW_FALSE_CHECK(cJSON_AddStringToObject(pJson, "tmq_meta_version", TMQ_META_VERSION)); cJSON* pMetaArr = cJSON_CreateArray(); + RAW_NULL_CHECK(pMetaArr); int32_t num = taosArrayGetSize(rsp.batchMetaReq); for (int32_t i = 0; i < num; i++) { - int32_t len = *(int32_t*)taosArrayGet(rsp.batchMetaLen, i); + int32_t* len = taosArrayGet(rsp.batchMetaLen, i); + RAW_NULL_CHECK(len); void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i); + RAW_NULL_CHECK(tmpBuf); SDecoder metaCoder = {0}; SMqMetaRsp metaRsp = {0}; - tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), len - sizeof(SMqRspHead)); + tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead)); if(tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0 ) { - goto _end; + goto end; } - cJSON* pItem = processSimpleMeta(&metaRsp); + cJSON* pItem = NULL; + processSimpleMeta(&metaRsp, &pItem); tDeleteMqMetaRsp(&metaRsp); - cJSON_AddItemToArray(pMetaArr, pItem); + RAW_FALSE_CHECK(cJSON_AddItemToArray(pMetaArr, pItem)); } - cJSON_AddItemToObject(pJson, "metas", pMetaArr); + RAW_FALSE_CHECK(cJSON_AddItemToObject(pJson, "metas", pMetaArr)); tDeleteMqBatchMetaRsp(&rsp); char* fullStr = cJSON_PrintUnformatted(pJson); cJSON_Delete(pJson); - return fullStr; + *string = fullStr; -_end: +end: cJSON_Delete(pJson); tDeleteMqBatchMetaRsp(&rsp); - return NULL; } char* tmq_get_json_meta(TAOS_RES* res) { @@ -2110,14 +2003,19 @@ char* tmq_get_json_meta(TAOS_RES* res) { if (TD_RES_TMQ_METADATA(res)) { SMqTaosxRspObj* pMetaDataRspObj = (SMqTaosxRspObj*)res; - return processAutoCreateTable(&pMetaDataRspObj->rsp); + char* string = NULL; + processAutoCreateTable(&pMetaDataRspObj->rsp, &string); + return string; } else if (TD_RES_TMQ_BATCH_META(res)) { SMqBatchMetaRspObj* pBatchMetaRspObj = (SMqBatchMetaRspObj*)res; - return processBatchMetaToJson(&pBatchMetaRspObj->rsp); + char* string = NULL; + processBatchMetaToJson(&pBatchMetaRspObj->rsp, &string); + return string; } SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res; - cJSON* pJson = processSimpleMeta(&pMetaRspObj->metaRsp); + cJSON* pJson = NULL; + processSimpleMeta(&pMetaRspObj->metaRsp, &pJson); char* string = cJSON_PrintUnformatted(pJson); cJSON_Delete(pJson); return string; @@ -2145,48 +2043,47 @@ static int32_t encodeMqDataRsp(__encode_func__* encodeFunc, void* rspObj, tmq_ra void* buf = NULL; tEncodeSize(encodeFunc, rspObj, len, code); if (code < 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto FAILED; } len += sizeof(int8_t) + sizeof(int32_t); buf = taosMemoryCalloc(1, len); if (buf == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; goto FAILED; } tEncoderInit(&encoder, buf, len); if (tEncodeI8(&encoder, MQ_DATA_RSP_VERSION) < 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto FAILED; } int32_t offsetLen = getOffSetLen(rspObj); if (offsetLen <= 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto FAILED; } if (tEncodeI32(&encoder, offsetLen) < 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto FAILED; } if (encodeFunc(&encoder, rspObj) < 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto FAILED; } tEncoderClear(&encoder); raw->raw = buf; raw->raw_len = len; - return 0; + return code; FAILED: tEncoderClear(&encoder); taosMemoryFree(buf); - return terrno; + return code; } int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) { if (!raw || !res) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } if (TD_RES_TMQ_META(res)) { SMqMetaRspObj* pMetaRspObj = (SMqMetaRspObj*)res; @@ -2196,18 +2093,20 @@ int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) { uDebug("tmq get raw type meta:%p", raw); } else if (TD_RES_TMQ(res)) { SMqRspObj* rspObj = ((SMqRspObj*)res); - if (encodeMqDataRsp(tEncodeMqDataRsp, &rspObj->rsp, raw) != 0) { + int32_t code = encodeMqDataRsp(tEncodeMqDataRsp, &rspObj->rsp, raw); + if (code != 0) { uError("tmq get raw type error:%d", terrno); - return terrno; + return code; } raw->raw_type = RES_TYPE__TMQ; uDebug("tmq get raw type data:%p", raw); } else if (TD_RES_TMQ_METADATA(res)) { SMqTaosxRspObj* rspObj = ((SMqTaosxRspObj*)res); - if (encodeMqDataRsp(tEncodeSTaosxRsp, &rspObj->rsp, raw) != 0) { + int32_t code = encodeMqDataRsp(tEncodeSTaosxRsp, &rspObj->rsp, raw); + if (code != 0) { uError("tmq get raw type error:%d", terrno); - return terrno; + return code; } raw->raw_type = RES_TYPE__TMQ_METADATA; uDebug("tmq get raw type metadata:%p", raw); @@ -2219,8 +2118,7 @@ int32_t tmq_get_raw(TAOS_RES* res, tmq_raw_data* raw) { uDebug("tmq get raw batch meta:%p", raw); } else { uError("tmq get raw error type:%d", *(int8_t*)res); - terrno = TSDB_CODE_TMQ_INVALID_MSG; - return terrno; + return TSDB_CODE_TMQ_INVALID_MSG; } return TSDB_CODE_SUCCESS; } @@ -2230,7 +2128,7 @@ void tmq_free_raw(tmq_raw_data raw) { if (raw.raw_type == RES_TYPE__TMQ || raw.raw_type == RES_TYPE__TMQ_METADATA) { taosMemoryFree(raw.raw); } - memset(terrMsg, 0, ERR_MSG_LEN); + (void)memset(terrMsg, 0, ERR_MSG_LEN); } static int32_t writeRawImpl(TAOS* taos, void* buf, uint32_t len, uint16_t type) { @@ -2268,39 +2166,39 @@ int32_t tmq_write_raw(TAOS* taos, tmq_raw_data raw) { static int32_t tmqWriteBatchMetaDataImpl(TAOS* taos, void* meta, int32_t metaLen) { if (taos == NULL || meta == NULL) { - terrno = TSDB_CODE_INVALID_PARA; - return terrno; + return TSDB_CODE_INVALID_PARA; } SMqBatchMetaRsp rsp = {0}; - SDecoder coder; + SDecoder coder = {0}; int32_t code = TSDB_CODE_SUCCESS; // decode and process req tDecoderInit(&coder, meta, metaLen); if (tDecodeMqBatchMetaRsp(&coder, &rsp) < 0) { code = TSDB_CODE_INVALID_PARA; - goto _end; + goto end; } int32_t num = taosArrayGetSize(rsp.batchMetaReq); for (int32_t i = 0; i < num; i++) { - int32_t len = *(int32_t*)taosArrayGet(rsp.batchMetaLen, i); - void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i); + int32_t* len = taosArrayGet(rsp.batchMetaLen, i); + RAW_NULL_CHECK(len); + void* tmpBuf = taosArrayGetP(rsp.batchMetaReq, i); + RAW_NULL_CHECK(tmpBuf); SDecoder metaCoder = {0}; SMqMetaRsp metaRsp = {0}; - tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), len - sizeof(SMqRspHead)); + tDecoderInit(&metaCoder, POINTER_SHIFT(tmpBuf, sizeof(SMqRspHead)), *len - sizeof(SMqRspHead)); if (tDecodeMqMetaRsp(&metaCoder, &metaRsp) < 0) { code = TSDB_CODE_INVALID_PARA; - goto _end; + goto end; } code = writeRawImpl(taos, metaRsp.metaRsp, metaRsp.metaRspLen, metaRsp.resMsgType); tDeleteMqMetaRsp(&metaRsp); if (code != TSDB_CODE_SUCCESS) { - goto _end; + goto end; } } -_end: +end: tDeleteMqBatchMetaRsp(&rsp); - errno = code; return code; } diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 7e89753241..692771602e 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -10820,7 +10820,9 @@ int32_t tDecodeMqBatchMetaRsp(SDecoder *pDecoder, SMqBatchMetaRsp *pRsp) { if (tDecodeI32(pDecoder, &size) < 0) return -1; if (size > 0) { pRsp->batchMetaReq = taosArrayInit(size, POINTER_BYTES); + if (!pRsp->batchMetaReq) return -1; pRsp->batchMetaLen = taosArrayInit(size, sizeof(int32_t)); + if (!pRsp->batchMetaLen) return -1; for (int32_t i = 0; i < size; i++) { void *pCreate = NULL; uint64_t len = 0; diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index 4048c8841b..83484fdc65 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -448,7 +448,8 @@ int32_t ctgGetTbTag(SCatalog* pCtg, SRequestConnInfo* pConn, SName* pTableName, CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); } - char* pJson = parseTagDatatoJson(pTag); + char* pJson = NULL; + parseTagDatatoJson(pTag, &pJson); STagVal tagVal; tagVal.cid = 0; tagVal.type = TSDB_DATA_TYPE_JSON; diff --git a/source/libs/catalog/src/ctgAsync.c b/source/libs/catalog/src/ctgAsync.c index 601e01f7e9..e27548ab4f 100644 --- a/source/libs/catalog/src/ctgAsync.c +++ b/source/libs/catalog/src/ctgAsync.c @@ -1772,7 +1772,8 @@ int32_t ctgHandleGetTbTagRsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf* CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); } - char* pJson = parseTagDatatoJson(pTag); + char* pJson = NULL; + parseTagDatatoJson(pTag, &pJson); STagVal tagVal; tagVal.cid = 0; tagVal.type = TSDB_DATA_TYPE_JSON; diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index 40e85e90d2..9506e801c5 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -503,11 +503,10 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) { } if (tTagIsJson(pTag)) { - char* pJson = parseTagDatatoJson(pTag); - if (pJson) { - *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s", pJson); - taosMemoryFree(pJson); - } + char* pJson = NULL; + parseTagDatatoJson(pTag, &pJson); + *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "%s", pJson); + taosMemoryFree(pJson); return TSDB_CODE_SUCCESS; } diff --git a/source/libs/executor/src/sysscanoperator.c b/source/libs/executor/src/sysscanoperator.c index 415c7d1f0e..dd6c653a8a 100644 --- a/source/libs/executor/src/sysscanoperator.c +++ b/source/libs/executor/src/sysscanoperator.c @@ -966,7 +966,8 @@ static int32_t sysTableUserTagsFillOneTableTags(const SSysTableScanInfo* pInfo, char* tagVarChar = NULL; if (tagData != NULL) { if (tagType == TSDB_DATA_TYPE_JSON) { - char* tagJson = parseTagDatatoJson(tagData); + char* tagJson = NULL; + parseTagDatatoJson(tagData, &tagJson); tagVarChar = taosMemoryMalloc(strlen(tagJson) + VARSTR_HEADER_SIZE); memcpy(varDataVal(tagVarChar), tagJson, strlen(tagJson)); varDataSetLen(tagVarChar, strlen(tagJson)); diff --git a/source/libs/qcom/src/queryUtil.c b/source/libs/qcom/src/queryUtil.c index 9ff6fc3e49..836cd6752b 100644 --- a/source/libs/qcom/src/queryUtil.c +++ b/source/libs/qcom/src/queryUtil.c @@ -415,7 +415,7 @@ int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t return TSDB_CODE_SUCCESS; } -char* parseTagDatatoJson(void* p) { +void parseTagDatatoJson(void* p, char** jsonStr) { char* string = NULL; SArray* pTagVals = NULL; cJSON* json = NULL; @@ -434,6 +434,9 @@ char* parseTagDatatoJson(void* p) { } for (int j = 0; j < nCols; ++j) { STagVal* pTagVal = (STagVal*)taosArrayGet(pTagVals, j); + if (pTagVal == NULL) { + continue; + } // json key encode by binary tstrncpy(tagJsonKey, pTagVal->pKey, sizeof(tagJsonKey)); // json value @@ -443,11 +446,16 @@ char* parseTagDatatoJson(void* p) { if (value == NULL) { goto end; } - cJSON_AddItemToObject(json, tagJsonKey, value); + if(!cJSON_AddItemToObject(json, tagJsonKey, value)){ + goto end; + } } else if (type == TSDB_DATA_TYPE_NCHAR) { cJSON* value = NULL; if (pTagVal->nData > 0) { char* tagJsonValue = taosMemoryCalloc(pTagVal->nData, 1); + if (tagJsonValue == NULL) { + goto end; + } int32_t length = taosUcs4ToMbs((TdUcs4*)pTagVal->pData, pTagVal->nData, tagJsonValue); if (length < 0) { qError("charset:%s to %s. val:%s convert json value failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, @@ -462,25 +470,34 @@ char* parseTagDatatoJson(void* p) { } } else if (pTagVal->nData == 0) { value = cJSON_CreateString(""); + if (value == NULL) { + goto end; + } } else { goto end; } - cJSON_AddItemToObject(json, tagJsonKey, value); + if(!cJSON_AddItemToObject(json, tagJsonKey, value)){ + goto end; + } } else if (type == TSDB_DATA_TYPE_DOUBLE) { double jsonVd = *(double*)(&pTagVal->i64); cJSON* value = cJSON_CreateNumber(jsonVd); if (value == NULL) { goto end; } - cJSON_AddItemToObject(json, tagJsonKey, value); + if(!cJSON_AddItemToObject(json, tagJsonKey, value)){ + goto end; + } } else if (type == TSDB_DATA_TYPE_BOOL) { char jsonVd = *(char*)(&pTagVal->i64); cJSON* value = cJSON_CreateBool(jsonVd); if (value == NULL) { goto end; } - cJSON_AddItemToObject(json, tagJsonKey, value); + if(!cJSON_AddItemToObject(json, tagJsonKey, value)){ + goto end; + } } else { goto end; } @@ -492,7 +509,7 @@ end: if (string == NULL) { string = taosStrdup(TSDB_DATA_NULL_STR_L); } - return string; + *jsonStr = string; } int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst) {