From d4b8d1486a69ee5bbd96a2758f56c1bbc6242bfb Mon Sep 17 00:00:00 2001 From: harklu Date: Mon, 8 Aug 2011 10:07:17 +0200 Subject: [PATCH] core: allocate OSD text buffers dynamically The OSD text buffers (mp_osd_msg_t.text and osd_state.text) used to be static arrays, with the buffer sizes spread all over the code as magic constants. Make the buffers dynamically allocated and remove the arbitrary length limits. --- command.c | 4 ++-- mplayer.c | 26 ++++++++++++-------------- sub/sub.c | 9 +++++++++ sub/sub.h | 3 ++- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/command.c b/command.c index 07b0513d5d..fc8750ea29 100644 --- a/command.c +++ b/command.c @@ -2981,7 +2981,7 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd) set_osd_msg(OSD_MSG_TEXT, cmd->args[2].v.i, (cmd->args[1].v.i < 0 ? osd_duration : cmd->args[1].v.i), - "%-.63s", cmd->args[0].v.s); + "%s", cmd->args[0].v.s); break; case MP_CMD_OSD_SHOW_PROPERTY_TEXT: { @@ -2993,7 +2993,7 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd) set_osd_msg(OSD_MSG_TEXT, cmd->args[2].v.i, (cmd->args[1].v.i < 0 ? osd_duration : cmd->args[1].v.i), - "%-.63s", txt); + "%s", txt); free(txt); } break; diff --git a/mplayer.c b/mplayer.c index 3b8f588c0e..b77acb3088 100644 --- a/mplayer.c +++ b/mplayer.c @@ -1471,7 +1471,7 @@ struct mp_osd_msg { /// Previous message on the stack. mp_osd_msg_t *prev; /// Message text. - char msg[128]; + char *msg; int id, level, started; /// Display duration in ms. unsigned time; @@ -1491,14 +1491,13 @@ static void set_osd_msg_va(int id, int level, int time, const char *fmt, va_list ap) { mp_osd_msg_t *msg, *last = NULL; - int r; // look if the id is already in the stack for (msg = osd_msg_stack; msg && msg->id != id; last = msg, msg = msg->prev) ; // not found: alloc it if (!msg) { - msg = calloc(1, sizeof(mp_osd_msg_t)); + msg = talloc_zero(NULL, mp_osd_msg_t); msg->prev = osd_msg_stack; osd_msg_stack = msg; } else if (last) { // found, but it's not on top of the stack @@ -1506,10 +1505,9 @@ static void set_osd_msg_va(int id, int level, int time, const char *fmt, msg->prev = osd_msg_stack; osd_msg_stack = msg; } + talloc_free(msg->msg); // write the msg - r = vsnprintf(msg->msg, 128, fmt, ap); - if (r >= 128) - msg->msg[127] = 0; + msg->msg = talloc_vasprintf(msg, fmt, ap); // set id and time msg->id = id; msg->level = level; @@ -1533,7 +1531,6 @@ void set_osd_tmsg(int id, int level, int time, const char *fmt, ...) va_end(ap); } - /** * \brief Remove a message from the OSD stack * @@ -1556,7 +1553,7 @@ void rm_osd_msg(int id) last->prev = msg->prev; else osd_msg_stack = msg->prev; - free(msg); + talloc_free(msg); } /** @@ -1569,7 +1566,7 @@ static void clear_osd_msgs(void) mp_osd_msg_t *msg = osd_msg_stack, *prev = NULL; while (msg) { prev = msg->prev; - free(msg); + talloc_free(msg); msg = prev; } osd_msg_stack = NULL; @@ -1630,7 +1627,7 @@ static mp_osd_msg_t *get_osd_msg(struct MPContext *mpctx) continue; } // kill the message - free(msg); + talloc_free(msg); if (last) { last->prev = prev; msg = last; @@ -1720,7 +1717,7 @@ static void update_osd_msg(struct MPContext *mpctx) // Look if we have a msg if ((msg = get_osd_msg(mpctx))) { if (strcmp(osd->osd_text, msg->msg)) { - strncpy(osd->osd_text, msg->msg, 127); + osd_set_text(osd, msg->msg); if (mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else if (opts->term_osd) @@ -1774,20 +1771,21 @@ static void update_osd_msg(struct MPContext *mpctx) } if (opts->osd_level == 3) - snprintf(osd_text_timer, 63, + snprintf(osd_text_timer, sizeof(osd_text_timer), "%c %02d:%02d:%02d%s / %02d:%02d:%02d%s", mpctx->osd_function, pts / 3600, (pts / 60) % 60, pts % 60, fractions_text, len / 3600, (len / 60) % 60, len % 60, percentage_text); else - snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s%s", + snprintf(osd_text_timer, sizeof(osd_text_timer), + "%c %02d:%02d:%02d%s%s", mpctx->osd_function, pts / 3600, (pts / 60) % 60, pts % 60, fractions_text, percentage_text); } else osd_text_timer[0] = 0; if (strcmp(osd->osd_text, osd_text_timer)) { - strncpy(osd->osd_text, osd_text_timer, 63); + osd_set_text(osd, osd_text_timer); vo_osd_changed(OSDTYPE_OSD); } return; diff --git a/sub/sub.c b/sub/sub.c index 880793373c..efe648948c 100644 --- a/sub/sub.c +++ b/sub/sub.c @@ -1239,9 +1239,18 @@ struct osd_state *osd_create(void) #ifdef CONFIG_FREETYPE force_load_font = 1; #endif + osd_set_text(osd, NULL); return osd; } +void osd_set_text(struct osd_state *osd, const char *text) { + talloc_free(osd->osd_text); + //osd->text must never be NULL + if (!text) + text = ""; + osd->osd_text = talloc_strdup(osd, text); +} + int vo_osd_changed_flag=0; void osd_remove_text(struct osd_state *osd, int dxs, int dys, diff --git a/sub/sub.h b/sub/sub.h index 30a7919aa1..e7ded2556a 100644 --- a/sub/sub.h +++ b/sub/sub.h @@ -72,7 +72,7 @@ struct osd_state { struct ass_library *ass_library; // flag to signal reinitialization due to ass-related option changes bool ass_force_reload; - unsigned char osd_text[128]; + char *osd_text; struct font_desc *sub_font; struct ass_track *ass_track; bool ass_track_changed; @@ -148,6 +148,7 @@ void osd_remove_text(struct osd_state *osd, int dxs, int dys, void (*remove)(int x0, int y0, int w, int h)); struct osd_state *osd_create(void); +void osd_set_text(struct osd_state *osd, const char *text); int osd_update(struct osd_state *osd, int dxs, int dys); int vo_osd_changed(int new_value); int vo_osd_check_range_update(int,int,int,int);