diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index e28c691a8986fce0bcb7a04b8bb1e5f7f5c6605c..044e167af7938d3ce1d15e9e097c52cc9a1ef2e1 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -474,7 +474,7 @@ static int dumpResultToFile(const char* fname, TAOS_RES* tres) { } while( row != NULL); result = NULL; - taos_free_result(tres); + //taos_free_result(tres); fclose(fp); return numOfRows; @@ -803,6 +803,7 @@ void source_file(TAOS *con, char *fptr) { char *fname = full_path.we_wordv[0]; + /* if (access(fname, F_OK) != 0) { fprintf(stderr, "ERROR: file %s is not exist\n", fptr); @@ -810,6 +811,7 @@ void source_file(TAOS *con, char *fptr) { free(cmd); return; } + */ FILE *f = fopen(fname, "r"); if (f == NULL) { @@ -849,7 +851,7 @@ void source_file(TAOS *con, char *fptr) { void shellGetGrantInfo(void *con) { return; - +#if 0 char sql[] = "show grants"; TAOS_RES* tres = taos_query(con, sql); @@ -900,4 +902,5 @@ void shellGetGrantInfo(void *con) { } fprintf(stdout, "\n"); + #endif } diff --git a/src/kit/shell/src/shellImport.c b/src/kit/shell/src/shellImport.c index 347f99671db1bc9147c98d97ec708a3bf8bcf689..ba123ac2d4b51ec8a5d30460dae302c585bf051a 100644 --- a/src/kit/shell/src/shellImport.c +++ b/src/kit/shell/src/shellImport.c @@ -73,7 +73,7 @@ static void shellParseDirectory(const char *directoryName, const char *prefix, c } int fileNum = 0; - while (fscanf(fp, "%s", fileArray[fileNum++])) { + while (fscanf(fp, "%128s", fileArray[fileNum++])) { if (strcmp(fileArray[fileNum-1], shellTablesSQLFile) == 0) { fileNum--; } @@ -150,9 +150,11 @@ static void shellSourceFile(TAOS *con, char *fptr) { char *fname = full_path.we_wordv[0]; if (fname == NULL) { fprintf(stderr, "ERROR: invalid filename\n"); + free(cmd); return; } + /* if (access(fname, F_OK) != 0) { fprintf(stderr, "ERROR: file %s is not exist\n", fptr); @@ -168,6 +170,7 @@ static void shellSourceFile(TAOS *con, char *fptr) { free(cmd); return; } + */ FILE *f = fopen(fname, "r"); if (f == NULL) { diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index 829ceb9e5f19107ef65c5a3884bffd8a03e593b8..0325fa34f92914ab1b109eba874d66fdf1fb1173 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -162,7 +162,7 @@ void shellReadCommand(TAOS *con, char *command) { // Read input. char c; while (1) { - c = getchar(); + c = (char)getchar(); // getchar() return an 'int' value if (c < 0) { // For UTF-8 int count = countPrefixOnes(c); diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 4a455271fff51e1bd9438ce01d044eee3760f6c2..1dd31779fe9c786c0bcc86487edbe87dc24faa15 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -34,6 +34,7 @@ #include #include "taos.h" +#include "tutil.h" extern char configDir[]; @@ -82,7 +83,7 @@ typedef struct DemoArguments { bool insert_only; char *output_file; int mode; - char *datatype[MAX_NUM_DATATYPE]; + char *datatype[MAX_NUM_DATATYPE+1]; int len_of_binary; int num_of_CPR; int num_of_threads; @@ -432,7 +433,7 @@ int main(int argc, char *argv[]) { tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); printf("###################################################################\n\n"); printf("Press enter key to continue"); - getchar(); + (void)getchar(); fprintf(fp, "###################################################################\n"); fprintf(fp, "# Server IP: %s:%hu\n", ip_addr == NULL ? "localhost" : ip_addr, port); @@ -845,10 +846,10 @@ void *syncWrite(void *sarg) { pstr += sprintf(pstr, "insert into %s.%s%d values", winfo->db_name, winfo->tb_prefix, tID); int k; for (k = 0; k < winfo->nrecords_per_request;) { - int rand_num = rand() % 100; + int rand_num = trand() % 100; int len = -1; if (winfo->data_of_order ==1 && rand_num < winfo->data_of_rate) { - long d = tmp_time - rand() % 1000000 + rand_num; + long d = tmp_time - trand() % 1000000 + rand_num; len = generateData(data, data_type, ncols_per_record, d, len_of_binary); } else { len = generateData(data, data_type, ncols_per_record, tmp_time += 1000, len_of_binary); @@ -940,10 +941,10 @@ void callBack(void *param, TAOS_RES *res, int code) { pstr += sprintf(pstr, "insert into %s values", tb_info->tb_name); for (int i = 0; i < tb_info->nrecords_per_request; i++) { - int rand_num = rand() % 100; + int rand_num = trand() % 100; if (tb_info->data_of_order ==1 && rand_num < tb_info->data_of_rate) { - long d = tmp_time - rand() % 1000000 + rand_num; + long d = tmp_time - trand() % 1000000 + rand_num; generateData(data, datatype, ncols_per_record, d, len_of_binary); } else { @@ -985,22 +986,27 @@ int32_t generateData(char *res, char **data_type, int num_of_cols, int64_t times } } + if (0 == c) { + perror("data type error!"); + exit(-1); + } + for (int i = 0; i < num_of_cols; i++) { if (strcasecmp(data_type[i % c], "tinyint") == 0) { - pstr += sprintf(pstr, ", %d", (int)(rand() % 128)); + pstr += sprintf(pstr, ", %d", (int)(trand() % 128)); } else if (strcasecmp(data_type[i % c], "smallint") == 0) { - pstr += sprintf(pstr, ", %d", (int)(rand() % 32767)); + pstr += sprintf(pstr, ", %d", (int)(trand() % 32767)); } else if (strcasecmp(data_type[i % c], "int") == 0) { - pstr += sprintf(pstr, ", %d", (int)(rand() % 10)); + pstr += sprintf(pstr, ", %d", (int)(trand() % 10)); } else if (strcasecmp(data_type[i % c], "bigint") == 0) { - pstr += sprintf(pstr, ", %" PRId64, rand() % 2147483648); + pstr += sprintf(pstr, ", %" PRId64, trand() % 2147483648); } else if (strcasecmp(data_type[i % c], "float") == 0) { - pstr += sprintf(pstr, ", %10.4f", (float)(rand() / 1000)); + pstr += sprintf(pstr, ", %10.4f", (float)(trand() / 1000)); } else if (strcasecmp(data_type[i % c], "double") == 0) { - double t = (double)(rand() / 1000000); + double t = (double)(trand() / 1000000); pstr += sprintf(pstr, ", %20.8f", t); } else if (strcasecmp(data_type[i % c], "bool") == 0) { - bool b = rand() & 1; + bool b = trand() & 1; pstr += sprintf(pstr, ", %s", b ? "true" : "false"); } else if (strcasecmp(data_type[i % c], "binary") == 0) { char s[len_of_binary]; @@ -1026,7 +1032,7 @@ void rand_string(char *str, int size) { --size; int n; for (n = 0; n < size; n++) { - int key = rand() % (int)(sizeof charset - 1); + int key = trand() % (int)(sizeof charset - 1); str[n] = charset[key]; } str[n] = 0; diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index ccef783709e6cee69ff1178d939716b33a410e39..46b14d6e56b5a526a9d84d40735ffe3f6292abcd 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -229,7 +229,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { fprintf(stderr, "Invalid path %s\n", arg); return -1; } - strcpy(arguments->output, full_path.we_wordv[0]); + tstrncpy(arguments->output, full_path.we_wordv[0], TSDB_FILENAME_LEN); wordfree(&full_path); break; case 'i': @@ -411,7 +411,7 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) { if ((row = taos_fetch_row(result)) != NULL) { isSet = true; pTableRecordInfo->isMetric = true; - strcpy(pTableRecordInfo->tableRecord.metric, table); + tstrncpy(pTableRecordInfo->tableRecord.metric, table, TSDB_TABLE_NAME_LEN); } taos_free_result(result); @@ -642,9 +642,12 @@ int taosDumpDb(SDbInfo *dbInfo, SDumpArguments *arguments, FILE *fp) { taos_free_result(result); - lseek(fd, 0, SEEK_SET); + (void)lseek(fd, 0, SEEK_SET); - while (read(fd, &tableRecord, sizeof(STableRecord)) > 0) { + while (1) { + ssize_t ret = read(fd, &tableRecord, sizeof(STableRecord)); + if (ret <= 0) break; + tableRecord.name[sizeof(tableRecord.name) - 1] = 0; tableRecord.metric[sizeof(tableRecord.metric) - 1] = 0; taosDumpTable(tableRecord.name, tableRecord.metric, arguments, fp); @@ -807,7 +810,7 @@ int taosGetTableDes(char *table, STableDef *tableDes) { TAOS_FIELD *fields = taos_fetch_fields(result); - strcpy(tableDes->name, table); + tstrncpy(tableDes->name, table, TSDB_COL_NAME_LEN); while ((row = taos_fetch_row(result)) != NULL) { strncpy(tableDes->cols[count].field, (char *)row[TSDB_DESCRIBE_METRIC_FIELD_INDEX], @@ -903,16 +906,19 @@ int32_t taosDumpMetric(char *metric, SDumpArguments *arguments, FILE *fp) { taos_free_result(result); result = NULL; - lseek(fd, 0, SEEK_SET); + (void)lseek(fd, 0, SEEK_SET); - while (read(fd, &tableRecord, sizeof(STableRecord)) > 0) { + while (1) { + ssize_t ret = read(fd, &tableRecord, sizeof(STableRecord)); + if (ret <= 0) break; + tableRecord.name[sizeof(tableRecord.name) - 1] = 0; tableRecord.metric[sizeof(tableRecord.metric) - 1] = 0; taosDumpTable(tableRecord.name, tableRecord.metric, arguments, fp); } tclose(fd); - remove(".table.tmp"); + (void)remove(".table.tmp"); return 0; } @@ -1004,7 +1010,7 @@ int taosDumpTableData(FILE *fp, char *tbname, SDumpArguments *arguments) { break; } } - pstr += sprintf(pstr, ")"); + sprintf(pstr, ")"); count++; fprintf(fp, "%s", buffer); @@ -1327,7 +1333,7 @@ int convertNCharToReadable(char *str, int size, char *buf, int bufsize) { if ((int)wc < 256) { pbuf = stpcpy(pbuf, ascii_literal_list[(int)wc]); - } else { + } else if (byte_width > 0) { memcpy(pbuf, pstr, byte_width); pbuf += byte_width; } diff --git a/src/os/linux/src/linuxSysPara.c b/src/os/linux/src/linuxSysPara.c index 0e76ab00469540f7c31a2508ec51ff828d9e8d4c..31ae33cdfc7c7c71064f3ee21bc690ae57656596 100644 --- a/src/os/linux/src/linuxSysPara.c +++ b/src/os/linux/src/linuxSysPara.c @@ -162,7 +162,7 @@ static void taosGetSystemTimezone() { FILE *f = fopen("/etc/timezone", "r"); char buf[65] = {0}; if (f != NULL) { - fread(buf, 64, 1, f); + (void)fread(buf, 64, 1, f); fclose(f); } @@ -547,7 +547,7 @@ void taosSetCoreDump() { struct rlimit rlim; struct rlimit rlim_new; if (getrlimit(RLIMIT_CORE, &rlim) == 0) { - uPrint("the old unlimited para: rlim_cur=%d, rlim_max=%d", rlim.rlim_cur, rlim.rlim_max); + uPrint("the old unlimited para: rlim_cur=%d, rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); rlim_new.rlim_cur = RLIM_INFINITY; rlim_new.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { @@ -559,7 +559,7 @@ void taosSetCoreDump() { } if (getrlimit(RLIMIT_CORE, &rlim) == 0) { - uPrint("the new unlimited para: rlim_cur=%d, rlim_max=%d", rlim.rlim_cur, rlim.rlim_max); + uPrint("the new unlimited para: rlim_cur=%d, rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); } #ifndef _TD_ARM_ @@ -586,7 +586,7 @@ void taosSetCoreDump() { uPrint("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); } - uPrint("The old core_uses_pid[%d]: %d", old_len, old_usespid); + uPrint("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); old_usespid = 0; @@ -603,7 +603,7 @@ void taosSetCoreDump() { uPrint("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); } - uPrint("The new core_uses_pid[%d]: %d", old_len, old_usespid); + uPrint("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); #endif #if 0 diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index a314f0e31ddde3e1e58c9c8ba7bcfd89efe4b1d2..94084b5638a497e07a6ab4bd29fa51749d671dfb 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -119,6 +119,8 @@ extern "C" { uint32_t taosRand(void); +uint32_t trand(void); + size_t twcslen(const wchar_t *wcs); int32_t strdequote(char *src); diff --git a/src/util/src/tnote.c b/src/util/src/tnote.c index a8d9e8d416ce9c9ee2653a665ecb080930e2c7ab..20c77d4fbe14b9f8151abe314e9e1df369931edc 100644 --- a/src/util/src/tnote.c +++ b/src/util/src/tnote.c @@ -128,10 +128,12 @@ int taosOpenNewNote(taosNoteInfo * pNote) bool taosCheckNoteIsOpen(char *noteName, taosNoteInfo * pNote) { + /* int exist = access(noteName, F_OK); if (exist != 0) { return false; } + */ int fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd < 0) { diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index aa5bfe322a9e0912ae13eecdaa75783505986648..ccdf7111a7264726d35a4bf938e851b1fa5c3edf 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -55,6 +55,25 @@ uint32_t taosRand(void) */ return rand(); } + +uint32_t trand(void) +{ + int fd; + int seed; + + fd = open("/dev/urandom", 0); + if (fd < 0) { + seed = time(0); + } else { + int len = read(fd, &seed, sizeof(seed)); + if (len < 0) { + seed = time(0); + } + close(fd); + } + + return (uint32_t)seed; +} #endif size_t twcslen(const wchar_t *wcs) {