This commit is contained in:
facetosea 2024-11-11 17:14:01 +08:00
parent 1d331d89a8
commit 14e240d442
3 changed files with 141 additions and 18 deletions

View File

@ -140,9 +140,10 @@ extern threadlocal bool tsEnableRandErr;
#define OS_PARAM_CHECK(_o) \
do { \
if ((_o) == NULL) { \
return TSDB_CODE_INVALID_PARA; \
terrno = TSDB_CODE_INVALID_PARA; \
return terrno; \
} \
} while(0)
} while (0)
#ifdef __cplusplus
}

View File

@ -233,6 +233,8 @@ int32_t taosBlockSIGPIPE() {
}
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip) {
OS_PARAM_CHECK(fqdn);
OS_PARAM_CHECK(ip);
#ifdef WINDOWS
// Initialize Winsock
WSADATA wsaData;
@ -309,6 +311,7 @@ int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip) {
}
int32_t taosGetFqdn(char *fqdn) {
OS_PARAM_CHECK(fqdn);
#ifdef WINDOWS
// Initialize Winsock
WSADATA wsaData;
@ -384,6 +387,9 @@ int32_t taosGetFqdn(char *fqdn) {
}
void tinet_ntoa(char *ipstr, uint32_t ip) {
if (ipstr == NULL) {
return;
}
(void)snprintf(ipstr, TD_IP_LEN, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24);
}

View File

@ -25,11 +25,15 @@ extern int wcwidth(wchar_t c);
extern int wcswidth(const wchar_t *s, size_t n);
char *tstrdup(const char *str) {
if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
#ifdef WINDOWS
return _strdup(str);
#else
char* p = strdup(str);
if (str != NULL && NULL == p) {
char *p = strdup(str);
if (NULL == p) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
}
return p;
@ -41,6 +45,10 @@ char *tstrdup(const char *str) {
// No errors are expected to occur
char *strsep(char **stringp, const char *delim) {
if (stringp == NULL || delim == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
char *s;
const char *spanp;
int32_t c, sc;
@ -81,6 +89,10 @@ char *taosStrndup(const char *s, int size) {
/* Copy no more than N characters of SRC to DEST, returning the address of
the terminating '\0' in DEST, if any, or else DEST + N. */
char *stpncpy(char *dest, const char *src, int n) {
if (dest == NULL || src == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
size_t size = strnlen(src, n);
memcpy(dest, src, size);
dest += size;
@ -117,6 +129,7 @@ int32_t taosStr2int64(const char *str, int64_t *val) {
}
int32_t taosStr2int16(const char *str, int16_t *val) {
OS_PARAM_CHECK(str);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
@ -130,6 +143,7 @@ int32_t taosStr2int16(const char *str, int16_t *val) {
}
int32_t taosStr2int32(const char *str, int32_t *val) {
OS_PARAM_CHECK(str);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
@ -143,6 +157,8 @@ int32_t taosStr2int32(const char *str, int32_t *val) {
}
int32_t taosStr2int8(const char *str, int8_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
@ -156,6 +172,9 @@ int32_t taosStr2int8(const char *str, int8_t *val) {
}
int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) {
if (bytes > 0 && (f1_ucs4 == NULL || f2_ucs4 == NULL)) {
return TSDB_CODE_INVALID_PARA;
}
for (int32_t i = 0; i < bytes; i += sizeof(TdUcs4)) {
int32_t f1 = *(int32_t *)((char *)f1_ucs4 + i);
int32_t f2 = *(int32_t *)((char *)f2_ucs4 + i);
@ -191,6 +210,9 @@ int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) {
}
int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) {
if (target_ucs4 == NULL || source_ucs4 == NULL || len_ucs4 <= 0) {
return TSDB_CODE_INVALID_PARA;
}
if (taosMemorySize(target_ucs4) < len_ucs4 * sizeof(TdUcs4)) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
@ -261,6 +283,10 @@ void taosConvDestroy() {
}
iconv_t taosAcquireConv(int32_t *idx, ConvType type) {
if(idx == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return (iconv_t)-1;
}
if (gConvMaxNum[type] <= 0) {
*idx = -1;
if (type == M2C) {
@ -321,6 +347,10 @@ void taosReleaseConv(int32_t idx, iconv_t conv, ConvType type) {
}
bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len) {
if(mbs == NULL || ucs4 == NULL || ucs4_max_len <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return false;
}
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
printf("Nchar cannot be read and written without iconv, please install iconv library and recompile.\n");
terrno = TSDB_CODE_APP_ERROR;
@ -359,6 +389,10 @@ bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4
// if success, return the number of bytes written to mbs ( >= 0)
// otherwise return error code ( < 0)
int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) {
if(ucs4 == NULL || mbs == NULL || ucs4_max_len <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
printf("Nchar cannot be read and written without iconv, please install iconv library and recompile.\n");
terrno = TSDB_CODE_APP_ERROR;
@ -390,6 +424,10 @@ int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) {
// if success, return the number of bytes written to mbs ( >= 0)
// otherwise return error code ( < 0)
int32_t taosUcs4ToMbsEx(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs, iconv_t conv) {
if(ucs4 == NULL || mbs == NULL || ucs4_max_len <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
printf("Nchar cannot be read and written without iconv, please install iconv library and recompile.\n");
terrno = TSDB_CODE_APP_ERROR;
@ -408,6 +446,10 @@ int32_t taosUcs4ToMbsEx(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs, iconv_t c
}
bool taosValidateEncodec(const char *encodec) {
if(encodec == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return false;
}
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
printf("Nchar cannot be read and written without iconv, please install iconv library and recompile.\n");
terrno = TSDB_CODE_APP_ERROR;
@ -443,7 +485,7 @@ int32_t taosUcs4len(TdUcs4 *ucs4) {
// dst buffer size should be at least 2*len + 1
int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len, int32_t bufSize) {
if (!dst) {
if (!dst || !src || len <= 0 || bufSize <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
@ -456,7 +498,7 @@ int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len, int32_t
}
int32_t taosHexDecode(const char *src, char *dst, int32_t len) {
if (!dst) {
if(!src || !dst || len <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
@ -475,19 +517,42 @@ int32_t taosHexDecode(const char *src, char *dst, int32_t len) {
int32_t taosWcharWidth(TdWchar wchar) { return wcwidth(wchar); }
int32_t taosWcharsWidth(TdWchar *pWchar, int32_t size) { return wcswidth(pWchar, size); }
int32_t taosWcharsWidth(TdWchar *pWchar, int32_t size) {
if (pWchar == NULL || size <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return wcswidth(pWchar, size);
}
int32_t taosMbToWchar(TdWchar *pWchar, const char *pStr, int32_t size) { return mbtowc(pWchar, pStr, size); }
int32_t taosMbToWchar(TdWchar *pWchar, const char *pStr, int32_t size) {
if (pWchar == NULL || pStr == NULL || size <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return mbtowc(pWchar, pStr, size);
}
int32_t taosMbsToWchars(TdWchar *pWchars, const char *pStrs, int32_t size) { return mbstowcs(pWchars, pStrs, size); }
int32_t taosMbsToWchars(TdWchar *pWchars, const char *pStrs, int32_t size) {
if (pWchars == NULL || pStrs == NULL || size <= 0) {
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
return mbstowcs(pWchars, pStrs, size);
}
int32_t taosWcharToMb(char *pStr, TdWchar wchar) { return wctomb(pStr, wchar); }
int32_t taosWcharToMb(char *pStr, TdWchar wchar) {
OS_PARAM_CHECK(pStr);
return wctomb(pStr, wchar); }
char *taosStrCaseStr(const char *str, const char *pattern) {
if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
if (!pattern || !*pattern) return (char *)str;
size_t i;
if (!*pattern) return (char *)str;
for (; *str; str++) {
if (toupper(*str) == toupper(*pattern)) {
for (i = 1;; i++) {
@ -500,6 +565,10 @@ char *taosStrCaseStr(const char *str, const char *pattern) {
}
int64_t taosStr2Int64(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
int64_t tmp = strtoll(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -508,6 +577,10 @@ int64_t taosStr2Int64(const char *str, char **pEnd, int32_t radix) {
}
uint64_t taosStr2UInt64(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
uint64_t tmp = strtoull(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -516,6 +589,10 @@ uint64_t taosStr2UInt64(const char *str, char **pEnd, int32_t radix) {
}
int32_t taosStr2Int32(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
int32_t tmp = strtol(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -524,6 +601,10 @@ int32_t taosStr2Int32(const char *str, char **pEnd, int32_t radix) {
}
uint32_t taosStr2UInt32(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
uint32_t tmp = strtol(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -532,6 +613,10 @@ uint32_t taosStr2UInt32(const char *str, char **pEnd, int32_t radix) {
}
int16_t taosStr2Int16(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
int32_t tmp = strtol(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -540,6 +625,10 @@ int16_t taosStr2Int16(const char *str, char **pEnd, int32_t radix) {
}
uint16_t taosStr2UInt16(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
uint32_t tmp = strtoul(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -548,11 +637,19 @@ uint16_t taosStr2UInt16(const char *str, char **pEnd, int32_t radix) {
}
int8_t taosStr2Int8(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
int32_t tmp = strtol(str, pEnd, radix);
return tmp;
}
uint8_t taosStr2UInt8(const char *str, char **pEnd, int32_t radix) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
uint32_t tmp = strtoul(str, pEnd, radix);
#if defined(DARWIN) || defined(_ALPINE)
if (errno == EINVAL) errno = 0;
@ -561,11 +658,19 @@ uint8_t taosStr2UInt8(const char *str, char **pEnd, int32_t radix) {
}
double taosStr2Double(const char *str, char **pEnd) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
double tmp = strtod(str, pEnd);
return tmp;
}
float taosStr2Float(const char *str, char **pEnd) {
if(str == NULL || pEnd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
float tmp = strtof(str, pEnd);
return tmp;
}
@ -578,6 +683,10 @@ bool isHex(const char *z, uint32_t n) {
}
bool isValidateHex(const char *z, uint32_t n) {
if(!z) {
terrno = TSDB_CODE_INVALID_PARA;
return false;
}
if ((n & 1) != 0) return false;
for (size_t i = HEX_PREFIX_LEN; i < n; i++) {
if (isxdigit(z[i]) == 0) {
@ -588,6 +697,9 @@ bool isValidateHex(const char *z, uint32_t n) {
}
int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size) {
OS_PARAM_CHECK(z);
OS_PARAM_CHECK(data);
OS_PARAM_CHECK(size);
n -= HEX_PREFIX_LEN; // remove 0x
z += HEX_PREFIX_LEN;
*size = n / HEX_PREFIX_LEN;
@ -712,6 +824,10 @@ int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size) {
}
int64_t tsnprintf(char *dst, int64_t size, const char *format, ...) {
if (dst == NULL || format == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return 0;
}
if (size <= 0) return 0;
if (size == 1) {
dst[0] = '\0';