Fix out-of-bounds error when incorporating email from stdin
[mmh] / sbr / seq_setunseen.c
index 2bb09b9..bd57a2c 100644 (file)
@@ -8,34 +8,38 @@
 */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 /*
 ** We scan through the folder and act upon all messages
 ** that are marked with the SELECT_UNSEEN bit.
 **
-** If seen == 1, delete messages from unseen sequence.
-** If seen == 0, add messages to unseen sequence.
+** Either add messages to or (if doadd is false) delete messages from
+** the unseen sequence(s).
 */
-
 void
-seq_setunseen(struct msgs *mp, int seen)
+seq_setunseen(struct msgs *mp, int doadd)
 {
        int n;
        char **ap, *cp, *dp;
 
+       if (mp->lowmsg == 0) {
+               return;
+       }
+
        /*
        ** Get the list of sequences for Unseen-Sequence
        ** and split them.
        */
        if ((cp = context_find(usequence))) {
-               dp = getcpy(cp);
+               dp = mh_xstrdup(cp);
        } else {
                /* not set in profile, thus use the default */
-               dp = getcpy(seq_unseen);
+               dp = mh_xstrdup(seq_unseen);
        }
        if (!(ap = brkstring(dp, " ", "\n")) || !*ap) {
                /* contains no sequence name, i.e. we're finished */
-               free(dp);
+               mh_free0(&dp);
                return;
        }
 
@@ -44,7 +48,13 @@ seq_setunseen(struct msgs *mp, int seen)
        ** bit set to/from each of these sequences.
        */
        for (; *ap; ap++) {
-               if (seen) {
+               if (doadd) {
+                       for (n = mp->lowmsg; n <= mp->hghmsg; n++) {
+                               if (is_unseen(mp, n)) {
+                                       seq_addmsg(mp, *ap, n, -1, 0);
+                               }
+                       }
+               } else {
                        /* make sure sequence exists first */
                        if (seq_getnum(mp, *ap) != -1) {
                                for (n = mp->lowsel; n <= mp->hghsel; n++) {
@@ -53,14 +63,8 @@ seq_setunseen(struct msgs *mp, int seen)
                                        }
                                }
                        }
-               } else {
-                       for (n = mp->lowmsg; n <= mp->hghmsg; n++) {
-                               if (is_unseen(mp, n)) {
-                                       seq_addmsg(mp, *ap, n, -1, 0);
-                               }
-                       }
                }
        }
 
-       free(dp);
+       mh_free0(&dp);
 }