(platform_unix.c) Use safer 'fill_pathname_join_special' for assembling

file path names instead of snprintf
(ctr_gfx.c) Use strlcpy instead of snprintf where possible
(ctr_gfx.c) Use snprintf instead of sprintf where possible
(ctr_gfx.c) Some general style nits
This commit is contained in:
LibretroAdmin 2022-08-26 10:43:42 +02:00
parent b8b3fe55fe
commit 29caa95f7e
2 changed files with 99 additions and 91 deletions

View File

@ -689,7 +689,8 @@ static bool make_proc_acpi_key_val(char **_ptr, char **_key, char **_val)
static void check_proc_acpi_battery(const char * node, bool * have_battery,
bool * charging, int *seconds, int *percent)
{
char path[1024];
char basenode[512];
char path[PATH_MAX_LENGTH];
const char *base = proc_acpi_battery_path;
int64_t length = 0;
char *ptr = NULL;
@ -704,9 +705,8 @@ static void check_proc_acpi_battery(const char * node, bool * have_battery,
int secs = -1;
int pct = -1;
path[0] = '\0';
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state");
fill_pathname_join_special(basenode, base, node, sizeof(basenode));
fill_pathname_join_special(path, basenode, "state", sizeof(path));
if (!filestream_exists(path))
goto end;
@ -714,7 +714,7 @@ static void check_proc_acpi_battery(const char * node, bool * have_battery,
if (!filestream_read_file(path, (void**)&buf, &length))
goto end;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "info");
fill_pathname_join_special(path, basenode, "info", sizeof(path));
if (!filestream_read_file(path, (void**)&buf_info, &length))
goto end;
@ -798,7 +798,8 @@ static void check_proc_acpi_sysfs_battery(const char *node,
bool *have_battery, bool *charging, int *seconds,
int *percent, int *valid_pct_idx)
{
char path[1024];
char basenode[512];
char path[PATH_MAX_LENGTH];
const char *base = proc_acpi_sysfs_battery_path;
char *buf = NULL;
char *ptr = NULL;
@ -813,10 +814,9 @@ static void check_proc_acpi_sysfs_battery(const char *node,
int secs = -1;
int pct = -1;
path[0] = '\0';
/* Stat type. Avoid unknown or device supplies. Missing is considered System. */
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "scope");
fill_pathname_join_special(basenode, base, node, sizeof(basenode));
fill_pathname_join_special(path, basenode, "scope", sizeof(path));
if (filestream_exists(path) != 0)
{
@ -831,7 +831,7 @@ static void check_proc_acpi_sysfs_battery(const char *node,
}
}
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "status");
fill_pathname_join_special(path, basenode, "status", sizeof(path));
if (!filestream_exists(path))
return;
@ -854,7 +854,7 @@ static void check_proc_acpi_sysfs_battery(const char *node,
buf = NULL;
}
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "capacity");
fill_pathname_join_special(path, basenode, "capacity", sizeof(path));
if (filestream_read_file(path, (void**)&buf, &length) != 1)
goto end;
@ -874,7 +874,8 @@ end:
static void check_proc_acpi_ac_adapter(const char * node, bool *have_ac)
{
char path[1024];
char basenode[512];
char path[PATH_MAX_LENGTH];
const char *base = proc_acpi_ac_adapter_path;
char *buf = NULL;
char *ptr = NULL;
@ -882,9 +883,8 @@ static void check_proc_acpi_ac_adapter(const char * node, bool *have_ac)
char *val = NULL;
int64_t length = 0;
path[0] = '\0';
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state");
fill_pathname_join_special(basenode, base, node, sizeof(basenode));
fill_pathname_join_special(path, basenode, "state", sizeof(path));
if (!filestream_exists(path))
return;

View File

@ -21,6 +21,7 @@
#include <retro_inline.h>
#include <retro_math.h>
#include <compat/strl.h>
#include <formats/image.h>
#ifdef HAVE_CONFIG_H
@ -65,7 +66,7 @@ extern uint64_t lifecycle_state;
* externally, otherwise cannot detect current state
* when reinitialising... */
bool ctr_bottom_screen_enabled = true;
int fadeCount = 256;
static int fadeCount = 256;
static void ctr_set_bottom_screen_enable(bool enabled, bool idle);
@ -265,6 +266,7 @@ static const char *ctr_texture_path(unsigned id)
return "bottom_menu.png";
case CTR_TEXTURE_STATE_THUMBNAIL:
{
size_t _len;
static char texture_path[PATH_MAX_LENGTH];
char state_path[PATH_MAX_LENGTH];
@ -272,8 +274,13 @@ static const char *ctr_texture_path(unsigned id)
sizeof(state_path)))
return NULL;
snprintf(texture_path, sizeof(texture_path),
"%s.png", state_path);
_len = strlcpy(texture_path,
state_path, sizeof(texture_path));
texture_path[_len ] = '.';
texture_path[_len+1] = 'p';
texture_path[_len+2] = 'n';
texture_path[_len+3] = 'g';
texture_path[_len+4] = '\0';
return path_basename_nocompression(texture_path);
}
@ -289,7 +296,7 @@ static void ctr_update_state_date(void *data)
ctr_video_t *ctr = (ctr_video_t*)data;
time_t now = time(NULL);
struct tm *t = localtime(&now);
sprintf(ctr->state_date, "%02d/%02d/%d",
snprintf(ctr->state_date, sizeof(ctr->state_date), "%02d/%02d/%d",
t->tm_mon + 1, t->tm_mday, t->tm_year + 1900);
}
@ -325,14 +332,14 @@ static bool ctr_update_state_date_from_file(void *data)
ft = mtime;
t = localtime(&ft);
#endif
sprintf(ctr->state_date, "%02d/%02d/%d",
snprintf(ctr->state_date, sizeof(ctr->state_date), "%02d/%02d/%d",
t->tm_mon + 1, t->tm_mday, t->tm_year + 1900);
return true;
error:
ctr->state_data_exist = false;
snprintf(ctr->state_date, sizeof(ctr->state_date), "00/00/0000");
strlcpy(ctr->state_data, "00/00/0000", sizeof(ctr->state_date));
return false;
}
@ -535,7 +542,6 @@ static void bottom_menu_control(void* data, bool lcd_bottom)
state_tmp_touch.py > 99 &&
state_tmp_touch.py < 230)
{
char screenshot_full_path[PATH_MAX_LENGTH];
struct ctr_bottom_texture_data *o =
&ctr->bottom_textures[CTR_TEXTURE_STATE_THUMBNAIL];
@ -576,9 +582,11 @@ static void bottom_menu_control(void* data, bool lcd_bottom)
if (settings->bools.savestate_thumbnail_enable)
{
sprintf(screenshot_full_path, "%s/%s",
char screenshot_full_path[PATH_MAX_LENGTH];
fill_pathname_join_special(screenshot_full_path,
dir_get_ptr(RARCH_DIR_SAVESTATE),
ctr_texture_path(CTR_TEXTURE_STATE_THUMBNAIL));
ctr_texture_path(CTR_TEXTURE_STATE_THUMBNAIL),
sizeof(screenshot_full_path));
take_screenshot(NULL, screenshot_full_path, true,
video_driver_cached_frame_has_valid_framebuffer(),
@ -1073,7 +1081,8 @@ static void* ctr_init(const video_info_t* video,
ctr->drawbuffers.bottom = vramAlloc(CTR_BOTTOM_FRAMEBUFFER_WIDTH * CTR_BOTTOM_FRAMEBUFFER_HEIGHT * 2 * sizeof(uint32_t));
ctr->display_list_size = 0x4000;
ctr->display_list = linearAlloc(ctr->display_list_size * sizeof(uint32_t));
ctr->display_list = linearAlloc(
ctr->display_list_size * sizeof(uint32_t));
GPU_Reset(NULL, ctr->display_list, ctr->display_list_size);
ctr->vertex_cache.size = 0x1000;
@ -1096,8 +1105,7 @@ static void* ctr_init(const video_info_t* video,
ctr->idle_timestamp = 0;
ctr->state_slot = settings->ints.state_slot;
snprintf(ctr->state_date, sizeof(ctr->state_date), "%s", "00/00/0000");
ctr->state_date[CTR_STATE_DATE_SIZE - 1] = '\0';
strlcpy(ctr->state_date, "00/00/0000", sizeof(ctr->state_date));
ctr->rgb32 = video->rgb32;
ctr->texture_width = video->input_scale * RARCH_SCALE_BASE;