From be39146fd2b17777d2f8ee55832f77784b6586bb Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Sun, 18 Apr 2021 20:19:26 +0200 Subject: [PATCH] PVS Studio refactoring Signed-off-by: Janos SUTO --- src/archive.c | 2 +- src/digest.c | 4 ++-- src/imap.c | 30 ++++++++++++++++++------------ src/misc.c | 9 +++------ src/parser.c | 4 ++-- src/parser.h | 2 +- src/parser_utils.c | 40 ++++------------------------------------ src/piler.c | 2 +- src/pilerexport.c | 44 +++++++++++++++++++++++++++++--------------- src/pilerimport.c | 2 +- src/rules.c | 17 ++++++----------- src/smtp.c | 2 +- src/store.c | 2 +- 13 files changed, 70 insertions(+), 90 deletions(-) diff --git a/src/archive.c b/src/archive.c index 8ea7b504..97e5a376 100644 --- a/src/archive.c +++ b/src/archive.c @@ -244,7 +244,7 @@ int retrieve_file_from_archive(char *filename, int mode, char **buffer, FILE *de CLEANUP: - if(fd != -1) close(fd); + if(fd != -1) close(fd); //-V547 if(s) free(s); if(cfg->encrypt_messages == 1) #if OPENSSL_VERSION_NUMBER < 0x10100000L diff --git a/src/digest.c b/src/digest.c index 6899dba1..20134730 100644 --- a/src/digest.c +++ b/src/digest.c @@ -81,8 +81,8 @@ int make_digests(struct session_data *sdata, struct config *cfg){ SHA256_Final(md2, &context2); for(i=0;ibodydigest + i*2, 2*DIGEST_LENGTH, "%02x", md[i]); - snprintf(sdata->digest + i*2, 2*DIGEST_LENGTH, "%02x", md2[i]); + snprintf(sdata->bodydigest + i*2, 3, "%02x", md[i]); + snprintf(sdata->digest + i*2, 3, "%02x", md2[i]); } return 0; diff --git a/src/imap.c b/src/imap.c index bd5008ac..ade89446 100644 --- a/src/imap.c +++ b/src/imap.c @@ -416,7 +416,7 @@ int list_folders(struct data *data){ } // trim the "A3 OK LIST completed" trailer off - if(p) *p = '\0'; + if(p) *p = '\0'; //-V547 memset(attrs, 0, sizeof(attrs)); @@ -449,21 +449,27 @@ int list_folders(struct data *data){ } else { if(fldrlen) { - ruf = malloc(strlen(q) * 2 + 1); - memset(ruf, 0, strlen(q) * 2 + 1); - memcpy(ruf, q, strlen(q)); - r = ruf; - while(*r != '\0') { - if(*r == '\\') { - memmove(r + 1, r, strlen(r)); + int ruflen = strlen(q) * 2; + ruf = malloc(ruflen + 1); + if(ruf){ + snprintf(ruf, ruflen, "%s", q); + r = ruf; + while(*r != '\0') { + if(*r == '\\') { + memmove(r + 1, r, strlen(r)); + r++; + } r++; } - r++; + + snprintf(folder, sizeof(folder)-1, "%s", ruf); + + free(ruf); + } + else { + printf("error: ruf = malloc()\n"); } - snprintf(folder, sizeof(folder)-1, "%s", ruf); - - free(ruf); fldrlen = 0; } else { snprintf(folder, sizeof(folder)-1, "%s", q); diff --git a/src/misc.c b/src/misc.c index e5fe4c42..703b21d0 100644 --- a/src/misc.c +++ b/src/misc.c @@ -230,11 +230,8 @@ int extractEmail(char *rawmail, char *email){ */ void make_random_string(unsigned char *buf, int buflen){ - int i, len, fd; - int urandom=0; - static char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - len = strlen(alphanum); + const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + int i, fd, urandom=0, len = sizeof(alphanum)-1; fd = open(RANDOM_POOL, O_RDONLY); if(fd != -1){ @@ -289,7 +286,7 @@ int get_random_bytes(unsigned char *buf, int len, unsigned char server_id){ taia_now(&now); taia_pack(nowpack, &now); - memcpy(buf, nowpack, 12); + memcpy(buf, nowpack, 12); //-V512 fd = open(RANDOM_POOL, O_RDONLY); if(fd == -1) return ret; diff --git a/src/parser.c b/src/parser.c index dbaa6277..feaae3fa 100644 --- a/src/parser.c +++ b/src/parser.c @@ -45,7 +45,7 @@ struct parser_state parse_message(struct session_data *sdata, int take_into_piec if(take_into_pieces == 1 && state.writebufpos > 0){ if(write(state.mfd, writebuffer, state.writebufpos) == -1) syslog(LOG_PRIORITY, "ERROR: %s: write(), %s, %d, %s", sdata->ttmpfile, __func__, __LINE__, __FILE__); - memset(writebuffer, 0, sizeof(writebuffer)); + memset(writebuffer, 0, sizeof(writebuffer)); //-V597 state.writebufpos = 0; } @@ -212,7 +212,7 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata sdata->restored_copy = 1; } - if(cfg->security_header && state->found_security_header == 0 && strstr(buf, cfg->security_header)){ + if(cfg->security_header[0] && state->found_security_header == 0 && strstr(buf, cfg->security_header)){ state->found_security_header = 1; } diff --git a/src/parser.h b/src/parser.h index 5f536fd7..c9438491 100644 --- a/src/parser.h +++ b/src/parser.h @@ -20,7 +20,7 @@ void fixupEncodedHeaderLine(char *buf, int buflen); void fixupSoftBreakInQuotedPritableLine(char *buf, struct parser_state *state); void fixupBase64EncodedLine(char *buf, struct parser_state *state); void markHTML(char *buf, struct parser_state *state); -int appendHTMLTag(char *buf, char *htmlbuf, int pos, struct parser_state *state); +void setStateHTMLStyle(char *htmlbuf, int pos, struct parser_state *state); void translateLine(unsigned char *p, struct parser_state *state); void fix_email_address_for_sphinx(char *s); void split_email_address(char *s); diff --git a/src/parser_utils.c b/src/parser_utils.c index abc63ad2..d2fa5dce 100644 --- a/src/parser_utils.c +++ b/src/parser_utils.c @@ -538,7 +538,7 @@ void markHTML(char *buf, struct parser_state *state){ if(isspace(*s)){ if(j > 0){ - k += appendHTMLTag(puf, html, pos, state); + setStateHTMLStyle(html, pos, state); memset(html, 0, SMALLBUFSIZE); j=0; } pos++; @@ -563,7 +563,7 @@ void markHTML(char *buf, struct parser_state *state){ if(j > 0){ strncat(html, " ", SMALLBUFSIZE-1); - k += appendHTMLTag(puf, html, pos, state); + setStateHTMLStyle(html, pos, state); memset(html, 0, SMALLBUFSIZE); j=0; } } @@ -571,47 +571,15 @@ void markHTML(char *buf, struct parser_state *state){ } //printf("append last in line:*%s*, html=+%s+, j=%d\n", puf, html, j); - if(j > 0){ appendHTMLTag(puf, html, pos, state); } + if(j > 0){ setStateHTMLStyle(html, pos, state); } strcpy(buf, puf); } -int appendHTMLTag(char *buf, char *htmlbuf, int pos, struct parser_state *state){ - char html[SMALLBUFSIZE]; - int len; - +void setStateHTMLStyle(char *htmlbuf, int pos, struct parser_state *state){ if(pos == 0 && strncmp(htmlbuf, "style ", 6) == 0) state->style = 1; if(pos == 0 && strncmp(htmlbuf, "/style ", 7) == 0) state->style = 0; - - return 0; - - //printf("appendHTML: pos:%d, +%s+\n", pos, htmlbuf); - - if(state->style == 1) return 0; - - if(strlen(htmlbuf) == 0) return 0; - - snprintf(html, SMALLBUFSIZE-1, "HTML*%s", htmlbuf); - len = strlen(html); - - if(len > 8 && strchr(html, '=')){ - char *p = strstr(html, "cid:"); - if(p){ - *(p+3) = '\0'; - strncat(html, " ", SMALLBUFSIZE-1); - } - - strncat(buf, html, MAXBUFSIZE-1); - return len; - } - - if(strstr(html, "http") ){ - strncat(buf, html+5, MAXBUFSIZE-1); - return len-5; - } - - return 0; } diff --git a/src/piler.c b/src/piler.c index d26016cf..cfab8825 100644 --- a/src/piler.c +++ b/src/piler.c @@ -103,7 +103,7 @@ void child_sighup_handler(int sig){ int perform_checks(char *filename, struct session_data *sdata, struct data *data, struct parser_state *parser_state, struct config *cfg){ - if(cfg->security_header && parser_state->found_security_header == 0){ + if(cfg->security_header[0] && parser_state->found_security_header == 0){ syslog(LOG_PRIORITY, "%s: discarding: missing security header", filename); return ERR_DISCARDED; } diff --git a/src/pilerexport.c b/src/pilerexport.c index bdf7ba57..5ee47c86 100644 --- a/src/pilerexport.c +++ b/src/pilerexport.c @@ -25,7 +25,6 @@ extern int optind; int dryrun = 0; int exportall = 0; int verification_status = 0; -int rc = 0; int export_to_stdout = 0; char *query=NULL; int verbosity = 0; @@ -33,7 +32,7 @@ int max_matches = 1000; char *index_list = "main1,dailydelta1,delta1"; regex_t regexp; char *zipfile = NULL; -struct zip *z = NULL; +struct zip *zip = NULL; uint64 *zip_ids = NULL; int zip_counter = 0; int zip_batch = 2000; @@ -165,6 +164,7 @@ uint64 run_query(struct session_data *sdata, struct session_data *sdata2, char * MYSQL_ROW row; uint64 id=0; char s[SMALLBUFSIZE]; + int rc=0; *num = 0; @@ -194,6 +194,7 @@ uint64 run_query(struct session_data *sdata, struct session_data *sdata2, char * } if(!rc) export_emails_matching_to_query(sdata, query, cfg); + else printf("error: append_string_to_buffer() in run_query()\n"); free(query); query = NULL; @@ -240,6 +241,7 @@ void export_emails_matching_id_list(struct session_data *sdata, struct session_d int build_query_from_args(char *from, char *to, char *fromdomain, char *todomain, int minsize, int maxsize, unsigned long startdate, unsigned long stopdate){ char s[SMALLBUFSIZE]; + int rc=0; if(exportall == 1){ rc = append_string_to_buffer(&query, "SELECT `id`, `piler_id`, `digest`, `bodydigest` FROM "); @@ -328,9 +330,9 @@ int build_query_from_args(char *from, char *to, char *fromdomain, char *todomain #if LIBZIP_VERSION_MAJOR >= 1 void zip_flush(){ - zip_close(z); + zip_close(zip); - z = NULL; + zip = NULL; zip_counter = 0; if(!zip_ids) return; @@ -354,7 +356,7 @@ int export_emails_matching_to_query(struct session_data *sdata, char *s, struct char digest[SMALLBUFSIZE], bodydigest[SMALLBUFSIZE]; char filename[SMALLBUFSIZE]; struct sql sql; - int errorp; + int errorp, rc=0; if(prepare_sql_statement(sdata, &sql, s) == ERR) return ERR; @@ -410,9 +412,9 @@ int export_emails_matching_to_query(struct session_data *sdata, char *s, struct if(zipfile){ #if LIBZIP_VERSION_MAJOR >= 1 // Open zip file if handler is NULL - if(!z){ - z = zip_open(zipfile, ZIP_CREATE, &errorp); - if(!z){ + if(!zip){ + zip = zip_open(zipfile, ZIP_CREATE, &errorp); + if(!zip){ printf("error: error creating zip file=%s, error code=%d\n", zipfile, errorp); return ERR; } @@ -425,12 +427,12 @@ int export_emails_matching_to_query(struct session_data *sdata, char *s, struct return ERR; } - zip_source_t *zs = zip_source_file(z, filename, 0, 0); - if(zs && zip_file_add(z, filename, zs, ZIP_FL_ENC_UTF_8) >= 0){ + zip_source_t *zs = zip_source_file(zip, filename, 0, 0); + if(zs && zip_file_add(zip, filename, zs, ZIP_FL_ENC_UTF_8) >= 0){ *(zip_ids+zip_counter) = id; zip_counter++; } else { - printf("error adding file %s: %s\n", filename, zip_strerror(z)); + printf("error adding file %s: %s\n", filename, zip_strerror(zip)); return ERR; } @@ -539,7 +541,10 @@ int main(int argc, char **argv){ break; } - rc = append_email_to_buffer(&from, optarg); + if(append_email_to_buffer(&from, optarg)){ + printf("error: append_email_to_buffer() for from\n"); + return 1; + } break; @@ -550,7 +555,10 @@ int main(int argc, char **argv){ break; } - rc = append_email_to_buffer(&to, optarg); + if(append_email_to_buffer(&to, optarg)){ + printf("error: append_email_to_buffer() for to\n"); + return 1; + } break; @@ -561,7 +569,10 @@ int main(int argc, char **argv){ break; } - rc = append_email_to_buffer(&fromdomain, optarg); + if(append_email_to_buffer(&fromdomain, optarg)){ + printf("error: append_email_to_buffer() for fromdomain\n"); + return 1; + } break; @@ -572,7 +583,10 @@ int main(int argc, char **argv){ break; } - rc = append_email_to_buffer(&todomain, optarg); + if(append_email_to_buffer(&todomain, optarg)){ + printf("error: append_email_to_buffer() for todomain\n"); + return 1; + } break; diff --git a/src/pilerimport.c b/src/pilerimport.c index 51ce727c..3be6541f 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -295,7 +295,7 @@ int main(int argc, char **argv){ } - if(!mbox[0] && !data.import->mboxdir && !data.import->filename && !directory && !imapserver && !pop3server) usage(); + if(!mbox[0] && !data.import->mboxdir && !data.import->filename[0] && !directory && !imapserver && !pop3server) usage(); if(data.import->failed_folder && !can_i_write_directory(data.import->failed_folder)){ printf("cannot write failed directory '%s'\n", data.import->failed_folder); diff --git a/src/rules.c b/src/rules.c index 37c8d3cf..d2cd7c7e 100644 --- a/src/rules.c +++ b/src/rules.c @@ -118,7 +118,6 @@ int append_rule(struct node *xhash[], struct rule_cond *rule_cond){ struct rule *create_rule_item(struct rule_cond *rule_cond){ struct rule *h=NULL; - char empty = '\0'; int len; if(rule_cond == NULL) return NULL; @@ -144,16 +143,16 @@ struct rule *create_rule_item(struct rule_cond *rule_cond){ h->emptyfrom = h->emptyto = h->emptysubject = h->emptyaname = h->emptyatype = 0; - if(rule_cond->from == NULL || strlen(rule_cond->from) < 1){ rule_cond->from[0] = empty; h->emptyfrom = 1; } + if(rule_cond->from[0] == 0){ h->emptyfrom = 1; } if(regcomp(&(h->from), rule_cond->from, REG_ICASE | REG_EXTENDED)) h->compiled = 0; - if(rule_cond->to == NULL || strlen(rule_cond->to) < 1){ rule_cond->to[0] = empty; h->emptyto = 1; } + if(rule_cond->to[0] == 0){ h->emptyto = 1; } if(regcomp(&(h->to), rule_cond->to, REG_ICASE | REG_EXTENDED)) h->compiled = 0; - if(rule_cond->subject == NULL || strlen(rule_cond->subject) < 1){ rule_cond->subject[0] = empty; h->emptysubject = 1; } + if(rule_cond->subject[0] == 0){ h->emptysubject = 1; } if(regcomp(&(h->subject), rule_cond->subject, REG_ICASE | REG_EXTENDED)) h->compiled = 0; - if(rule_cond->body == NULL || strlen(rule_cond->body) < 1){ rule_cond->body[0] = empty; h->emptybody = 1; } + if(rule_cond->body[0] == 0){ h->emptybody = 1; } if(regcomp(&(h->body), rule_cond->body, REG_ICASE | REG_EXTENDED)) h->compiled = 0; h->spam = rule_cond->spam; @@ -161,20 +160,16 @@ struct rule *create_rule_item(struct rule_cond *rule_cond){ h->folder_id = rule_cond->folder_id; h->size = rule_cond->size; - - if(rule_cond->_size == NULL) rule_cond->_size[0] = empty; snprintf(h->_size, 3, "%s", rule_cond->_size); - if(rule_cond->attachment_name == NULL || strlen(rule_cond->attachment_name) < 1){ rule_cond->attachment_name[0] = empty; h->emptyaname = 1; } + if(rule_cond->attachment_name[0] == 0){ h->emptyaname = 1; } if(regcomp(&(h->attachment_name), rule_cond->attachment_name, REG_ICASE | REG_EXTENDED)) h->compiled = 0; - if(rule_cond->attachment_type == NULL || strlen(rule_cond->attachment_type) < 1){ rule_cond->attachment_type[0] = empty; h->emptyatype = 1; } + if(rule_cond->attachment_type[0] == 0){ h->emptyatype = 1; } if(regcomp(&(h->attachment_type), rule_cond->attachment_type, REG_ICASE | REG_EXTENDED)) h->compiled = 0; h->attachment_size = rule_cond->attachment_size; - - if(rule_cond->_attachment_size == NULL) rule_cond->_attachment_size[0] = empty; snprintf(h->_attachment_size, 3, "%s", rule_cond->_attachment_size); len = strlen(rule_cond->domain)+8 + strlen(rule_cond->from)+6 + strlen(rule_cond->to)+4 + strlen(rule_cond->subject)+9 + strlen(rule_cond->body)+6 + strlen(rule_cond->_size)+6 + strlen(rule_cond->attachment_name)+10 + strlen(rule_cond->attachment_type)+10 + strlen(rule_cond->_attachment_size)+10 + 9 + 15 + 15; diff --git a/src/smtp.c b/src/smtp.c index 8aa4c3ba..5cd69fe6 100644 --- a/src/smtp.c +++ b/src/smtp.c @@ -314,7 +314,7 @@ void process_command_period(struct smtp_session *session){ session->buflen = 0; session->last_data_char = 0; - memset(session->buf, 0, SMALLBUFSIZE); + memset(session->buf, 0, sizeof(session->buf)); send_smtp_response(session, buf); } diff --git a/src/store.c b/src/store.c index 31323f53..99abc307 100644 --- a/src/store.c +++ b/src/store.c @@ -228,7 +228,7 @@ int store_file(struct session_data *sdata, char *filename, int len, struct confi ENDE: if(outbuf) free(outbuf); - if(z) free(z); + if(z) free(z); //-V547 return ret; }