From 7287f31ee13f0aaa32f68c466a21d61774c0ce67 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 5 Apr 2017 15:20:47 +0100 Subject: [PATCH] coverity: test functions Test the return value of some functions that could fail. --- attach.c | 3 ++- copy.c | 15 ++++++++++----- crypt-gpgme.c | 6 +++--- menu.c | 7 +++++-- pager.c | 3 ++- parse.c | 3 ++- pop_lib.c | 5 ++++- send.c | 16 ++++++++++------ 8 files changed, 38 insertions(+), 20 deletions(-) diff --git a/attach.c b/attach.c index b39f0f8b0..892e9b62c 100644 --- a/attach.c +++ b/attach.c @@ -729,7 +729,8 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr) hn->msgno = hdr->msgno; /* required for MH/maildir */ hn->read = true; - fseeko (fp, m->offset, SEEK_SET); + if (fseeko (fp, m->offset, SEEK_SET) < 0) + return -1; if (fgets (buf, sizeof (buf), fp) == NULL) return -1; if (mx_open_mailbox(path, MUTT_APPEND | MUTT_QUIET, &ctx) == NULL) diff --git a/copy.c b/copy.c index dd621bfd5..e8cf4960d 100644 --- a/copy.c +++ b/copy.c @@ -64,7 +64,8 @@ mutt_copy_hdr (FILE *in, FILE *out, LOFF_T off_start, LOFF_T off_end, int flags, return -1; if (ftello (in) != off_start) - fseeko (in, off_start, SEEK_SET); + if (fseeko (in, off_start, SEEK_SET) < 0) + return -1; buf[0] = '\n'; buf[1] = 0; @@ -575,7 +576,8 @@ _mutt_copy_message (FILE *fpout, FILE *fpin, HEADER *hdr, BODY *body, new_offset = ftello (fpout); /* Copy the body */ - fseeko (fpin, body->offset, SEEK_SET); + if (fseeko (fpin, body->offset, SEEK_SET) < 0) + return -1; if (copy_delete_attach (body, fpin, fpout, date)) return -1; @@ -678,7 +680,8 @@ _mutt_copy_message (FILE *fpout, FILE *fpin, HEADER *hdr, BODY *body, mutt_write_mime_header (cur, fpout); fputc ('\n', fpout); - fseeko (fp, cur->offset, SEEK_SET); + if (fseeko (fp, cur->offset, SEEK_SET) < 0) + return -1; if (mutt_copy_bytes (fp, fpout, cur->length) == -1) { safe_fclose (&fp); @@ -690,7 +693,8 @@ _mutt_copy_message (FILE *fpout, FILE *fpin, HEADER *hdr, BODY *body, } else { - fseeko (fpin, body->offset, SEEK_SET); + if (fseeko (fpin, body->offset, SEEK_SET) < 0) + return -1; if (flags & MUTT_CM_PREFIX) { int c; @@ -760,7 +764,8 @@ _mutt_append_message (CONTEXT *dest, FILE *fpin, CONTEXT *src, HEADER *hdr, MESSAGE *msg = NULL; int r; - fseeko (fpin, hdr->offset, SEEK_SET); + if (fseeko (fpin, hdr->offset, SEEK_SET) < 0) + return -1; if (fgets (buf, sizeof (buf), fpin) == NULL) return -1; diff --git a/crypt-gpgme.c b/crypt-gpgme.c index fcb2be151..c16fe626b 100644 --- a/crypt-gpgme.c +++ b/crypt-gpgme.c @@ -530,7 +530,7 @@ static int data_object_to_stream (gpgme_data_t data, FILE *fp) return -1; } - while ((nread = gpgme_data_read (data, buf, sizeof (buf)))) + while ((nread = gpgme_data_read (data, buf, sizeof (buf))) >= 0) { /* fixme: we are not really converting CRLF to LF but just skipping CR. Doing it correctly needs a more complex logic */ @@ -565,7 +565,7 @@ static char *data_object_to_tempfile (gpgme_data_t data, char *tempf, FILE **ret int err; char tempfb[_POSIX_PATH_MAX]; FILE *fp = NULL; - size_t nread = 0; + ssize_t nread = 0; if (!tempf) { @@ -584,7 +584,7 @@ static char *data_object_to_tempfile (gpgme_data_t data, char *tempf, FILE **ret { char buf[4096]; - while ((nread = gpgme_data_read (data, buf, sizeof (buf)))) + while ((nread = gpgme_data_read (data, buf, sizeof (buf))) >= 0) { if (fwrite (buf, nread, 1, fp) != 1) { diff --git a/menu.c b/menu.c index a120c41d1..2039372b5 100644 --- a/menu.c +++ b/menu.c @@ -885,7 +885,7 @@ void mutt_current_menu_redraw () static int menu_search (MUTTMENU *menu, int op) { - int r, wrap = 0; + int r = 0, wrap = 0; int searchDir; regex_t re; char buf[SHORT_STRING]; @@ -913,7 +913,10 @@ static int menu_search (MUTTMENU *menu, int op) if (op == OP_SEARCH_OPPOSITE) searchDir = -searchDir; - if ((r = REGCOMP (&re, searchBuf, REG_NOSUB | mutt_which_case (searchBuf))) != 0) + if (searchBuf) + r = REGCOMP (&re, searchBuf, REG_NOSUB | mutt_which_case (searchBuf)); + + if (r != 0) { regerror (r, &re, buf, sizeof (buf)); mutt_error ("%s", buf); diff --git a/pager.c b/pager.c index b65345715..2002f99b4 100644 --- a/pager.c +++ b/pager.c @@ -1435,7 +1435,8 @@ display_line (FILE *f, LOFF_T *last_pos, struct line_t **lineInfo, int n, (*last)--; goto out; } - regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0); + if (regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0) != 0) + goto out; (*lineInfo)[n].quote = classify_quote (QuoteList, (char *) fmt + pmatch[0].rm_so, pmatch[0].rm_eo - pmatch[0].rm_so, diff --git a/parse.c b/parse.c index 136ab801c..a1fabbccb 100644 --- a/parse.c +++ b/parse.c @@ -621,7 +621,8 @@ BODY *mutt_parse_multipart (FILE *fp, const char *boundary, LOFF_T end_off, int #ifdef SUN_ATTACHMENT if (mutt_get_parameter ("content-lines", new->parameter)) { - mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines); + if (mutt_atoi (mutt_get_parameter ("content-lines", new->parameter), &lines) < 0) + lines = 0; for ( ; lines; lines-- ) if (ftello (fp) >= end_off || fgets (buffer, LONG_STRING, fp) == NULL) break; diff --git a/pop_lib.c b/pop_lib.c index 87f104896..4368ac21c 100644 --- a/pop_lib.c +++ b/pop_lib.c @@ -397,9 +397,12 @@ void pop_logout (CONTEXT *ctx) if (ret != -1) { strfcpy (buf, "QUIT\r\n", sizeof (buf)); - pop_query (pop_data, buf, sizeof (buf)); + ret = pop_query (pop_data, buf, sizeof (buf)); } + if (ret < 0) + mutt_debug(1, "Error closing POP connection\n"); + mutt_clear_error (); } diff --git a/send.c b/send.c index 777bc9c97..6ef3d8780 100644 --- a/send.c +++ b/send.c @@ -417,7 +417,8 @@ static int include_forward (CONTEXT *ctx, HEADER *cur, FILE *out) if (WithCrypto && (cur->security & ENCRYPT) && option (OPTFORWDECODE)) { /* make sure we have the user's passphrase before proceeding... */ - crypt_valid_passphrase (cur->security); + if (!crypt_valid_passphrase (cur->security)) + return -1; } mutt_forward_intro (out, cur); @@ -475,7 +476,8 @@ static int include_reply (CONTEXT *ctx, HEADER *cur, FILE *out) if (WithCrypto && (cur->security & ENCRYPT)) { /* make sure we have the user's passphrase before proceeding... */ - crypt_valid_passphrase (cur->security); + if (!crypt_valid_passphrase (cur->security)) + return -1; } mutt_parse_mime_message (ctx, cur); @@ -1158,13 +1160,15 @@ static void decode_descriptions (BODY *b) static void fix_end_of_file (const char *data) { FILE *fp = NULL; - int c; if ((fp = safe_fopen (data, "a+")) == NULL) return; - fseek (fp,-1,SEEK_END); - if ((c = fgetc(fp)) != '\n') - fputc ('\n', fp); + if (fseek (fp,-1,SEEK_END) >= 0) + { + int c = fgetc(fp); + if (c != '\n') + fputc ('\n', fp); + } safe_fclose (&fp); } -- 2.40.0