From 5bc3dc1bbb7abb22b6d3ac38856750e396aeee5d Mon Sep 17 00:00:00 2001 From: Hui Li Date: Mon, 22 Jun 2020 15:52:29 +0800 Subject: [PATCH] [modify for covrity scan] --- src/kit/shell/src/shellEngine.c | 7 +++++-- src/kit/shell/src/shellImport.c | 5 ++++- src/kit/shell/src/shellLinux.c | 2 +- src/kit/taosdemo/taosdemo.c | 33 +++++++++++++++++++-------------- src/kit/taosdump/taosdump.c | 26 ++++++++++++++++---------- src/os/linux/src/linuxSysPara.c | 10 +++++----- src/util/inc/tutil.h | 2 ++ src/util/src/tnote.c | 2 ++ src/util/src/tutil.c | 19 +++++++++++++++++++ 9 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index e28c691a89..044e167af7 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 347f99671d..ba123ac2d4 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 829ceb9e5f..0325fa34f9 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 4a455271ff..81a98e0fcc 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -82,7 +82,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 +432,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 +845,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 +940,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 +985,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 +1031,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 ccef783709..46b14d6e56 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 0e76ab0046..31ae33cdfc 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 a314f0e31d..94084b5638 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 a8d9e8d416..20c77d4fbe 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 aa5bfe322a..ccdf7111a7 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) { -- GitLab