fix: replace unsafe str fun and check return value
This commit is contained in:
parent
90642f4dc2
commit
036b7ebefa
|
@ -700,9 +700,9 @@ SWord* addWord(const char* p, int32_t len, bool pattern) {
|
|||
|
||||
// check format
|
||||
if (pattern && len > 0) {
|
||||
if (p[len-1] == ';') {
|
||||
if (p[len - 1] == ';') {
|
||||
word->type = wordType(p, len - 1);
|
||||
word->end = true;
|
||||
word->end = true;
|
||||
} else {
|
||||
word->type = wordType(p, len);
|
||||
}
|
||||
|
@ -1331,14 +1331,18 @@ void printScreen(TAOS* con, SShellCmd* cmd, SWords* match) {
|
|||
|
||||
if (word->end && str[strLen - 1] != ';') {
|
||||
// append end ';'
|
||||
char *p = taosMemoryMalloc(strLen + 8);
|
||||
strcpy(p, str);
|
||||
strcat(p, ";");
|
||||
shellInsertStr(cmd, (char *)p, strLen + 1);
|
||||
taosMemoryFree(p);
|
||||
char* p = taosMemoryCalloc(strLen + 8, 1);
|
||||
if (p) {
|
||||
tstrncpy(p, str, strLen);
|
||||
tstrncpy(p + strLen, ";", 1);
|
||||
shellInsertStr(cmd, (char*)p, strLen + 1);
|
||||
taosMemoryFree(p);
|
||||
} else {
|
||||
shellInsertStr(cmd, (char*)str, strLen);
|
||||
}
|
||||
} else {
|
||||
// insert new
|
||||
shellInsertStr(cmd, (char *)str, strLen);
|
||||
shellInsertStr(cmd, (char*)str, strLen);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1722,25 +1726,25 @@ bool fieldOptionsArea(char* p) {
|
|||
}
|
||||
|
||||
// find tags
|
||||
if(strstr(p, " tags") != NULL) {
|
||||
if (strstr(p, " tags") != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(p2 == NULL) {
|
||||
if (p2 == NULL) {
|
||||
// first field area
|
||||
p2 = p1;
|
||||
}
|
||||
|
||||
// find blank count
|
||||
int32_t cnt = 0;
|
||||
while(p2) {
|
||||
while (p2) {
|
||||
p2 = strchr(p2, ' ');
|
||||
if (p2) {
|
||||
// get prev char
|
||||
char prec = *(p2 - 1);
|
||||
if (prec != ',' && prec != '(') {
|
||||
// blank if before comma, not calc count. like st(ts timestamp, age int + BLANK + TAB only two blank
|
||||
cnt ++;
|
||||
cnt++;
|
||||
}
|
||||
|
||||
// continue blank is one blank
|
||||
|
@ -1758,18 +1762,18 @@ bool fieldOptionsArea(char* p) {
|
|||
|
||||
// if is input create fields or tags area, return true
|
||||
bool isCreateFieldsArea(char* p) {
|
||||
int32_t n = 0; // count
|
||||
char *p1 = p;
|
||||
int32_t n = 0; // count
|
||||
char* p1 = p;
|
||||
while (*p1 != 0) {
|
||||
switch (*p1) {
|
||||
case '(':
|
||||
++ n;
|
||||
break;
|
||||
case ')':
|
||||
-- n;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case '(':
|
||||
++n;
|
||||
break;
|
||||
case ')':
|
||||
--n;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// move next
|
||||
++p1;
|
||||
|
|
|
@ -17,56 +17,58 @@
|
|||
#include <iostream>
|
||||
#include "shellAuto.h"
|
||||
|
||||
|
||||
TEST(fieldOptionsArea, autoTabTest) {
|
||||
printf("hellow world SHELL tab test\n");
|
||||
|
||||
// str false
|
||||
const char *s0 [] = {
|
||||
"create table st(ts ",
|
||||
"create table st(ts timestamp, age ",
|
||||
"create table st(ts timestamp, age",
|
||||
"create table st(ts timestamp, age int , name ",
|
||||
"create table st(ts timestamp, age int , name binary(16)",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary",
|
||||
"create table st(ts timestamp, age int, name binary(16)) tags(area int , addr varbinary(32)",
|
||||
"create table st( ts timestamp, age int, name binary(16)) tags( area int, addr",
|
||||
"create table st (ts timestamp , age int, name binary(16) , area int,",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int ,addr varbinary",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level 'high' , no i",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode 'simple8b' level 'high', no in",
|
||||
const char *s0[] = {
|
||||
"create table st(ts ",
|
||||
"create table st(ts timestamp, age ",
|
||||
"create table st(ts timestamp, age",
|
||||
"create table st(ts timestamp, age int , name ",
|
||||
"create table st(ts timestamp, age int , name binary(16)",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary",
|
||||
"create table st(ts timestamp, age int, name binary(16)) tags(area int , addr varbinary(32)",
|
||||
"create table st( ts timestamp, age int, name binary(16)) tags( area int, addr",
|
||||
"create table st (ts timestamp , age int, name binary(16) , area int,",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int ,addr varbinary",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level "
|
||||
"'high' , no i",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode "
|
||||
"'simple8b' level 'high', no in",
|
||||
};
|
||||
|
||||
// str true
|
||||
const char *s1 [] = {
|
||||
"create table st(ts timestamp ",
|
||||
"create table st(ts timestamp, age int ",
|
||||
"create table st(ts timestamp, age int ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) , addr varbinary( 32 ) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ,area int, addr varbinary(32) ",
|
||||
"create table st(ts timestamp, age int , name binary(16), area int,addr varbinary(32) ",
|
||||
"create table st(ts timestamp, age int, name binary(16) , area int,addr varbinary(32) ",
|
||||
"create table st( ts timestamp, age int, name binary(16) ,area int,addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16), area int,addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16), area int , addr varbinary(32) compress 'zlib' ",
|
||||
"create table st (ts timestamp , age int, name binary(16), area int , addr varbinary(32) level 'high' ",
|
||||
"create table st (ts timestamp , age int, name binary(16) , area int , addr varbinary(32) encode 'simple8b' level 'high' ",
|
||||
const char *s1[] = {
|
||||
"create table st(ts timestamp ",
|
||||
"create table st(ts timestamp, age int ",
|
||||
"create table st(ts timestamp, age int ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) , addr varbinary( 32 ) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ,area int, addr varbinary(32) ",
|
||||
"create table st(ts timestamp, age int , name binary(16), area int,addr varbinary(32) ",
|
||||
"create table st(ts timestamp, age int, name binary(16) , area int,addr varbinary(32) ",
|
||||
"create table st( ts timestamp, age int, name binary(16) ,area int,addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16), area int,addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16), area int , addr varbinary(32) compress 'zlib' ",
|
||||
"create table st (ts timestamp , age int, name binary(16), area int , addr varbinary(32) level 'high' ",
|
||||
"create table st (ts timestamp , age int, name binary(16) , area int , addr varbinary(32) encode 'simple8b' "
|
||||
"level 'high' ",
|
||||
};
|
||||
|
||||
// s0 is false
|
||||
for(int32_t i = 0; i < sizeof(s0)/sizeof(char*) ; i++) {
|
||||
for (int32_t i = 0; i < sizeof(s0) / sizeof(char *); i++) {
|
||||
printf("s0 i=%d fieldOptionsArea %s expect false \n", i, s0[i]);
|
||||
ASSERT (fieldOptionsArea((char *)s0[i]) == false);
|
||||
ASSERT(fieldOptionsArea((char *)s0[i]) == false);
|
||||
}
|
||||
|
||||
// s1 is true
|
||||
for(int32_t i = 0; i < sizeof(s1)/sizeof(char*) ; i++) {
|
||||
for (int32_t i = 0; i < sizeof(s1) / sizeof(char *); i++) {
|
||||
printf("s1 i=%d fieldOptionsArea %s expect true \n", i, s1[i]);
|
||||
ASSERT (fieldOptionsArea((char *)s1[i]) == true);
|
||||
ASSERT(fieldOptionsArea((char *)s1[i]) == true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,51 +76,56 @@ TEST(isCreateFieldsArea, autoTabTest) {
|
|||
printf("hellow world SHELL tab test\n");
|
||||
|
||||
// str false
|
||||
const char *s0 [] = {
|
||||
"create table st(ts )",
|
||||
"create table st(ts timestamp, age) ",
|
||||
"create table st(ts timestamp, age)",
|
||||
"create table st(ts timestamp, age int , name binary(16) )",
|
||||
"create table st(ts timestamp, age int , name binary(16))",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( )",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr )",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary)",
|
||||
"create table st(ts timestamp, age int, name binary(16)) tags(area int , addr varbinary(32))",
|
||||
"create table st( ts timestamp, age int, name binary(16)) tags( area int, addr int)",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int,addr varbinary(32) )",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int ,addr varbinary(14))",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level 'high' )",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode 'simple8b' level 'high' ) ",
|
||||
const char *s0[] = {
|
||||
"create table st(ts )",
|
||||
"create table st(ts timestamp, age) ",
|
||||
"create table st(ts timestamp, age)",
|
||||
"create table st(ts timestamp, age int , name binary(16) )",
|
||||
"create table st(ts timestamp, age int , name binary(16))",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( )",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr )",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary)",
|
||||
"create table st(ts timestamp, age int, name binary(16)) tags(area int , addr varbinary(32))",
|
||||
"create table st( ts timestamp, age int, name binary(16)) tags( area int, addr int)",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int,addr varbinary(32) )",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int ,addr varbinary(14))",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level "
|
||||
"'high' )",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode "
|
||||
"'simple8b' level 'high' ) ",
|
||||
};
|
||||
|
||||
// str true
|
||||
const char *s1 [] = {
|
||||
"create table st(ts timestamp ",
|
||||
"create table st(ts timestamp, age int ",
|
||||
"create table st(ts timestamp, age int ,",
|
||||
"create table st(ts timestamp, age int , name binary(16), ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr varbinary(32) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary(32)",
|
||||
"create table st(ts timestamp, age int, name binary(16)) tags(area int,addr varbinary(32) ",
|
||||
"create table st( ts timestamp, age int, name binary(16)) tags(area int,addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int, addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) compress 'zlib' ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level 'high' ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode 'simple8b' level 'high' ",
|
||||
const char *s1[] = {
|
||||
"create table st(ts timestamp ",
|
||||
"create table st(ts timestamp, age int ",
|
||||
"create table st(ts timestamp, age int ,",
|
||||
"create table st(ts timestamp, age int , name binary(16), ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr varbinary(32) ",
|
||||
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary(32)",
|
||||
"create table st(ts timestamp, age int, name binary(16)) tags(area int,addr varbinary(32) ",
|
||||
"create table st( ts timestamp, age int, name binary(16)) tags(area int,addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int, addr varbinary(32) ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) compress "
|
||||
"'zlib' ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level "
|
||||
"'high' ",
|
||||
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode "
|
||||
"'simple8b' level 'high' ",
|
||||
};
|
||||
|
||||
// s0 is false
|
||||
for(int32_t i = 0; i < sizeof(s0)/sizeof(char*) ; i++) {
|
||||
for (int32_t i = 0; i < sizeof(s0) / sizeof(char *); i++) {
|
||||
printf("s0 i=%d isCreateFieldsArea %s expect false. \n", i, s0[i]);
|
||||
ASSERT (isCreateFieldsArea((char *)s0[i]) == false);
|
||||
ASSERT(isCreateFieldsArea((char *)s0[i]) == false);
|
||||
}
|
||||
|
||||
// s1 is true
|
||||
for(int32_t i = 0; i < sizeof(s1)/sizeof(char*) ; i++) {
|
||||
for (int32_t i = 0; i < sizeof(s1) / sizeof(char *); i++) {
|
||||
printf("s1 i=%d isCreateFieldsArea %s expect true. \n", i, s1[i]);
|
||||
ASSERT (isCreateFieldsArea((char *)s1[i]) == true);
|
||||
ASSERT(isCreateFieldsArea((char *)s1[i]) == true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue