Commit 77b116b1 77b116b12786c1f9520e87868528494db61ce8af by Sergey Poznyakoff

Fix memory leak in mu_rfc2047_decode.

* mailbox/rfc2047.c (getword): Change signature; return error
code.
(mu_rfc2047_decode): Reflect the above change.
1 parent 94adc8f4
...@@ -42,23 +42,26 @@ realloc_buffer (char **bufp, size_t *bufsizep, size_t incr) ...@@ -42,23 +42,26 @@ realloc_buffer (char **bufp, size_t *bufsizep, size_t incr)
42 return 0; 42 return 0;
43 } 43 }
44 44
45 static char * 45 int
46 getword (const char **pstr, int delim) 46 getword (char **pret, const char **pstr, int delim)
47 { 47 {
48 size_t len; 48 size_t len;
49 char *ret; 49 char *ret;
50 const char *start = *pstr; 50 const char *start = *pstr;
51 const char *end = strchr (start, delim); 51 const char *end = strchr (start, delim);
52
53 free (*pret);
52 if (!end) 54 if (!end)
53 return NULL; 55 return MU_ERR_BAD_2047_INPUT;
54 len = end - start; 56 len = end - start;
55 ret = malloc (len + 1); 57 ret = malloc (len + 1);
56 if (!ret) 58 if (!ret)
57 return NULL; 59 return ENOMEM;
58 memcpy (ret, start, len); 60 memcpy (ret, start, len);
59 ret[len] = 0; 61 ret[len] = 0;
60 *pstr = end + 1; 62 *pstr = end + 1;
61 return ret; 63 *pret = ret;
64 return 0;
62 } 65 }
63 66
64 int 67 int
...@@ -119,23 +122,21 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr) ...@@ -119,23 +122,21 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
119 const char *sp = fromstr + 2; 122 const char *sp = fromstr + 2;
120 char tmp[128]; 123 char tmp[128];
121 124
122 fromcode = getword (&sp, '?'); 125 status = getword (&fromcode, &sp, '?');
123 encoding_type = getword (&sp, '?'); 126 if (status)
124 encoded_text = getword (&sp, '?'); 127 break;
128 status = getword (&encoding_type, &sp, '?');
129 if (status)
130 break;
131 status = getword (&encoded_text, &sp, '?');
132 if (status)
133 break;
125 if (sp == NULL || sp[0] != '=') 134 if (sp == NULL || sp[0] != '=')
126 { 135 {
127 status = MU_ERR_BAD_2047_INPUT; 136 status = MU_ERR_BAD_2047_INPUT;
128 break; 137 break;
129 } 138 }
130 139
131 if (fromcode == NULL
132 || encoding_type == NULL
133 || encoded_text == NULL)
134 {
135 status = MU_ERR_BAD_2047_INPUT;
136 break;
137 }
138
139 size = strlen (encoded_text); 140 size = strlen (encoded_text);
140 141
141 switch (encoding_type[0]) 142 switch (encoding_type[0])
......