From 414f1809831a171cb4846a31e58f167846a8efdf Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Sun, 15 Jan 2017 10:00:55 -0800 Subject: [PATCH] Improve error handling in mbox magic detection. Thanks to Simon Ruderich for pointing out several small issues with the previous commit. --- mx.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/mx.c b/mx.c index fc877360f..3a3feaccd 100644 --- a/mx.c +++ b/mx.c @@ -483,20 +483,27 @@ int mx_get_magic (const char *path) else if ((f = fopen (path, "r")) != NULL) { struct utimbuf times; - int ch = 0; + int ch; /* Some mailbox creation tools erroneously append a blank line to * a file before appending a mail message. This allows mutt to * detect magic for and thus open those files. */ - while ((ch = fgetc(f)) && (ch == '\n' || ch == '\r')); - if (!feof(f) && ch) - ungetc(ch, f); - - fgets (tmp, sizeof (tmp), f); - if (mutt_strncmp ("From ", tmp, 5) == 0) - magic = MUTT_MBOX; - else if (mutt_strcmp (MMDF_SEP, tmp) == 0) - magic = MUTT_MMDF; + while ((ch = fgetc (f)) != EOF) + { + if (ch != '\n' && ch != '\r') + { + ungetc (ch, f); + break; + } + } + + if (fgets (tmp, sizeof (tmp), f)) + { + if (mutt_strncmp ("From ", tmp, 5) == 0) + magic = MUTT_MBOX; + else if (mutt_strcmp (MMDF_SEP, tmp) == 0) + magic = MUTT_MMDF; + } safe_fclose (&f); if (!option(OPTCHECKMBOXSIZE)) -- 2.40.0