From b1dbab9a41cccb1748a7d88bfcf132debbce05f0 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Sun, 26 Sep 2021 15:35:11 +0800 Subject: [PATCH] [TD-10423]: Wrong conversion when testing negative bigint --- deps/cJson/inc/cJSON.h | 2 +- deps/cJson/src/cJSON.c | 2 +- src/client/src/tscParseOpenTSDB.c | 26 ++++++++++++++++++++------ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/deps/cJson/inc/cJSON.h b/deps/cJson/inc/cJSON.h index cdd5faa523..6b9faac033 100644 --- a/deps/cJson/inc/cJSON.h +++ b/deps/cJson/inc/cJSON.h @@ -73,7 +73,7 @@ typedef struct cJSON char *string; //Keep the original string of number - char numberstring[13]; + char numberstring[22]; /* change this to 22 bytes to accommodate LLONG_MAX and LLONG_MINX*/ } cJSON; typedef struct cJSON_Hooks diff --git a/deps/cJson/src/cJSON.c b/deps/cJson/src/cJSON.c index f0ef9f6fe1..ff93e8730d 100644 --- a/deps/cJson/src/cJSON.c +++ b/deps/cJson/src/cJSON.c @@ -290,7 +290,7 @@ loop_end: input_buffer->offset += (size_t)(after_end - number_c_string); - strncpy(item->numberstring, (const char *)number_c_string, 12); + strncpy(item->numberstring, (const char *)number_c_string, strlen((const char*)number_c_string)); return true; } diff --git a/src/client/src/tscParseOpenTSDB.c b/src/client/src/tscParseOpenTSDB.c index ce0932e260..afae64727b 100644 --- a/src/client/src/tscParseOpenTSDB.c +++ b/src/client/src/tscParseOpenTSDB.c @@ -623,14 +623,19 @@ static int32_t convertJSONNumber(TAOS_SML_KV *pVal, char* typeStr, cJSON *value, //bigint if (strcasecmp(typeStr, "i64") == 0 || strcasecmp(typeStr, "bigint") == 0) { - if (!IS_VALID_BIGINT(value->valueint)) { - tscError("OTD:0x%"PRIx64" JSON value(%"PRId64") cannot fit in type(bigint)", info->id, value->valueint); - return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE; - } pVal->type = TSDB_DATA_TYPE_BIGINT; pVal->length = (int16_t)tDataTypes[pVal->type].bytes; pVal->value = tcalloc(pVal->length, 1); - *(int64_t *)(pVal->value) = (int64_t)(value->valueint); + /* cJSON conversion of legit BIGINT may overflow, + * use original string to do the conversion. + */ + errno = 0; + int64_t val = (int64_t)strtoll(value->numberstring, NULL, 10); + if (errno == ERANGE || !IS_VALID_BIGINT(val)) { + tscError("OTD:0x%"PRIx64" JSON value(%s) cannot fit in type(bigint)", info->id, value->numberstring); + return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE; + } + *(int64_t *)(pVal->value) = val; return TSDB_CODE_SUCCESS; } //float @@ -746,7 +751,16 @@ static int32_t parseValueFromJSON(cJSON *root, TAOS_SML_KV *pVal, SSmlLinesInfo* pVal->type = TSDB_DATA_TYPE_BIGINT; pVal->length = (int16_t)tDataTypes[pVal->type].bytes; pVal->value = tcalloc(pVal->length, 1); - *(int64_t *)(pVal->value) = (int64_t)(root->valuedouble); + /* cJSON conversion of legit BIGINT may overflow, + * use original string to do the conversion. + */ + errno = 0; + int64_t val = (int64_t)strtoll(root->numberstring, NULL, 10); + if (errno == ERANGE || !IS_VALID_BIGINT(val)) { + tscError("OTD:0x%"PRIx64" JSON value(%s) cannot fit in type(bigint)", info->id, root->numberstring); + return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE; + } + *(int64_t *)(pVal->value) = val; } else if (isValidFloat(root->numberstring)) { pVal->type = TSDB_DATA_TYPE_DOUBLE; pVal->length = (int16_t)tDataTypes[pVal->type].bytes; -- GitLab