From df57adcc6d6bab2400f139bf96d267ae3a9a835c Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 8 Dec 2013 04:16:49 -0500 Subject: [PATCH] fix memory leaks and safer way to convert strings from wc to mb --- src/chat.c | 16 ++++++++++++---- src/groupchat.c | 18 +++++++++++++----- src/misc_tools.c | 18 +++++++++--------- src/misc_tools.h | 19 ++++++++++--------- src/prompt.c | 13 +++++++++++-- 5 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/chat.c b/src/chat.c index e98cc4d..55dd013 100644 --- a/src/chat.c +++ b/src/chat.c @@ -362,7 +362,11 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } /* RETURN key: Execute command or print line */ else if (key == '\n') { - uint8_t *line = wcs_to_char(ctx->line); + uint8_t line[MAX_STR_SIZE]; + + if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) + memset(&line, 0, sizeof(line)); + wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); @@ -412,8 +416,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } else { reset_buf(ctx->line, &ctx->pos, &ctx->len); } - - free(line); } } @@ -426,7 +428,13 @@ static void chat_onDraw(ToxWindow *self, Tox *m) ChatContext *ctx = self->chatwin; wclear(ctx->linewin); - mvwprintw(ctx->linewin, 1, 0, "%s", wcs_to_char(ctx->line)); + + uint8_t line[MAX_STR_SIZE]; + + if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) + memset(&line, 0, sizeof(line)); + + mvwprintw(ctx->linewin, 1, 0, "%s", line); /* Draw status bar */ StatusBar *statusbar = self->stb; diff --git a/src/groupchat.c b/src/groupchat.c index b3a614c..2aff558 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -313,7 +313,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) int diff = complete_line(ctx->line, &ctx->pos, &ctx->len, groupchats[self->num].peer_names, groupchats[self->num].num_peers, TOX_MAX_NAME_LENGTH); - if (diff != -1 && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1)-1))) { + if (diff != -1) { if (x + diff > x2 - 1) { int ofst = (x + diff - 1) - (x2 - 1); wmove(self->window, y+1, ofst); @@ -356,7 +356,11 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) /* RETURN key: Execute command or print line */ else if (key == '\n') { - uint8_t *line = wcs_to_char(ctx->line); + uint8_t line[MAX_STR_SIZE]; + + if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) + memset(&line, 0, sizeof(line)); + wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); @@ -398,8 +402,6 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) else { reset_buf(ctx->line, &ctx->pos, &ctx->len); } - - free(line); } } @@ -412,7 +414,13 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m) ChatContext *ctx = self->chatwin; wclear(ctx->linewin); - mvwprintw(ctx->linewin, 1, 0, "%s", wcs_to_char(ctx->line)); + + uint8_t line[MAX_STR_SIZE]; + + if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) + memset(&line, 0, sizeof(line)); + + mvwprintw(ctx->linewin, 1, 0, "%s", line); wclear(ctx->sidebar); mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2); diff --git a/src/misc_tools.c b/src/misc_tools.c index 7312c07..84e7339 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -72,8 +72,8 @@ int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n) } /* converts wide character string into a multibyte string. - Same thing as wcs_to_char() but caller must provide its own buffer */ -int wcs_to_char_buf(uint8_t *buf, const wchar_t *string, size_t n) + Same thing as wcs_to_mbs() but caller must provide its own buffer */ +int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n) { size_t len = wcstombs(NULL, string, 0) + 1; @@ -86,8 +86,8 @@ int wcs_to_char_buf(uint8_t *buf, const wchar_t *string, size_t n) return len; } -/* convert wide characters to multibyte string */ -uint8_t *wcs_to_char(wchar_t *string) +/* convert wide characters to multibyte string: string returned must be free'd */ +uint8_t *wcs_to_mbs(wchar_t *string) { uint8_t *ret = NULL; size_t len = wcstombs(NULL, string, 0); @@ -270,12 +270,12 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len) /* looks for the first instance in list that begins with the last entered word in buf according to pos, then fills buf with the complete word. e.g. "Hello jo" would complete the buffer - with "Hello john". It shouldn't matter where pos is within buf. + with "Hello john". list is a pointer to the list of strings being compared, n_items is the number of items in the list, and size is the size of each item in the list. - Returns the difference between the old len and new len of buf */ + Returns the difference between the old len and new len of buf on success, -1 if error */ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const uint8_t *list, int n_items, int size) { if (*pos <= 0 || *len <= 0 || *len > MAX_STR_SIZE) @@ -283,7 +283,7 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const uint8_t *list, i uint8_t ubuf[MAX_STR_SIZE]; /* work with multibyte string copy of buf for simplicity */ - if (wcs_to_char_buf(ubuf, buf, MAX_STR_SIZE) == -1) + if (wcs_to_mbs_buf(ubuf, buf, MAX_STR_SIZE) == -1) return -1; /* isolate substring from space behind pos to pos */ @@ -325,11 +325,11 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const uint8_t *list, i if (char_to_wcs_buf(newbuf, ubuf, MAX_STR_SIZE) == -1) return -1; + wmemcpy(buf, newbuf, MAX_STR_SIZE); + int diff = m_len - s_len; *len += (size_t) diff; *pos += (size_t) diff; - wmemcpy(buf, newbuf, MAX_STR_SIZE); - return diff; } diff --git a/src/misc_tools.h b/src/misc_tools.h index d636287..ad1583c 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -20,13 +20,13 @@ int string_is_empty(char *string); int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n); /* converts wide character string into a multibyte string. - Same thing as wcs_to_char() but caller must provide its own buffer */ -int wcs_to_char_buf(uint8_t *buf, const wchar_t *string, size_t n); + Same thing as wcs_to_mbs() but caller must provide its own buffer */ +int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n); -/* convert wide characters to multibyte string string */ -uint8_t *wcs_to_char(wchar_t *string); +/* convert wide characters to multibyte string: string returned must be free'd */ +uint8_t *wcs_to_mbs(wchar_t *string); -/* convert a wide char to multibyte string string */ +/* convert a wide char to multibyte char */ char *wc_to_char(wchar_t ch); /* Returns true if connection has timed out, false otherwise */ @@ -66,11 +66,12 @@ void kill_buf(wchar_t *buf, size_t *pos, size_t *len); /* nulls buf and sets pos and len to 0 */ void reset_buf(wchar_t *buf, size_t *pos, size_t *len); -/* looks for the first instance in list that begins with the last entered word in buf, - then completes the word. e.g. "Hello jo" would complete the buffer with "Hello john". +/* looks for the first instance in list that begins with the last entered word in buf according to pos, + then fills buf with the complete word. e.g. "Hello jo" would complete the buffer + with "Hello john". list is a pointer to the list of strings being compared, n_items is the number of items - in the list, and size is the size of each item in the list. + in the list, and size is the size of each item in the list. - Returns the difference between the old len and new len of buf */ + Returns the difference between the old len and new len of buf on success, -1 if error */ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const uint8_t *list, int n_items, int size); \ No newline at end of file diff --git a/src/prompt.c b/src/prompt.c index 7669b9c..dafc003 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -159,7 +159,11 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) /* RETURN key: execute command */ else if (key == '\n') { wprintw(self->window, "\n"); - uint8_t *line = wcs_to_char(prt->line); + uint8_t line[MAX_STR_SIZE]; + + if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1) + memset(&line, 0, sizeof(line)); + execute(self->window, self, m, line, GLOBAL_COMMAND_MODE); reset_buf(prt->line, &prt->pos, &prt->len); } @@ -185,7 +189,12 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) int p_ofst = px2 != x2 ? 0 : X_OFST; if (prt->len > 0) { - mvwprintw(self->window, prt->orig_y, X_OFST, wcs_to_char(prt->line)); + uint8_t line[MAX_STR_SIZE]; + + if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1) + memset(&line, 0, sizeof(line)); + + mvwprintw(self->window, prt->orig_y, X_OFST, line); int k = prt->orig_y + ((prt->len + p_ofst) / px2);