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.
This commit is contained in:
Themaister 2013-10-07 21:54:29 +02:00
parent d8ee1bb133
commit 84c84814cf
16 changed files with 49 additions and 38 deletions

View File

@ -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);
}

View File

@ -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(

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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(

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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};

View File

@ -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)

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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++;

View File

@ -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;