]> diplodocus.org Git - nmh/blob - sbr/folder_realloc.c
Fix invalid pointer arithmetic.
[nmh] / sbr / folder_realloc.c
1 /* folder_realloc.c -- realloc a folder/msgs structure
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10
11 /*
12 * Reallocate some of the space in the folder
13 * structure (currently just message status array).
14 *
15 * Return pointer to new folder structure.
16 * If error, return NULL.
17 */
18
19 struct msgs *
20 folder_realloc (struct msgs *mp, int lo, int hi)
21 {
22 struct bvector *tmpstats, *t;
23 size_t i;
24 int msgnum;
25
26 /* sanity checks */
27 if (lo < 1)
28 adios (NULL, "BUG: called folder_realloc with lo (%d) < 1", lo);
29 if (hi < 1)
30 adios (NULL, "BUG: called folder_realloc with hi (%d) < 1", hi);
31 if (mp->nummsg > 0 && lo > mp->lowmsg)
32 adios (NULL, "BUG: called folder_realloc with lo (%d) > mp->lowmsg (%d)",
33 lo, mp->lowmsg);
34 if (mp->nummsg > 0 && hi < mp->hghmsg)
35 adios (NULL, "BUG: called folder_realloc with hi (%d) < mp->hghmsg (%d)",
36 hi, mp->hghmsg);
37
38 /* Check if we really need to reallocate anything */
39 if (lo == mp->lowoff && hi == mp->hghoff)
40 return mp;
41
42 /* first allocate the new message status space */
43 mp->num_msgstats = MSGSTATNUM (lo, hi);
44 tmpstats = mh_xmalloc (MSGSTATSIZE(mp));
45 for (i = 0, t = tmpstats; i < mp->num_msgstats; ++i, ++t) {
46 bvector_init(t);
47 }
48
49 /* then copy messages status array with shift */
50 if (mp->nummsg > 0) {
51 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
52 bvector_copy (tmpstats + msgnum - lo, msgstat (mp, msgnum));
53 }
54 free(mp->msgstats);
55 mp->msgstats = tmpstats;
56 mp->lowoff = lo;
57 mp->hghoff = hi;
58
59 return mp;
60 }