fix Debian bug #202667: crash when a message's filename overflows an int
[mmh] / sbr / folder_read.c
1
2 /*
3  * folder_read.c -- initialize folder structure and read folder
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13
14 /* We allocate the `mi' array 1024 elements at a time */
15 #define NUMMSGS  1024
16
17 /*
18  * 1) Create the folder/message structure
19  * 2) Read the directory (folder) and temporarily
20  *    record the numbers of the messages we have seen.
21  * 3) Then allocate the array for message attributes and
22  *    set the initial flags for all messages we've seen.
23  * 4) Read and initialize the sequence information.
24  */
25
26 struct msgs *
27 folder_read (char *name)
28 {
29     int msgnum, prefix_len, len, *mi;
30     struct msgs *mp;
31     struct stat st;
32     struct dirent *dp;
33     DIR *dd;
34
35     name = m_mailpath (name);
36     if (!(dd = opendir (name))) {
37         free (name);
38         return NULL;
39     }
40
41     if (stat (name, &st) == -1) {
42         free (name);
43         return NULL;
44     }
45
46     /* Allocate the main structure for folder information */
47     if (!(mp = (struct msgs *) malloc ((size_t) sizeof(*mp))))
48         adios (NULL, "unable to allocate folder storage");
49
50     clear_folder_flags (mp);
51     mp->foldpath = name;
52     mp->lowmsg = 0;
53     mp->hghmsg = 0;
54     mp->curmsg = 0;
55     mp->lowsel = 0;
56     mp->hghsel = 0;
57     mp->numsel = 0;
58     mp->nummsg = 0;
59
60     if (access (name, W_OK) == -1 || st.st_uid != getuid())
61         set_readonly (mp);
62     prefix_len = strlen(BACKUP_PREFIX);
63
64     /*
65      * Allocate a temporary place to record the
66      * name of the messages in this folder.
67      */
68     len = NUMMSGS;
69     if (!(mi = (int *) malloc ((size_t) (len * sizeof(*mi)))))
70         adios (NULL, "unable to allocate storage");
71
72     while ((dp = readdir (dd))) {
73         if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
74             /*
75              * Check if we need to allocate more
76              * temporary elements for message names.
77              */
78             if (mp->nummsg >= len) {
79                 len += NUMMSGS;
80                 if (!(mi = (int *) realloc (mi,
81                         (size_t) (len * sizeof(*mi))))) {
82                     adios (NULL, "unable to allocate storage");
83                 }
84             }
85
86             /* Check if this is the first message we've seen */
87             if (mp->nummsg == 0) {
88                 mp->lowmsg = msgnum;
89                 mp->hghmsg = msgnum;
90             } else {
91                 /* Check if this is it the highest or lowest we've seen? */
92                 if (msgnum < mp->lowmsg)
93                    mp->lowmsg = msgnum;
94                 if (msgnum > mp->hghmsg)
95                    mp->hghmsg = msgnum;
96             }
97
98             /*
99              * Now increment count, and record message
100              * number in a temporary place for now.
101              */
102             mi[mp->nummsg++] = msgnum;
103
104         } else {
105             switch (dp->d_name[0]) {
106                 case '.': 
107                 case ',': 
108 #ifdef MHE
109                 case '+': 
110 #endif /* MHE */
111                     continue;
112
113                 default: 
114                     /* skip any files beginning with backup prefix */
115                     if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
116                         continue;
117
118                     /* skip the LINK file */
119                     if (!strcmp (dp->d_name, LINK))
120                         continue;
121
122                     /* indicate that there are other files in folder */
123                     set_other_files (mp);
124                     continue;
125             }
126         }
127     }
128
129     closedir (dd);
130     mp->lowoff = max (mp->lowmsg, 1);
131
132     /* Go ahead and allocate space for 100 additional messages. */
133     mp->hghoff = mp->hghmsg + 100;
134
135     /* for testing, allocate minimal necessary space */
136     /* mp->hghoff = max (mp->hghmsg, 1); */
137
138     /*
139      * Allocate space for status of each message.
140      */
141     if (!(mp->msgstats = malloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff))))
142         adios (NULL, "unable to allocate storage for msgstats");
143
144     /*
145      * Clear all the flag bits for all the message
146      * status entries we just allocated.
147      */
148     for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
149         clear_msg_flags (mp, msgnum);
150
151     /*
152      * Scan through the array of messages we've seen and
153      * setup the initial flags for those messages in the
154      * newly allocated mp->msgstats area.
155      */
156     for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
157         set_exists (mp, mi[msgnum]);
158
159     free (mi);          /* We don't need this anymore    */
160
161     /*
162      * Read and initialize the sequence information.
163      */
164     seq_read (mp);
165
166     return mp;
167 }