From db53878cb13cdfc1d9c032d83b7a8b4c54f65419 Mon Sep 17 00:00:00 2001 From: Dan Cross Date: Mon, 15 Oct 2018 18:36:38 +0000 Subject: [PATCH] Replace realloc() et al in unmangle_ansi with stralloc. I think this is correct. The code, both before and after, doesn't appear to NUL-terminate its output. Signed-off-by: Dan Cross --- src/mail_menu.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/mail_menu.c b/src/mail_menu.c index 9e623a4..486315e 100644 --- a/src/mail_menu.c +++ b/src/mail_menu.c @@ -820,9 +820,7 @@ void unmangle_ansi(char *body, int len, char **body_out, int *body_len) { int params[16]; int param_count = 0; int bold = 0; - char *out; - int out_len; - int out_max; + stralloc out = EMPTY_STRALLOC; char buffer[1024]; int buf_at; int i, j, k; @@ -1290,10 +1288,6 @@ void unmangle_ansi(char *body, int len, char **body_out, int *body_len) { fg = 0x07; bg = 0x00; - out_max = 256; - out_len = 0; - out = (char *)malloz(256); - for (i = 0; i < line_count; i++) { buf_at = 0; for (j = 0; j < 79; j++) { @@ -1402,14 +1396,7 @@ void unmangle_ansi(char *body, int len, char **body_out, int *body_len) { } buffer[buf_at++] = '\r'; - - while (buf_at + out_len > out_max) { - out_max += 256; - out = (char *)realloc(out, out_max); - } - - memcpy(&out[out_len], buffer, buf_at); - out_len += buf_at; + stralloc_catb(&out, buffer, buf_at); } for (i = 0; i < line_count; i++) { @@ -1420,12 +1407,12 @@ void unmangle_ansi(char *body, int len, char **body_out, int *body_len) { } free(fake_screen); - while (out[out_len - 2] == '\r') { - out_len--; + while (out.s[out.len - 2] == '\r') { + out.len--; } - *body_out = out; - *body_len = out_len; + *body_out = out.s; + *body_len = out.len; } int read_message(struct user_record *user, struct msg_headers *msghs, int mailno, int newscan) {