From 7bbdd6d18adc612f8f440efb547e9a1e0c406cfe Mon Sep 17 00:00:00 2001 From: twinaphex Date: Tue, 18 May 2021 15:14:08 +0200 Subject: [PATCH] Prefer using snprintf instead of insecure sprintf --- gfx/video_layout/scope.c | 4 ++-- libretro-common/formats/libchdr/libchdr_chd.c | 8 +++++++- menu/cbs/menu_cbs_sublabel.c | 2 +- menu/menu_displaylist.c | 2 +- misc/cpufreq/cpufreq.c | 10 +++++----- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/gfx/video_layout/scope.c b/gfx/video_layout/scope.c index 05a16b2651..a37fa5f88b 100644 --- a/gfx/video_layout/scope.c +++ b/gfx/video_layout/scope.c @@ -129,7 +129,7 @@ void scope_repeat(scope_t *scope) gen->value.val_dec = (float)((int)gen->value.val_dec << gen->shift); else if (gen->shift < 0) gen->value.val_dec = (float)((int)gen->value.val_dec >> -gen->shift); - sprintf(tmp, "%f", gen->value.val_dec); + snprintf(tmp, sizeof(tmp), "%f", gen->value.val_dec); } else { @@ -138,7 +138,7 @@ void scope_repeat(scope_t *scope) gen->value.val_int <<= gen->shift; else if (gen->shift < 0) gen->value.val_int >>= -gen->shift; - sprintf(tmp, "%d", gen->value.val_int); + snprintf(tmp, sizeof(tmp), "%d", gen->value.val_int); } string_set(¶m->value, tmp); diff --git a/libretro-common/formats/libchdr/libchdr_chd.c b/libretro-common/formats/libchdr/libchdr_chd.c index 8574c20db5..603163c6ce 100644 --- a/libretro-common/formats/libchdr/libchdr_chd.c +++ b/libretro-common/formats/libchdr/libchdr_chd.c @@ -1235,7 +1235,13 @@ chd_error chd_get_metadata(chd_file *chd, UINT32 searchtag, UINT32 searchindex, UINT32 faux_length; /* fill in the faux metadata */ - sprintf(faux_metadata, HARD_DISK_METADATA_FORMAT, chd->header.obsolete_cylinders, chd->header.obsolete_heads, chd->header.obsolete_sectors, chd->header.hunkbytes / chd->header.obsolete_hunksize); + snprintf(faux_metadata, + sizeof(faux_metadata), + HARD_DISK_METADATA_FORMAT, + chd->header.obsolete_cylinders, + chd->header.obsolete_heads, + chd->header.obsolete_sectors, + chd->header.hunkbytes / chd->header.obsolete_hunksize); faux_length = (UINT32)strlen(faux_metadata) + 1; /* copy the metadata itself */ diff --git a/menu/cbs/menu_cbs_sublabel.c b/menu/cbs/menu_cbs_sublabel.c index 53de296c1f..107df632b2 100644 --- a/menu/cbs/menu_cbs_sublabel.c +++ b/menu/cbs/menu_cbs_sublabel.c @@ -1025,7 +1025,7 @@ static int action_bind_sublabel_cpu_policy_entry_list( int idx = atoi(path); if (drivers) { - sprintf(s, "%s | Freq: %u MHz\n", drivers[idx]->scaling_governor, + snprintf(s, len, "%s | Freq: %u MHz\n", drivers[idx]->scaling_governor, drivers[idx]->current_frequency / 1000); return 0; } diff --git a/menu/menu_displaylist.c b/menu/menu_displaylist.c index 4e2b19a92a..e845ac49ba 100644 --- a/menu/menu_displaylist.c +++ b/menu/menu_displaylist.c @@ -10047,7 +10047,7 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type, while (*drivers) { char policyid[16]; - sprintf(policyid, "%u", count++); + snprintf(policyid, sizeof(policyid), "%u", count++); menu_entries_append_enum(info->list, policyid, policyid, diff --git a/misc/cpufreq/cpufreq.c b/misc/cpufreq/cpufreq.c index 84e8280b9f..ee20c1c18e 100644 --- a/misc/cpufreq/cpufreq.c +++ b/misc/cpufreq/cpufreq.c @@ -197,9 +197,9 @@ bool set_cpu_scaling_min_frequency( { char fpath[PATH_MAX_LENGTH]; char value[16]; - sprintf(fpath, CPU_POLICIES_DIR "policy%u/scaling_min_freq", + snprintf(fpath, sizeof(fpath), CPU_POLICIES_DIR "policy%u/scaling_min_freq", driver->policy_id); - sprintf(value, "%" PRIu32 "\n", min_freq); + snprintf(value, sizeof(value), "%" PRIu32 "\n", min_freq); if (filestream_write_file(fpath, value, strlen(value))) { driver->min_policy_freq = min_freq; @@ -215,9 +215,9 @@ bool set_cpu_scaling_max_frequency( { char fpath[PATH_MAX_LENGTH]; char value[16]; - sprintf(fpath, CPU_POLICIES_DIR "policy%u/scaling_max_freq", + snprintf(fpath, sizeof(fpath), CPU_POLICIES_DIR "policy%u/scaling_max_freq", driver->policy_id); - sprintf(value, "%" PRIu32 "\n", max_freq); + snprintf(value, sizeof(value), "%" PRIu32 "\n", max_freq); if (filestream_write_file(fpath, value, strlen(value))) { driver->max_policy_freq = max_freq; @@ -294,7 +294,7 @@ uint32_t get_cpu_scaling_next_frequency_limit(uint32_t freq, int step) bool set_cpu_scaling_governor(cpu_scaling_driver_t *driver, const char* governor) { char fpath[PATH_MAX_LENGTH]; - sprintf(fpath, CPU_POLICIES_DIR "policy%u/scaling_governor", + snprintf(fpath, sizeof(fpath), CPU_POLICIES_DIR "policy%u/scaling_governor", driver->policy_id); if (filestream_write_file(fpath, governor, strlen(governor))) {