Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common file.c util leaks #1661

Merged
merged 6 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bootScreen/bootScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ int main(int argc, char *argv[])
SDL_Color color = theme()->total.color;

if (show_version) {
const char *version_str = file_read("/mnt/SDCARD/.tmp_update/onionVersion/version.txt");
char *version_str = file_read("/mnt/SDCARD/.tmp_update/onionVersion/version.txt");
if (strlen(version_str) > 0) {
SDL_Surface *version = TTF_RenderUTF8_Blended(font, version_str, color);
if (version) {
SDL_BlitSurface(version, NULL, screen, &(SDL_Rect){20, 450 - version->h / 2});
SDL_FreeSurface(version);
}
}
free(version_str);
}

if (strlen(message_str) > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/common/components/JsonGameEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ JsonGameEntry JsonGameEntry_fromJson(const char *json_str)
json_getInt(root, "type", &entry.type);
json_getString(root, "rompath", entry.rompath);
json_getString(root, "imgpath", entry.imgpath);
cJSON_Delete(root);

strcpy(entry.emupath, entry.rompath);
str_split(entry.emupath, "/../../");
Expand Down
3 changes: 2 additions & 1 deletion src/common/system/lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ void lang_removeIconLabels(bool remove_icon_labels, bool remove_hints)
char file_path[STR_MAX * 2];
snprintf(file_path, STR_MAX * 2 - 1, LANG_DIR "/%s", ep->d_name);

const char *json_data = file_read(file_path);
char *json_data = file_read(file_path);
cJSON *root = cJSON_Parse(json_data);
free(json_data);

if (!root)
continue;
Expand Down
13 changes: 9 additions & 4 deletions src/common/system/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool __get_path_romscreen(char *path_out)

bool __get_path_recent(char *path_out)
{
char *fnptr;
char *fnptr, *no_extension;
uint32_t i;

strcpy(path_out, "/mnt/SDCARD/Screenshots/");
Expand All @@ -43,8 +43,11 @@ bool __get_path_recent(char *path_out)

if (system_state == MODE_GAME && (process_searchpid("retroarch") != 0 || process_searchpid("ra32") != 0)) {
char file_path[STR_MAX];
if (history_getRecentPath(file_path) != NULL)
strcat(path_out, file_removeExtension(basename(file_path)));
if (history_getRecentPath(file_path) != NULL) {
no_extension = file_removeExtension(basename(file_path));
strcat(path_out, no_extension);
free(no_extension);
}
}
else if (system_state == MODE_SWITCHER)
strcat(path_out, "GameSwitcher");
Expand All @@ -61,7 +64,9 @@ bool __get_path_recent(char *path_out)
if (strstr(cmd, "; chmod") != NULL)
state_getAppName(app_name, cmd);
else {
strcpy(app_name, file_removeExtension(basename(cmd)));
no_extension = file_removeExtension(basename(cmd));
strcpy(app_name, no_extension);
free(no_extension);
}
printf_debug("app: '%s'\n", app_name);

Expand Down
3 changes: 2 additions & 1 deletion src/common/system/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ void _settings_load_keymap(void)

void _settings_load_mainui(void)
{
const char *json_str = NULL;
char *json_str = NULL;

if (!(json_str = file_read(MAIN_UI_SETTINGS)))
return;

cJSON *json_root = cJSON_Parse(json_str);
free(json_str);

json_getInt(json_root, "vol", &settings.volume);
json_getInt(json_root, "bgmvol", &settings.bgm_volume);
Expand Down
12 changes: 8 additions & 4 deletions src/common/system/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ static pid_t system_state_pid = 0;

bool check_isRetroArch(void)
{
bool rc = false;
if (!exists(CMD_TO_RUN_PATH))
return false;
const char *cmd = file_read(CMD_TO_RUN_PATH);
char *cmd = file_read(CMD_TO_RUN_PATH);
if (strstr(cmd, "retroarch") != NULL ||
strstr(cmd, "/mnt/SDCARD/Emu/") != NULL ||
strstr(cmd, "/mnt/SDCARD/RApp/") != NULL) {
pid_t pid;
if ((pid = process_searchpid("retroarch")) != 0 ||
(pid = process_searchpid("ra32")) != 0) {
system_state_pid = pid;
return true;
rc = true;
}
}
return false;
free(cmd);
return rc;
}

bool check_isMainUI(void)
Expand Down Expand Up @@ -390,8 +392,10 @@ void resumeGame(int n)
if (lineCount > 1) {
temp_flag_set("quick_switch", true);

file_add_line_to_beginning(getMiyooRecentFilePath(), file_read_lineN(getMiyooRecentFilePath(), lineCount));
char * line_n = file_read_lineN(getMiyooRecentFilePath(), lineCount);
file_add_line_to_beginning(getMiyooRecentFilePath(), line_n);
file_delete_line(getMiyooRecentFilePath(), lineCount + 1);
free(line_n);
}

file_put_sync(fp, CMD_TO_RUN_PATH, "%s", LaunchCommand);
Expand Down
3 changes: 2 additions & 1 deletion src/common/theme/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ void json_fontStyle(cJSON *root, FontStyle_s *dest, FontStyle_s *fallback)
bool theme_applyConfig(Theme_s *config, const char *config_path,
bool use_fallbacks)
{
const char *json_str = NULL;
char *json_str = NULL;

if (!exists(config_path) || !(json_str = file_read(config_path)))
return false;

// Get JSON objects
cJSON *json_root = cJSON_Parse(json_str);
free(json_str);
cJSON *json_batteryPercentage =
cJSON_GetObjectItem(json_root, "batteryPercentage");
cJSON *json_hideLabels = cJSON_GetObjectItem(json_root, "hideLabels");
Expand Down
11 changes: 7 additions & 4 deletions src/common/utils/apply_icons.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ bool _apply_singleIconFromPack(const char *config_path,
if (!json_getString(config, "icon", temp_path))
return false;

char icon_name[56];
strncpy(icon_name, file_removeExtension(basename(temp_path)), 55);
char *icon_name = file_removeExtension(basename(temp_path));
str_split(icon_name, "-");

IconMode_e mode = icons_getIconMode(config_path);
Expand All @@ -156,13 +155,16 @@ bool _apply_singleIconFromPack(const char *config_path,
icon_name);
}

if (!is_file(icon_path))
if (!is_file(icon_path)) {
free(icon_name);
return false;
}
}

char sel_path[STR_MAX];
sprintf(sel_path, icons_getSelectedIconPathFormat(mode), icon_pack_path,
icon_name);
free(icon_name);

if (is_file(sel_path))
json_forceSetString(config, "iconsel", sel_path);
Expand All @@ -183,7 +185,7 @@ bool _apply_singleIconFromPack(const char *config_path,
bool apply_singleIcon(const char *config_path)
{
char icon_pack_path[STR_MAX];
const char *active_icon_pack = file_read(ACTIVE_ICON_PACK);
char *active_icon_pack = file_read(ACTIVE_ICON_PACK);

if (active_icon_pack != NULL && is_dir(active_icon_pack))
strncpy(icon_pack_path, active_icon_pack, STR_MAX - 1);
Expand All @@ -199,6 +201,7 @@ bool apply_singleIcon(const char *config_path)
_apply_singleIconFromPack(GUEST_ON_CONFIG, icon_pack_path, false);
}

free(active_icon_pack);
return _apply_singleIconFromPack(config_path, icon_pack_path, false);
}

Expand Down
16 changes: 12 additions & 4 deletions src/common/utils/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ bool file_isModified(const char *path, time_t *old_mtime)
return false;
}

const char *file_basename(const char *filename)
{
char *p = strrchr(filename, '/');
return p ? p + 1 : (char *) filename;
}

/**
* @brief Create directories in dir_path using `mkdir -p` command.
*
Expand Down Expand Up @@ -100,7 +106,7 @@ void file_readLastLine(const char *filename, char *out_str)
}
}

const char *file_read(const char *path)
char *file_read(const char *path)
{
FILE *f = NULL;
char *buffer = NULL;
Expand Down Expand Up @@ -141,7 +147,7 @@ void file_copy(const char *src_path, const char *dest_path)
system(system_cmd);
}

char *file_removeExtension(char *myStr)
char *file_removeExtension(const char *myStr)
{
if (myStr == NULL)
return NULL;
Expand Down Expand Up @@ -173,7 +179,7 @@ char *extractPath(const char *absolutePath)

void file_cleanName(char *name_out, const char *file_name)
{
char *name_without_ext = file_removeExtension(strdup(file_name));
char *name_without_ext = file_removeExtension(file_name);
char *no_underscores = str_replace(name_without_ext, "_", " ");
char *dot_ptr = strstr(no_underscores, ".");
if (dot_ptr != NULL) {
Expand Down Expand Up @@ -450,7 +456,9 @@ void file_add_line_to_beginning(const char *filename, const char *lineToAdd)
return;
}
char tempPath[STR_MAX];
sprintf(tempPath, "%s/temp.txt", extractPath(filename));
char *path = extractPath(filename);
sprintf(tempPath, "%s/temp.txt", path);
free(path);

FILE *tempFile = fopen(tempPath, "w");
if (tempFile == NULL) {
Expand Down
25 changes: 21 additions & 4 deletions src/common/utils/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ bool is_file(const char *file_path);
bool is_dir(const char *file_path);
bool file_isModified(const char *path, time_t *old_mtime);

/**
* @brief returns the filename component of a path
*
* This is a copy of the GNU `basename` version and
* retains all the quirks that come along with it
*
* See 'Versions' here:
* https://man7.org/linux/man-pages/man3/basename.3.html
*
* Copied from:
* https://sourceware.org/git/?p=glibc.git;a=blob;f=string/basename.c;h=d5b5d4763dd3fa307497cc99788b0bb24c95bcf1;hb=refs/heads/master#l22
*
* @param filename The full file path.
* @return * char*
*/
const char *file_basename(const char *filename);

/**
* @brief Create directories in dir_path using `mkdir -p` command.
*
Expand All @@ -76,15 +93,15 @@ bool mkdirs(const char *dir_path);

void file_readLastLine(const char *filename, char *out_str);

const char *file_read(const char *path);
char *file_read(const char *path) __attribute__((malloc));

bool file_write(const char *path, const char *str, uint32_t len);

void file_copy(const char *src_path, const char *dest_path);

char *file_removeExtension(char *myStr);
char *file_removeExtension(const char *myStr) __attribute__((malloc));

char *extractPath(const char *absolutePath);
char *extractPath(const char *absolutePath) __attribute__((malloc));

char *extractLastDirectory(const char *path);

Expand All @@ -104,7 +121,7 @@ bool file_findNewest(const char *dir_path, char *newest_file, size_t buffer_size

FILE *file_open_ensure_path(const char *path, const char *mode);

char *file_read_lineN(const char *filename, int n);
char *file_read_lineN(const char *filename, int n) __attribute__((malloc));

void file_delete_line(const char *fileName, int n);

Expand Down
6 changes: 5 additions & 1 deletion src/common/utils/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "./file.h"
#include "cjson/cJSON.h"
Expand Down Expand Up @@ -88,7 +89,10 @@ bool json_forceSetString(cJSON *object, const char *key, const char *value)
*/
cJSON *json_load(const char *file_path)
{
return cJSON_Parse(file_read(file_path));
char *file_contents = file_read(file_path);
cJSON *json_contents = cJSON_Parse(file_contents);
free(file_contents);
return json_contents;
}

void json_save(cJSON *object, char *file_path)
Expand Down
4 changes: 3 additions & 1 deletion src/gameSwitcher/gameSwitcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ void getGameName(char *name_out, const char *rom_path)
free(cache_item);
}
else {
strcpy(name_out, file_removeExtension(basename(strdup(rom_path))));
char *no_extension = file_removeExtension(file_basename(rom_path));
strcpy(name_out, no_extension);
free(no_extension);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/infoPanel/infoPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static bool loadImagesPathsFromJson(const char *config_path,
int *images_paths_count,
char ***images_titles)
{
const char *json_str = NULL;
char *json_str = NULL;

char temp_path[STR_MAX];
strncpy(temp_path, config_path, STR_MAX - 1);
Expand All @@ -42,6 +42,7 @@ static bool loadImagesPathsFromJson(const char *config_path,

// Get JSON objects
cJSON *json_root = cJSON_Parse(json_str);
free(json_str);
cJSON *json_images_array = cJSON_GetObjectItem(json_root, "images");
*images_paths_count = cJSON_GetArraySize(json_images_array);
*images_paths = (char **)malloc(*images_paths_count * sizeof(char *));
Expand Down Expand Up @@ -362,10 +363,12 @@ int main(int argc, char *argv[])
g_image_index >= 0) {
current_image_path = g_images_paths[g_image_index];
}
char *no_extension = file_removeExtension(basename(current_image_path));
theme_renderHeader(
screen,
file_removeExtension(basename(current_image_path)),
no_extension,
false);
free(no_extension);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/playActivity/cacheDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ CacheDBItem *cache_db_find(const char *path_or_name)
char *sql;
int cache_version = cache_get_path(cache_db_file_path, cache_type, path_or_name);

char *game_name = file_removeExtension(basename(strdup(path_or_name)));
char *game_name = file_removeExtension(file_basename(path_or_name));

if (cache_version == 2) {
sql = sqlite3_mprintf("SELECT disp, path, imgpath FROM %q_roms WHERE path LIKE '%%%q' OR disp = %Q LIMIT 1;", cache_type, rel_path, game_name);
Expand All @@ -129,6 +129,7 @@ CacheDBItem *cache_db_find(const char *path_or_name)
printf("No cache db found\n");
return NULL;
}
free(game_name);

sqlite3_stmt *stmt = cache_db_prepare(cache_db_file_path, sql);
sqlite3_free(sql);
Expand Down
5 changes: 4 additions & 1 deletion src/playActivity/migrateDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,14 @@ void migrateDB(void)

while (sqlite3_step(stmt) == SQLITE_ROW && is_found == false) {
char *rom_path = (char *)sqlite3_column_text(stmt, 1);
char *no_extension = file_removeExtension(rom_path);

if (strcmp(basename(rom_path), rom_list[i].name) != 0 &&
strcmp(basename(file_removeExtension(rom_path)), rom_list[i].name) != 0) {
strcmp(basename(no_extension), rom_list[i].name) != 0) {
free(no_extension);
continue;
}
free(no_extension);

is_found = true;

Expand Down
Loading
Loading