summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxhe <xw897002528@gmail.com>2020-01-22 21:50:39 +0800
committerxhe <xw897002528@gmail.com>2020-01-22 22:18:06 +0800
commitfa416e2e897614314abcb711069ad19af2a44354 (patch)
tree0335419c16d77f1971802a8a327b1d3712290531
parent29c38d04e39c9b0ccea4d2e99ef197910431dbc4 (diff)
downloadgettext-tiny-fa416e2e897614314abcb711069ad19af2a44354.tar.gz
poparser: seperate malloc for buffers
poparser allocated one single big buffer for all string buffers, and this actually makes debugging difficult. When buffer overflowed, i can not tell which buffer is damaged directly, since then're actually different parts of one single buffer. So, let's alloc space seperately.
-rw-r--r--src/poparser.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/poparser.c b/src/poparser.c
index dd794f4..a9d9435 100644
--- a/src/poparser.c
+++ b/src/poparser.c
@@ -398,12 +398,12 @@ enum po_error poparser_finish(struct po_parser *p) {
len += p->max_strlen[cnt];
memset(msg, 0, sizeof(struct po_message));
- msg->ctxt = (char*)malloc(len);
- msg->id = msg->ctxt + p->max_ctxt_len;
- msg->plural = msg->id + p->max_id_len;
- msg->str[0] = msg->plural + p->max_plural_len;
- for (cnt = 1; cnt < MAX_NPLURALS; cnt++)
- msg->str[cnt] = msg->str[cnt-1] + p->max_strlen[cnt-1];
+ msg->ctxt = (char*)malloc(p->max_ctxt_len);
+ msg->id = (char*)malloc(p->max_id_len);
+ msg->plural = (char*)malloc(p->max_plural_len);
+
+ for (cnt = 0; cnt < MAX_NPLURALS; cnt++)
+ msg->str[cnt] = (char*)malloc(p->max_strlen[cnt]);
p->hdr.nplurals = 2;
p->current_trans_index = 0;
@@ -414,7 +414,14 @@ enum po_error poparser_finish(struct po_parser *p) {
return t;
if ( (t = poparser_clean(p, msg)) != po_success)
return t;
+
if (msg->ctxt) free(msg->ctxt);
+ if (msg->id) free(msg->id);
+ if (msg->plural) free(msg->plural);
+
+ for (cnt = 0; cnt < MAX_NPLURALS; cnt++)
+ if (msg->str[cnt]) free(msg->str[cnt]);
+
if (p->cd) iconv_close(p->cd);
}