tsnprintf

This commit is contained in:
xsren 2024-10-10 10:35:29 +08:00
parent 8a580a5392
commit c7f6349e14
5 changed files with 73 additions and 10 deletions

View File

@ -62,9 +62,10 @@ typedef enum { M2C = 0, C2M } ConvType;
#define tstrncpy(dst, src, size) \ #define tstrncpy(dst, src, size) \
do { \ do { \
(void)strncpy((dst), (src), (size)); \ (void)strncpy((dst), (src), (size)); \
(dst)[(size)-1] = 0; \ (dst)[(size) - 1] = 0; \
} while (0) } while (0)
int64_t tsnprintf(char *dst, int64_t size, const char *format, ...);
#define TAOS_STRCPY(_dst, _src) ((void)strcpy(_dst, _src)) #define TAOS_STRCPY(_dst, _src) ((void)strcpy(_dst, _src))
#define TAOS_STRNCPY(_dst, _src, _size) ((void)strncpy(_dst, _src, _size)) #define TAOS_STRNCPY(_dst, _src, _size) ((void)strncpy(_dst, _src, _size))
#define TAOS_STRCAT(_dst, _src) ((void)strcat(_dst, _src)) #define TAOS_STRCAT(_dst, _src) ((void)strcat(_dst, _src))

View File

@ -510,20 +510,24 @@ void appendColumnFields(char* buf, int32_t* len, STableCfg* pCfg) {
#define LTYPE_LEN (32 + 60) // 60 byte for compress info #define LTYPE_LEN (32 + 60) // 60 byte for compress info
char type[LTYPE_LEN]; char type[LTYPE_LEN];
snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name); snprintf(type, LTYPE_LEN, "%s", tDataTypes[pSchema->type].name);
int typeLen = strlen(type);
if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type || if (TSDB_DATA_TYPE_VARCHAR == pSchema->type || TSDB_DATA_TYPE_VARBINARY == pSchema->type ||
TSDB_DATA_TYPE_GEOMETRY == pSchema->type) { TSDB_DATA_TYPE_GEOMETRY == pSchema->type) {
snprintf(type + strlen(type), LTYPE_LEN - strlen(type), "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE)); snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)", (int32_t)(pSchema->bytes - VARSTR_HEADER_SIZE));
} else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) { } else if (TSDB_DATA_TYPE_NCHAR == pSchema->type) {
snprintf(type + strlen(type), LTYPE_LEN - strlen(type), "(%d)", snprintf(type + typeLen, LTYPE_LEN - typeLen, "(%d)",
(int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE)); (int32_t)((pSchema->bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE));
} }
if (useCompress(pCfg->tableType) && pCfg->pSchemaExt) { if (useCompress(pCfg->tableType) && pCfg->pSchemaExt) {
snprintf(type + strlen(type), LTYPE_LEN - strlen(type), " ENCODE \'%s\'", typeLen = strlen(type);
snprintf(type + typeLen, LTYPE_LEN - typeLen, " ENCODE \'%s\'",
columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress))); columnEncodeStr(COMPRESS_L1_TYPE_U32(pCfg->pSchemaExt[i].compress)));
snprintf(type + strlen(type), LTYPE_LEN - strlen(type), " COMPRESS \'%s\'", typeLen = strlen(type);
snprintf(type + typeLen, LTYPE_LEN - typeLen, " COMPRESS \'%s\'",
columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress))); columnCompressStr(COMPRESS_L2_TYPE_U32(pCfg->pSchemaExt[i].compress)));
snprintf(type + strlen(type), LTYPE_LEN - strlen(type), " LEVEL \'%s\'", typeLen = strlen(type);
snprintf(type + typeLen, LTYPE_LEN - typeLen, " LEVEL \'%s\'",
columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress))); columnLevelStr(COMPRESS_L2_TYPE_LEVEL_U32(pCfg->pSchemaExt[i].compress)));
} }
if (!(pSchema->flags & COL_IS_KEY)) { if (!(pSchema->flags & COL_IS_KEY)) {
@ -694,7 +698,7 @@ void appendTableOptions(char* buf, int32_t* len, SDbCfgInfo* pDbCfg, STableCfg*
if (nSma < pCfg->numOfColumns && nSma > 0) { if (nSma < pCfg->numOfColumns && nSma > 0) {
bool smaOn = false; bool smaOn = false;
*len += snprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len), *len += tsnprintf(buf + VARSTR_HEADER_SIZE + *len, SHOW_CREATE_TB_RESULT_FIELD2_LEN - (VARSTR_HEADER_SIZE + *len),
" SMA("); " SMA(");
for (int32_t i = 0; i < pCfg->numOfColumns; ++i) { for (int32_t i = 0; i < pCfg->numOfColumns; ++i) {
if (IS_BSMA_ON(pCfg->pSchemas + i)) { if (IS_BSMA_ON(pCfg->pSchemas + i)) {

View File

@ -143,7 +143,7 @@ static int32_t udfSpawnUdfd(SUdfdData *pData) {
char udfdPathLdLib[1024] = {0}; char udfdPathLdLib[1024] = {0};
size_t udfdLdLibPathLen = strlen(tsUdfdLdLibPath); size_t udfdLdLibPathLen = strlen(tsUdfdLdLibPath);
tstrncpy(udfdPathLdLib, tsUdfdLdLibPath, sizeof(udfdPathLdLib) < sizeof(tsUdfdLdLibPath) ? sizeof(udfdPathLdLib) : sizeof(tsUdfdLdLibPath)); tstrncpy(udfdPathLdLib, tsUdfdLdLibPath, sizeof(udfdPathLdLib));
udfdPathLdLib[udfdLdLibPathLen] = ':'; udfdPathLdLib[udfdLdLibPathLen] = ':';
tstrncpy(udfdPathLdLib + udfdLdLibPathLen + 1, pathTaosdLdLib, sizeof(udfdPathLdLib) - udfdLdLibPathLen - 1); tstrncpy(udfdPathLdLib + udfdLdLibPathLen + 1, pathTaosdLdLib, sizeof(udfdPathLdLib) - udfdLdLibPathLen - 1);

View File

@ -710,3 +710,18 @@ int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size) {
return 0; return 0;
} }
int64_t tsnprintf(char *dst, int64_t size, const char *format, ...) {
if (size <= 0 || size > SIZE_MAX) return 0;
int64_t ret;
va_list args;
va_start(args, format);
ret = vsnprintf(dst, size, format, args);
va_end(args );
if (ret >= size) {
return size - 1;
} else {
return ret;
}
}

View File

@ -14,6 +14,7 @@
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstring>
#include <iostream> #include <iostream>
#pragma GCC diagnostic push #pragma GCC diagnostic push
@ -111,3 +112,45 @@ TEST(osStringTests, osUcs4lenTests2) {
TdUcs4 ucs4_3[] = {'C', 'h', 'i', 'n', 'a', 0x4E2D, 0x6587, '\0'}; TdUcs4 ucs4_3[] = {'C', 'h', 'i', 'n', 'a', 0x4E2D, 0x6587, '\0'};
EXPECT_EQ(taosUcs4len(ucs4_3), 7); EXPECT_EQ(taosUcs4len(ucs4_3), 7);
} }
TEST(osStringTests, ostsnprintfTests) {
char buffer[50] = {0};
int64_t ret;
ret = tsnprintf(buffer, sizeof(buffer), "Hello, %s!", "World");
EXPECT_EQ(ret, 13);
EXPECT_STREQ(buffer, "Hello, World!");
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, 10, "Hello, %s!", "World");
EXPECT_EQ(ret, 9);
EXPECT_EQ(strncmp(buffer, "Hello, Wo", 9), 0);
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, 10, "Hello%s", "World");
EXPECT_EQ(ret, 9);
EXPECT_EQ(strncmp(buffer, "Hello, Wo", 9), 0);
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, 0, "Hello, %s!", "World");
EXPECT_EQ(ret, 0);
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, SIZE_MAX + 1, "Hello, %s!", "World");
EXPECT_EQ(ret, 0);
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, sizeof(buffer), "");
EXPECT_EQ(ret, 0);
EXPECT_STREQ(buffer, "");
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, sizeof(buffer), "Number: %d", 42);
EXPECT_EQ(ret, 10);
EXPECT_STREQ(buffer, "Number: 42");
memset(buffer, 0, sizeof(buffer));
ret = tsnprintf(buffer, sizeof(buffer), "Float: %.2f", 3.14);
EXPECT_EQ(ret, 11);
EXPECT_STREQ(buffer, "Float: 3.14");
}