提交 8c49afc1 编写于 作者: B Bomin Zhang

fix incorrect length in nchar conversion

上级 73eee9d8
...@@ -1888,11 +1888,14 @@ static void transferNcharData(SSqlObj *pSql, int32_t columnIndex, TAOS_FIELD *pF ...@@ -1888,11 +1888,14 @@ static void transferNcharData(SSqlObj *pSql, int32_t columnIndex, TAOS_FIELD *pF
/* string terminated char for binary data*/ /* string terminated char for binary data*/
memset(pRes->buffer[columnIndex], 0, pField->bytes + TSDB_NCHAR_SIZE); memset(pRes->buffer[columnIndex], 0, pField->bytes + TSDB_NCHAR_SIZE);
if (taosUcs4ToMbs(pRes->tsrow[columnIndex], pField->bytes - VARSTR_HEADER_SIZE, pRes->buffer[columnIndex])) { int32_t length = taosUcs4ToMbs(pRes->tsrow[columnIndex], pRes->length[columnIndex], pRes->buffer[columnIndex]);
if ( length >= 0 ) {
pRes->tsrow[columnIndex] = pRes->buffer[columnIndex]; pRes->tsrow[columnIndex] = pRes->buffer[columnIndex];
pRes->length[columnIndex] = length;
} else { } else {
tscError("%p charset:%s to %s. val:%ls convert failed.", pSql, DEFAULT_UNICODE_ENCODEC, tsCharset, pRes->tsrow[columnIndex]); tscError("%p charset:%s to %s. val:%ls convert failed.", pSql, DEFAULT_UNICODE_ENCODEC, tsCharset, pRes->tsrow[columnIndex]);
pRes->tsrow[columnIndex] = NULL; pRes->tsrow[columnIndex] = NULL;
pRes->length[columnIndex] = 0;
} }
} }
} }
......
...@@ -145,7 +145,7 @@ bool taosMbsToUcs4(char *mbs, size_t mbs_len, char *ucs4, int32_t ucs4_max_len, ...@@ -145,7 +145,7 @@ bool taosMbsToUcs4(char *mbs, size_t mbs_len, char *ucs4, int32_t ucs4_max_len,
int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes); int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes);
bool taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs); int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs);
bool taosValidateEncodec(const char *encodec); bool taosValidateEncodec(const char *encodec);
......
...@@ -447,10 +447,10 @@ int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes) { ...@@ -447,10 +447,10 @@ int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes) {
int32_t ucs4_max_len = bytes + 4; int32_t ucs4_max_len = bytes + 4;
char *f1_mbs = calloc(bytes, 1); char *f1_mbs = calloc(bytes, 1);
char *f2_mbs = calloc(bytes, 1); char *f2_mbs = calloc(bytes, 1);
if (!taosUcs4ToMbs(f1_ucs4, ucs4_max_len, f1_mbs)) { if (taosUcs4ToMbs(f1_ucs4, ucs4_max_len, f1_mbs) < 0) {
return -1; return -1;
} }
if (!taosUcs4ToMbs(f2_ucs4, ucs4_max_len, f2_mbs)) { if (taosUcs4ToMbs(f2_ucs4, ucs4_max_len, f2_mbs) < 0) {
return -1; return -1;
} }
int32_t ret = strcmp(f1_mbs, f2_mbs); int32_t ret = strcmp(f1_mbs, f2_mbs);
...@@ -464,29 +464,29 @@ int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes) { ...@@ -464,29 +464,29 @@ int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes) {
#endif #endif
} }
bool taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) { int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs) {
#ifdef USE_LIBICONV #ifdef USE_LIBICONV
iconv_t cd = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC); iconv_t cd = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC);
size_t ucs4_input_len = ucs4_max_len; size_t ucs4_input_len = ucs4_max_len;
size_t outLen = ucs4_max_len; size_t outLen = ucs4_max_len;
if (iconv(cd, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) { if (iconv(cd, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) {
iconv_close(cd); iconv_close(cd);
return false; return -1;
} }
iconv_close(cd); iconv_close(cd);
return true; return (int32_t)(ucs4_max_len - outLen);
#else #else
mbstate_t state = {0}; mbstate_t state = {0};
int32_t len = (int32_t) wcsnrtombs(NULL, (const wchar_t **) &ucs4, ucs4_max_len / 4, 0, &state); int32_t len = (int32_t) wcsnrtombs(NULL, (const wchar_t **) &ucs4, ucs4_max_len / 4, 0, &state);
if (len < 0) { if (len < 0) {
return false; return -1;
} }
memset(&state, 0, sizeof(state)); memset(&state, 0, sizeof(state));
len = wcsnrtombs(mbs, (const wchar_t **) &ucs4, ucs4_max_len / 4, (size_t) len, &state); len = wcsnrtombs(mbs, (const wchar_t **) &ucs4, ucs4_max_len / 4, (size_t) len, &state);
if (len < 0) { if (len < 0) {
return false; return -1;
} }
return true; return len;
#endif #endif
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册