Fix memory allocation in libmu_imap
* libproto/imap/tag.c (_mu_imap_tag_incr): Fix size calculation when reallocating tag_buf (_mu_imap_tag_clr): Re-initialize members on error.
Showing
1 changed file
with
10 additions
and
3 deletions
... | @@ -35,11 +35,13 @@ _mu_imap_tag_incr (mu_imap_t imap) | ... | @@ -35,11 +35,13 @@ _mu_imap_tag_incr (mu_imap_t imap) |
35 | if (++i == imap->tag_len) | 35 | if (++i == imap->tag_len) |
36 | { | 36 | { |
37 | char *sp; | 37 | char *sp; |
38 | int *np = realloc (imap->tag_buf, imap->tag_len + 1); | 38 | int *np = realloc (imap->tag_buf, |
39 | (imap->tag_len + 1) * sizeof imap->tag_buf[0]); | ||
39 | if (!np) | 40 | if (!np) |
40 | return ENOMEM; | 41 | return ENOMEM; |
41 | imap->tag_buf = np; | 42 | imap->tag_buf = np; |
42 | sp = realloc (imap->tag_str, imap->tag_len + 2); | 43 | sp = realloc (imap->tag_str, |
44 | (imap->tag_len + 2) * sizeof imap->tag_str[0]); | ||
43 | if (!sp) | 45 | if (!sp) |
44 | return ENOMEM; | 46 | return ENOMEM; |
45 | imap->tag_str = sp; | 47 | imap->tag_str = sp; |
... | @@ -69,11 +71,16 @@ _mu_imap_tag_clr (mu_imap_t imap) | ... | @@ -69,11 +71,16 @@ _mu_imap_tag_clr (mu_imap_t imap) |
69 | imap->tag_len = 2; | 71 | imap->tag_len = 2; |
70 | imap->tag_buf = calloc (imap->tag_len, sizeof (imap->tag_buf[0])); | 72 | imap->tag_buf = calloc (imap->tag_len, sizeof (imap->tag_buf[0])); |
71 | if (!imap->tag_buf) | 73 | if (!imap->tag_buf) |
72 | return ENOMEM; | 74 | { |
75 | imap->tag_len = 0; | ||
76 | return ENOMEM; | ||
77 | } | ||
73 | imap->tag_str = calloc (imap->tag_len + 1, sizeof (imap->tag_str[0])); | 78 | imap->tag_str = calloc (imap->tag_len + 1, sizeof (imap->tag_str[0])); |
74 | if (!imap->tag_str) | 79 | if (!imap->tag_str) |
75 | { | 80 | { |
76 | free (imap->tag_buf); | 81 | free (imap->tag_buf); |
82 | imap->tag_buf = NULL; | ||
83 | imap->tag_len = 0; | ||
77 | return ENOMEM; | 84 | return ENOMEM; |
78 | } | 85 | } |
79 | } | 86 | } | ... | ... |
-
Please register or sign in to post a comment