diff --git a/mh.c b/mh.c index 645c6f60105..41580af85b5 100644 --- a/mh.c +++ b/mh.c @@ -1337,44 +1337,46 @@ static int maildir_open_mailbox(struct Context *ctx) static int maildir_open_mailbox_append(struct Context *ctx, int flags) { - if (flags & MUTT_APPENDNEW) + if (!(flags & MUTT_APPENDNEW)) { - if (mkdir(ctx->path, S_IRWXU)) - { - mutt_perror(ctx->path); - return -1; - } + return 0; + } - char tmp[_POSIX_PATH_MAX]; + if (mkdir(ctx->path, S_IRWXU)) + { + mutt_perror(ctx->path); + return -1; + } + + char tmp[_POSIX_PATH_MAX]; + snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path); + if (mkdir(tmp, S_IRWXU)) + { + mutt_perror(tmp); + rmdir(ctx->path); + return -1; + } + + snprintf(tmp, sizeof(tmp), "%s/new", ctx->path); + if (mkdir(tmp, S_IRWXU)) + { + mutt_perror(tmp); snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path); - if (mkdir(tmp, S_IRWXU)) - { - mutt_perror(tmp); - rmdir(ctx->path); - return -1; - } + rmdir(tmp); + rmdir(ctx->path); + return -1; + } + snprintf(tmp, sizeof(tmp), "%s/tmp", ctx->path); + if (mkdir(tmp, S_IRWXU)) + { + mutt_perror(tmp); + snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path); + rmdir(tmp); snprintf(tmp, sizeof(tmp), "%s/new", ctx->path); - if (mkdir(tmp, S_IRWXU)) - { - mutt_perror(tmp); - snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path); - rmdir(tmp); - rmdir(ctx->path); - return -1; - } - - snprintf(tmp, sizeof(tmp), "%s/tmp", ctx->path); - if (mkdir(tmp, S_IRWXU)) - { - mutt_perror(tmp); - snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path); - rmdir(tmp); - snprintf(tmp, sizeof(tmp), "%s/new", ctx->path); - rmdir(tmp); - rmdir(ctx->path); - return -1; - } + rmdir(tmp); + rmdir(ctx->path); + return -1; } return 0; @@ -1387,25 +1389,27 @@ static int mh_open_mailbox(struct Context *ctx) static int mh_open_mailbox_append(struct Context *ctx, int flags) { - if (flags & MUTT_APPENDNEW) + if (!(flags & MUTT_APPENDNEW)) { - if (mkdir(ctx->path, S_IRWXU)) - { - mutt_perror(ctx->path); - return -1; - } + return 0; + } - char tmp[_POSIX_PATH_MAX]; - snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", ctx->path); - const int i = creat(tmp, S_IRWXU); - if (i == -1) - { - mutt_perror(tmp); - rmdir(ctx->path); - return -1; - } - close(i); + if (mkdir(ctx->path, S_IRWXU)) + { + mutt_perror(ctx->path); + return -1; + } + + char tmp[_POSIX_PATH_MAX]; + snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", ctx->path); + const int i = creat(tmp, S_IRWXU); + if (i == -1) + { + mutt_perror(tmp); + rmdir(ctx->path); + return -1; } + close(i); return 0; } diff --git a/mutt/file.c b/mutt/file.c index 9f319f86de8..d894288f664 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -227,35 +227,39 @@ int mutt_file_fsync_close(FILE **f) */ void mutt_file_unlink(const char *s) { - struct stat sb, sb2; + struct stat sb; /* Defend against symlink attacks */ - if ((lstat(s, &sb) == 0) && S_ISREG(sb.st_mode)) + const bool is_regular_file = (lstat(s, &sb) == 0) && S_ISREG(sb.st_mode); + if (!is_regular_file) { - const int fd = open(s, O_RDWR | O_NOFOLLOW); - if (fd < 0) - return; + return; + } - if ((fstat(fd, &sb2) != 0) || !S_ISREG(sb2.st_mode) || - (sb.st_dev != sb2.st_dev) || (sb.st_ino != sb2.st_ino)) - { - close(fd); - return; - } + const int fd = open(s, O_RDWR | O_NOFOLLOW); + if (fd < 0) + return; - FILE *f = fdopen(fd, "r+"); - if (f) + struct stat sb2; + if ((fstat(fd, &sb2) != 0) || !S_ISREG(sb2.st_mode) || + (sb.st_dev != sb2.st_dev) || (sb.st_ino != sb2.st_ino)) + { + close(fd); + return; + } + + FILE *f = fdopen(fd, "r+"); + if (f) + { + unlink(s); + char buf[2048]; + memset(buf, 0, sizeof(buf)); + while (sb.st_size > 0) { - unlink(s); - char buf[2048]; - memset(buf, 0, sizeof(buf)); - while (sb.st_size > 0) - { - fwrite(buf, 1, MIN(sizeof(buf), sb.st_size), f); - sb.st_size -= MIN(sizeof(buf), sb.st_size); - } - mutt_file_fclose(&f); + fwrite(buf, 1, MIN(sizeof(buf), sb.st_size), f); + sb.st_size -= MIN(sizeof(buf), sb.st_size); } + mutt_file_fclose(&f); } } diff --git a/pattern.c b/pattern.c index ca95cb0a4e0..8285f917750 100644 --- a/pattern.c +++ b/pattern.c @@ -891,14 +891,16 @@ static int patmatch(const struct Pattern *pat, const char *buf) static int msg_search(struct Context *ctx, struct Pattern *pat, int msgno) { - struct Message *msg = NULL; - struct State s; + int match = 0; + struct Message *msg = mx_open_message(ctx, msgno); + if (!msg) + { + return match; + } + FILE *fp = NULL; long lng = 0; - int match = 0; struct Header *h = ctx->hdrs[msgno]; - char *buf = NULL; - size_t blen; #ifdef USE_FMEMOPEN char *temp = NULL; size_t tempsize; @@ -907,141 +909,138 @@ static int msg_search(struct Context *ctx, struct Pattern *pat, int msgno) struct stat st; #endif - msg = mx_open_message(ctx, msgno); - if (msg) + if (ThoroughSearch) { - if (ThoroughSearch) - { - /* decode the header / body */ - memset(&s, 0, sizeof(s)); - s.fpin = msg->fp; - s.flags = MUTT_CHARCONV; + /* decode the header / body */ + struct State s; + memset(&s, 0, sizeof(s)); + s.fpin = msg->fp; + s.flags = MUTT_CHARCONV; #ifdef USE_FMEMOPEN - s.fpout = open_memstream(&temp, &tempsize); - if (!s.fpout) - { - mutt_perror(_("Error opening memstream")); - return 0; - } + s.fpout = open_memstream(&temp, &tempsize); + if (!s.fpout) + { + mutt_perror(_("Error opening memstream")); + return 0; + } #else - mutt_mktemp(tempfile, sizeof(tempfile)); - s.fpout = mutt_file_fopen(tempfile, "w+"); - if (!s.fpout) - { - mutt_perror(tempfile); - return 0; - } + mutt_mktemp(tempfile, sizeof(tempfile)); + s.fpout = mutt_file_fopen(tempfile, "w+"); + if (!s.fpout) + { + mutt_perror(tempfile); + return 0; + } #endif - if (pat->op != MUTT_BODY) - mutt_copy_header(msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL); + if (pat->op != MUTT_BODY) + mutt_copy_header(msg->fp, h, s.fpout, CH_FROM | CH_DECODE, NULL); - if (pat->op != MUTT_HEADER) - { - mutt_parse_mime_message(ctx, h); + if (pat->op != MUTT_HEADER) + { + mutt_parse_mime_message(ctx, h); - if (WithCrypto && (h->security & ENCRYPT) && !crypt_valid_passphrase(h->security)) + if (WithCrypto && (h->security & ENCRYPT) && !crypt_valid_passphrase(h->security)) + { + mx_close_message(ctx, &msg); + if (s.fpout) { - mx_close_message(ctx, &msg); - if (s.fpout) - { - mutt_file_fclose(&s.fpout); + mutt_file_fclose(&s.fpout); #ifdef USE_FMEMOPEN - FREE(&temp); + FREE(&temp); #else - unlink(tempfile); + unlink(tempfile); #endif - } - return 0; } - - fseeko(msg->fp, h->offset, SEEK_SET); - mutt_body_handler(h->content, &s); + return 0; } + fseeko(msg->fp, h->offset, SEEK_SET); + mutt_body_handler(h->content, &s); + } + #ifdef USE_FMEMOPEN - fclose(s.fpout); - lng = tempsize; + fclose(s.fpout); + lng = tempsize; - if (tempsize) + if (tempsize) + { + fp = fmemopen(temp, tempsize, "r"); + if (!fp) { - fp = fmemopen(temp, tempsize, "r"); - if (!fp) - { - mutt_perror(_("Error re-opening memstream")); - return 0; - } + mutt_perror(_("Error re-opening memstream")); + return 0; } - else - { /* fmemopen cannot handle empty buffers */ - fp = mutt_file_fopen("/dev/null", "r"); - if (!fp) - { - mutt_perror(_("Error opening /dev/null")); - return 0; - } + } + else + { /* fmemopen cannot handle empty buffers */ + fp = mutt_file_fopen("/dev/null", "r"); + if (!fp) + { + mutt_perror(_("Error opening /dev/null")); + return 0; } + } #else - fp = s.fpout; - fflush(fp); - fseek(fp, 0, SEEK_SET); - fstat(fileno(fp), &st); - lng = (long) st.st_size; + fp = s.fpout; + fflush(fp); + fseek(fp, 0, SEEK_SET); + fstat(fileno(fp), &st); + lng = (long) st.st_size; #endif + } + else + { + /* raw header / body */ + fp = msg->fp; + if (pat->op != MUTT_BODY) + { + fseeko(fp, h->offset, SEEK_SET); + lng = h->content->offset - h->offset; } - else + if (pat->op != MUTT_HEADER) { - /* raw header / body */ - fp = msg->fp; - if (pat->op != MUTT_BODY) - { - fseeko(fp, h->offset, SEEK_SET); - lng = h->content->offset - h->offset; - } - if (pat->op != MUTT_HEADER) - { - if (pat->op == MUTT_BODY) - fseeko(fp, h->content->offset, SEEK_SET); - lng += h->content->length; - } + if (pat->op == MUTT_BODY) + fseeko(fp, h->content->offset, SEEK_SET); + lng += h->content->length; } + } - blen = STRING; - buf = mutt_mem_malloc(blen); + size_t blen = STRING; + char *buf = mutt_mem_malloc(blen); - /* search the file "fp" */ - while (lng > 0) + /* search the file "fp" */ + while (lng > 0) + { + if (pat->op == MUTT_HEADER) { - if (pat->op == MUTT_HEADER) - { - buf = mutt_read_rfc822_line(fp, buf, &blen); - if (*buf == '\0') - break; - } - else if (fgets(buf, blen - 1, fp) == NULL) - break; /* don't loop forever */ - if (patmatch(pat, buf) == 0) - { - match = 1; + buf = mutt_read_rfc822_line(fp, buf, &blen); + if (*buf == '\0') break; - } - lng -= mutt_str_strlen(buf); } + else if (fgets(buf, blen - 1, fp) == NULL) + break; /* don't loop forever */ + if (patmatch(pat, buf) == 0) + { + match = 1; + break; + } + lng -= mutt_str_strlen(buf); + } - FREE(&buf); + FREE(&buf); - mx_close_message(ctx, &msg); + mx_close_message(ctx, &msg); - if (ThoroughSearch) - { - mutt_file_fclose(&fp); + if (ThoroughSearch) + { + mutt_file_fclose(&fp); #ifdef USE_FMEMOPEN - if (tempsize) - FREE(&temp); + if (tempsize) + FREE(&temp); #else - unlink(tempfile); + unlink(tempfile); #endif - } } return match; diff --git a/sidebar.c b/sidebar.c index 0c0e8e3da58..39af41502a6 100644 --- a/sidebar.c +++ b/sidebar.c @@ -605,15 +605,11 @@ static int select_page_up(void) */ static bool prepare_sidebar(int page_size) { - struct SbEntry *opn_entry = NULL, *hil_entry = NULL; - if (!EntryCount || (page_size <= 0)) return false; - if (OpnIndex >= 0) - opn_entry = Entries[OpnIndex]; - if (HilIndex >= 0) - hil_entry = Entries[HilIndex]; + const struct SbEntry *opn_entry = (OpnIndex >= 0) ? Entries[OpnIndex] : NULL; + const struct SbEntry *hil_entry = (HilIndex >= 0) ? Entries[HilIndex] : NULL; update_entries_visibility(); sort_entries();