From 84c84814cf55e1bfb0e69b2471b8191865b61a2d Mon Sep 17 00:00:00 2001 From: Themaister Date: Mon, 7 Oct 2013 21:54:29 +0200 Subject: [PATCH] Fix possible crash with uninitialized buf_fps. If fps count == 0, buf_fps would not be written to. Also, make writing into buf_fps optional. Drop always_write variable as it doesn't serve a purpose anymore. --- gfx/context/androidegl_ctx.c | 5 +++-- gfx/context/bbqnx_ctx.c | 2 +- gfx/context/drm_egl_ctx.c | 5 +++-- gfx/context/emscriptenegl_ctx.c | 5 +++-- gfx/context/glx_ctx.c | 5 +++-- gfx/context/null_ctx.c | 2 +- gfx/context/ps3_ctx.c | 6 +++--- gfx/context/vc_egl_ctx.c | 7 ++++--- gfx/context/wgl_ctx.c | 5 +++-- gfx/context/xdk_ctx.c | 5 +++-- gfx/context/xegl_ctx.c | 6 +++++- gfx/gfx_common.c | 13 ++++++------- gfx/gfx_common.h | 6 +++--- gfx/sdl_gfx.c | 4 ++-- gfx/xvideo.c | 6 +++--- gx/gx_video.c | 5 +++-- 16 files changed, 49 insertions(+), 38 deletions(-) diff --git a/gfx/context/androidegl_ctx.c b/gfx/context/androidegl_ctx.c index 39dc605a36..198dd09f85 100644 --- a/gfx/context/androidegl_ctx.c +++ b/gfx/context/androidegl_ctx.c @@ -197,9 +197,10 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps)); - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/bbqnx_ctx.c b/gfx/context/bbqnx_ctx.c index 3dc2294cd1..fd60a8eda4 100644 --- a/gfx/context/bbqnx_ctx.c +++ b/gfx/context/bbqnx_ctx.c @@ -323,7 +323,7 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); + gfx_get_fps(buf, sizeof(buf), buf_fps, sizeof(buf_fps)); } static bool gfx_ctx_set_video_mode( diff --git a/gfx/context/drm_egl_ctx.c b/gfx/context/drm_egl_ctx.c index 0aa52299cb..9abec0f7df 100644 --- a/gfx/context/drm_egl_ctx.c +++ b/gfx/context/drm_egl_ctx.c @@ -216,9 +216,10 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps)); - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/emscriptenegl_ctx.c b/gfx/context/emscriptenegl_ctx.c index b7ad20d821..ec650bf64a 100644 --- a/gfx/context/emscriptenegl_ctx.c +++ b/gfx/context/emscriptenegl_ctx.c @@ -83,9 +83,10 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps)); - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/glx_ctx.c b/gfx/context/glx_ctx.c index e1228a113b..488129a64e 100644 --- a/gfx/context/glx_ctx.c +++ b/gfx/context/glx_ctx.c @@ -166,10 +166,11 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - if (gfx_get_fps(buf, sizeof(buf), true, buf_fps, sizeof(buf_fps))) + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + if (gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps))) XStoreName(g_dpy, g_win, buf); - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/null_ctx.c b/gfx/context/null_ctx.c index 4ec4ec7a1e..346fddd67c 100644 --- a/gfx/context/null_ctx.c +++ b/gfx/context/null_ctx.c @@ -64,7 +64,7 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); + gfx_get_fps(buf, sizeof(buf), buf_fps, sizeof(buf_fps)); } static bool gfx_ctx_set_video_mode( diff --git a/gfx/context/ps3_ctx.c b/gfx/context/ps3_ctx.c index 9b330002df..43df50e836 100644 --- a/gfx/context/ps3_ctx.c +++ b/gfx/context/ps3_ctx.c @@ -189,10 +189,10 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) { } static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps)); - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); - - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/vc_egl_ctx.c b/gfx/context/vc_egl_ctx.c index e5d0affc8a..dfdef5a7f7 100644 --- a/gfx/context/vc_egl_ctx.c +++ b/gfx/context/vc_egl_ctx.c @@ -109,10 +109,11 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { - char buf[128], buf_fps[128] - gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)); + char buf[128], buf_fps[128]; + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps)); - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/wgl_ctx.c b/gfx/context/wgl_ctx.c index d3b09aaa72..49360994ee 100644 --- a/gfx/context/wgl_ctx.c +++ b/gfx/context/wgl_ctx.c @@ -278,10 +278,11 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - if (gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps))) + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + if (gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps))) SetWindowText(g_hwnd, buf); - if ((g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW))) + if (fps_draw) msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } diff --git a/gfx/context/xdk_ctx.c b/gfx/context/xdk_ctx.c index 496fbb2eeb..0fbdbc052d 100644 --- a/gfx/context/xdk_ctx.c +++ b/gfx/context/xdk_ctx.c @@ -84,10 +84,11 @@ extern unsigned font_x, font_y; static void gfx_ctx_xdk_update_window_title(void) { char buf[128], buf_fps[128]; + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); xdk_d3d_video_t *d3d = (xdk_d3d_video_t*)driver.video_data; - if (gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps)) && - g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW)) + if (gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps)) && + fps_draw) { MEMORYSTATUS stat; font_params_t font_parms = {0}; diff --git a/gfx/context/xegl_ctx.c b/gfx/context/xegl_ctx.c index 0afcdee90a..0dda2fef17 100644 --- a/gfx/context/xegl_ctx.c +++ b/gfx/context/xegl_ctx.c @@ -180,8 +180,12 @@ static void gfx_ctx_set_resize(unsigned width, unsigned height) static void gfx_ctx_update_window_title(void) { char buf[128], buf_fps[128]; - if (gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps))) + bool fps_draw = g_extern.lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + if (gfx_get_fps(buf, sizeof(buf), fps_draw ? buf_fps : NULL, sizeof(buf_fps))) XStoreName(g_dpy, g_win, buf); + + if (fps_draw) + msg_queue_push(g_extern.msg_queue, buf_fps, 1, 1); } static void gfx_ctx_get_video_size(unsigned *width, unsigned *height) diff --git a/gfx/gfx_common.c b/gfx/gfx_common.c index d5c031db01..7a8c42f3c6 100644 --- a/gfx/gfx_common.c +++ b/gfx/gfx_common.c @@ -24,7 +24,7 @@ static inline float time_to_fps(rarch_time_t last_time, rarch_time_t new_time, i } #define FPS_UPDATE_INTERVAL 256 -bool gfx_get_fps(char *buf, size_t size, bool always_write, char *buf_fps, size_t size_fps) +bool gfx_get_fps(char *buf, size_t size, char *buf_fps, size_t size_fps) { static rarch_time_t time; static rarch_time_t fps_time; @@ -45,20 +45,19 @@ bool gfx_get_fps(char *buf, size_t size, bool always_write, char *buf_fps, size_ last_fps = time_to_fps(time, new_time, FPS_UPDATE_INTERVAL); time = new_time; - snprintf(buf_fps, size_fps, "FPS: %6.1f || Frames: %d", last_fps, g_extern.frame_count); snprintf(buf, size, "%s || FPS: %6.1f || Frames: %d", g_extern.title_buf, last_fps, g_extern.frame_count); ret = true; } - else if (always_write) - { + + if (buf_fps) snprintf(buf_fps, size_fps, "FPS: %6.1f || Frames: %d", last_fps, g_extern.frame_count); - snprintf(buf, size, "%s || FPS: %6.1f || Frames: %d", g_extern.title_buf, last_fps, g_extern.frame_count); - } } else { time = fps_time = new_time; - snprintf(buf, size, "%s", g_extern.title_buf); + strlcpy(buf, g_extern.title_buf, size); + if (buf_fps) + strlcpy(buf_fps, "N/A", size_fps); ret = true; } diff --git a/gfx/gfx_common.h b/gfx/gfx_common.h index 64fbf97260..01ea03d5ca 100644 --- a/gfx/gfx_common.h +++ b/gfx/gfx_common.h @@ -28,9 +28,9 @@ extern "C" { #include "../config.h" #endif -// If always_write is true, will always update FPS value -// If always_write is false, returns true if FPS value was updated. -bool gfx_get_fps(char *buf, size_t size, bool always_write, char *buf_fps, size_t size_fps); +// bufs gets a string suitable for Window title, buf_fps for raw FPS only. +// buf_fps is optional. +bool gfx_get_fps(char *buf, size_t size, char *buf_fps, size_t size_fps); #ifdef _WIN32 void gfx_set_dwm(void); diff --git a/gfx/sdl_gfx.c b/gfx/sdl_gfx.c index 7fc53682df..a6a2100e55 100644 --- a/gfx/sdl_gfx.c +++ b/gfx/sdl_gfx.c @@ -311,8 +311,8 @@ static bool sdl_gfx_frame(void *data, const void *frame, unsigned width, unsigne if (SDL_MUSTLOCK(vid->screen)) SDL_UnlockSurface(vid->screen); - char buf[128], buf_fps[128]; - if (gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps))) + char buf[128]; + if (gfx_get_fps(buf, sizeof(buf), NULL, 0)) SDL_WM_SetCaption(buf, NULL); SDL_Flip(vid->screen); diff --git a/gfx/xvideo.c b/gfx/xvideo.c index 899b41f778..ff90a04cd8 100644 --- a/gfx/xvideo.c +++ b/gfx/xvideo.c @@ -409,7 +409,7 @@ static void *xv_init(const video_info_t *video, const input_driver_t **input, vo XMapWindow(xv->display, xv->window); - if (gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps))) + if (gfx_get_fps(buf, sizeof(buf), NULL, 0)) XStoreName(xv->display, xv->window, buf); x11_set_window_attr(xv->display, xv->window); @@ -700,8 +700,8 @@ static bool xv_frame(void *data, const void *frame, unsigned width, unsigned hei true); XSync(xv->display, False); - char buf[128], buf_fps[128]; - if (gfx_get_fps(buf, sizeof(buf), false, buf_fps, sizeof(buf_fps))) + char buf[128]; + if (gfx_get_fps(buf, sizeof(buf), NULL, 0)) XStoreName(xv->display, xv->window, buf); g_extern.frame_count++; diff --git a/gx/gx_video.c b/gx/gx_video.c index d56b3a91e0..7d2e4d9a48 100644 --- a/gx/gx_video.c +++ b/gx/gx_video.c @@ -944,9 +944,10 @@ static bool gx_frame(void *data, const void *frame, } char fps_txt[128], fps_text_buf[128]; - gfx_get_fps(fps_txt, sizeof(fps_txt), lifecycle_mode_state & (1ULL << MODE_FPS_DRAW) ? true : false, fps_text_buf, sizeof(fps_text_buf)); + bool fps_draw = lifecycle_mode_state & (1ULL << MODE_FPS_DRAW); + gfx_get_fps(fps_txt, sizeof(fps_txt), fps_draw ? fps_text_buf : NULL, sizeof(fps_text_buf)); - if (lifecycle_mode_state & (1ULL << MODE_FPS_DRAW)) + if (fps_draw) { char mem1_txt[128]; unsigned x = 15;