From 77837f6e6a9be731a3efd4b16c9131aaf8f77735 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 30 Jul 2022 15:10:24 +0800 Subject: [PATCH] fix: fix crash issue --- include/os/osString.h | 1 + source/client/src/clientEnv.c | 2 ++ source/dnode/mgmt/exe/dmMain.c | 2 ++ source/os/src/osString.c | 36 +++++++++++++++++----------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/os/osString.h b/include/os/osString.h index 2a22c6d8ff..8eb341faa7 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -62,6 +62,7 @@ typedef int32_t TdUcs4; int32_t taosUcs4len(TdUcs4 *ucs4); int64_t taosStr2int64(const char *str); +void taosConvInit(void); void taosConvDestroy(); int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs); bool taosMbsToUcs4(const char *mbs, size_t mbs_len, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 893aa39ce3..3d539ea251 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -361,6 +361,8 @@ void taos_init_imp(void) { initQueryModuleMsgHandle(); + taosConvInit(); + rpcInit(); SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100}; diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index 34c3b40556..4030eaa6fe 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -218,6 +218,8 @@ int mainWindows(int argc,char** argv) { taosCleanupArgs(); return -1; } + + taosConvInit(); if (global.dumpConfig) { dmDumpCfg(); diff --git a/source/os/src/osString.c b/source/os/src/osString.c index 503b8e0c6b..26aafd743f 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -143,29 +143,29 @@ SConv *gConv = NULL; int32_t convUsed = 0; int32_t gConvMaxNum = 0; -void taosConvInitImpl(void) { +void taosConvInit(void) { gConvMaxNum = 512; gConv = taosMemoryCalloc(gConvMaxNum, sizeof(SConv)); for (int32_t i = 0; i < gConvMaxNum; ++i) { gConv[i].conv = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); + if ((iconv_t)-1 == gConv[i].conv || (iconv_t)0 == gConv[i].conv) { + ASSERT(0); + } } } -static TdThreadOnce convInit = PTHREAD_ONCE_INIT; -void taosConvInit() { - taosThreadOnce(&convInit, taosConvInitImpl); -} - void taosConvDestroy() { for (int32_t i = 0; i < gConvMaxNum; ++i) { iconv_close(gConv[i].conv); } taosMemoryFreeClear(gConv); + gConvMaxNum = -1; } -void taosAcquireConv(int32_t *idx) { - if (0 == gConvMaxNum) { - taosConvInit(); +iconv_t taosAcquireConv(int32_t *idx) { + if (gConvMaxNum <= 0) { + *idx = -1; + return iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset); } while (true) { @@ -193,12 +193,12 @@ void taosAcquireConv(int32_t *idx) { } *idx = startId; + return gConv[startId].conv; } -void taosReleaseConv(int32_t idx) { - if (0 == gConvMaxNum) { - iconv_close(gConv[0].conv); - taosMemoryFreeClear(gConv); +void taosReleaseConv(int32_t idx, iconv_t conv) { + if (idx < 0) { + iconv_close(conv); return; } @@ -213,16 +213,16 @@ bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4 #else memset(ucs4, 0, ucs4_max_len); - int32_t idx = 0; - taosAcquireConv(&idx); + int32_t idx = -1; + iconv_t conv = taosAcquireConv(&idx); size_t ucs4_input_len = mbsLength; size_t outLeft = ucs4_max_len; - if (iconv(gConv[idx].conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { - taosReleaseConv(idx); + if (iconv(conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { + taosReleaseConv(idx, conv); return false; } - taosReleaseConv(idx); + taosReleaseConv(idx, conv); if (len != NULL) { *len = (int32_t)(ucs4_max_len - outLeft); if (*len < 0) { -- GitLab