diff --git a/src/batmon/batmon.c b/src/batmon/batmon.c index 4ebc110d21..e985c8d35d 100644 --- a/src/batmon/batmon.c +++ b/src/batmon/batmon.c @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) best_session_time = get_best_session_time(); FILE *fp; - int old_percentage = -1, current_percentage, warn_at = 15, last_logged_percentage = -1; + int old_percentage = -1, current_percentage = 0, warn_at = 15, last_logged_percentage = -1; atexit(cleanup); signal(SIGINT, sigHandler); diff --git a/src/batmon/batmonDB.h b/src/batmon/batmonDB.h index e421bce5a3..da39283c96 100644 --- a/src/batmon/batmonDB.h +++ b/src/batmon/batmonDB.h @@ -65,6 +65,7 @@ int get_best_session_time(void) } sqlite3_finalize(stmt); } + close_battery_log_db(); } return best_time; } diff --git a/src/bootScreen/bootScreen.c b/src/bootScreen/bootScreen.c index e41e4c91d2..031f65a8cf 100644 --- a/src/bootScreen/bootScreen.c +++ b/src/bootScreen/bootScreen.c @@ -107,6 +107,7 @@ int main(int argc, char *argv[]) sleep(4); // for debugging purposes #endif + TTF_CloseFont(font); SDL_FreeSurface(screen); SDL_FreeSurface(video); SDL_Quit(); diff --git a/src/common/config.mk b/src/common/config.mk index 3cd7f8b128..c332e14dcb 100644 --- a/src/common/config.mk +++ b/src/common/config.mk @@ -41,8 +41,14 @@ ifeq ($(TEST),1) CFLAGS := $(CFLAGS) -I../include -I../src/common -I$(GTEST_INCLUDE_DIR) endif +ifeq ($(SANITIZE),1) +CFILES := $(CFILES) ../common/utils/asan.c +CFLAGS := $(CFLAGS) -fno-omit-frame-pointer -fsanitize=address -static-libasan +LDFLAGS := -fsanitize=address -static-libasan $(LDFLAGS) +endif + CXXFLAGS := $(CFLAGS) -LDFLAGS := -L../../lib -L/usr/local/lib +LDFLAGS := $(LDFLAGS) -L../../lib -L/usr/local/lib ifeq ($(PLATFORM),miyoomini) CFLAGS := $(CFLAGS) -marm -mtune=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7ve -Wl,-rpath=$(LIB) diff --git a/src/common/recipes.mk b/src/common/recipes.mk index b5489869b8..4092c6c061 100644 --- a/src/common/recipes.mk +++ b/src/common/recipes.mk @@ -1,6 +1,6 @@ $(TARGET): $(OFILES) @$(CXX) $(OFILES) -o "$@" $(LDFLAGS) - @if test -z "$(DEBUG)"; then \ + @if test -z "$(DEBUG)" && test -z "$(SANITIZE)"; then \ $(STRIP) "$@"; \ fi @-mv -f $(TARGET) "$(BUILD_DIR)/$(TARGET)" diff --git a/src/common/system/device_model.h b/src/common/system/device_model.h index 28f2935590..db27ada31a 100644 --- a/src/common/system/device_model.h +++ b/src/common/system/device_model.h @@ -8,7 +8,7 @@ #define MIYOO354 354 static int DEVICE_ID; -static char DEVICE_SN[12]; +static char DEVICE_SN[13]; /** * @brief Get device model diff --git a/src/common/system/lang.h b/src/common/system/lang.h index 6062989673..9a27347c7f 100644 --- a/src/common/system/lang.h +++ b/src/common/system/lang.h @@ -45,8 +45,10 @@ void lang_removeIconLabels(bool remove_icon_labels, bool remove_hints) } } - if (!remove_icon_labels && !remove_hints) + if (!remove_icon_labels && !remove_hints) { + closedir(dp); return; + } // backup lang files if (!exists(LANG_DIR_BACKUP)) @@ -162,7 +164,12 @@ bool lang_load(void) return true; } -void lang_free(void) { free(lang_list); } +void lang_free(void) { + for (int i = 0; i < LANG_MAX; i++) { + free(lang_list[i]); + } + free(lang_list); +} const char *lang_get(lang_hash key, const char *fallback) { diff --git a/src/common/theme/load.h b/src/common/theme/load.h index 8f5258805f..2537840a9e 100644 --- a/src/common/theme/load.h +++ b/src/common/theme/load.h @@ -7,6 +7,7 @@ #include "utils/file.h" #include "utils/str.h" +#include "utils/json.h" #define SYSTEM_CONFIG "/mnt/SDCARD/system.json" #define FALLBACK_FONT "/customer/app/Exo-2-Bold-Italic.ttf" @@ -66,7 +67,9 @@ TTF_Font *theme_loadFont(const char *theme_path, const char *font, int size) char *theme_getPath(char *theme_path) { - file_parseKeyValue(SYSTEM_CONFIG, "theme", theme_path, ':', 0); + cJSON *j = json_load(SYSTEM_CONFIG); + json_getString(j,"theme", theme_path); + cJSON_Delete(j); if (strcmp(theme_path, "./") == 0 || !is_dir(theme_path)) { strcpy(theme_path, FALLBACK_THEME_PATH); diff --git a/src/common/utils/apply_icons.h b/src/common/utils/apply_icons.h index 3b74a38e32..eeb725fa09 100644 --- a/src/common/utils/apply_icons.h +++ b/src/common/utils/apply_icons.h @@ -173,7 +173,9 @@ bool _apply_singleIconFromPack(const char *config_path, json_forceSetString(config, "icon", icon_path); - _saveConfigFile(config_path, cJSON_Print(config)); + char* config_str = cJSON_Print(config); + _saveConfigFile(config_path, config_str); + cJSON_free(config_str); cJSON_Delete(config); printf_debug("Applied icon to %s\nicon: %s\niconsel: %s\n", config_path, diff --git a/src/common/utils/asan.c b/src/common/utils/asan.c new file mode 100644 index 0000000000..aacb640601 --- /dev/null +++ b/src/common/utils/asan.c @@ -0,0 +1,5 @@ + +__attribute__((externally_visible)) +const char *__asan_default_options() { + return "log_path=/mnt/SDCARD/.tmp_update/logs/ASAN.log:halt_on_error=0"; +} \ No newline at end of file diff --git a/src/common/utils/file.c b/src/common/utils/file.c index 05360c7358..d8b96a60db 100644 --- a/src/common/utils/file.c +++ b/src/common/utils/file.c @@ -245,6 +245,7 @@ char *file_parseKeyValue(const char *file_path, const char *key_in, key[0] = 0; val[0] = 0; } + free(line); fclose(fp); } diff --git a/src/common/utils/keystate.h b/src/common/utils/keystate.h index 0314d00538..8e16b0d0bf 100644 --- a/src/common/utils/keystate.h +++ b/src/common/utils/keystate.h @@ -25,6 +25,8 @@ bool updateKeystate(KeyState keystate[320], bool *quit_flag, bool enabled, switch (keystate_event.type) { case SDL_QUIT: *quit_flag = true; + if (changed_key != NULL) + *changed_key = SDLK_UNKNOWN; break; case SDL_KEYDOWN: if (keystate[key] != RELEASED) @@ -42,6 +44,8 @@ bool updateKeystate(KeyState keystate[320], bool *quit_flag, bool enabled, retval = true; break; default: + if (changed_key != NULL) + *changed_key = SDLK_UNKNOWN; break; } } diff --git a/src/common/utils/log.c b/src/common/utils/log.c index a093c70b82..087ca1e84a 100644 --- a/src/common/utils/log.c +++ b/src/common/utils/log.c @@ -24,9 +24,10 @@ void log_debug(const char *file_path, int line, const char *format_str, ...) vsprintf(log_message + strlen(log_message), format_str, valist); va_end(valist); - snprintf(cmd, 1023, "echo -n \"%s\" >/dev/stderr", - str_replace(log_message, "\"", "\\\"")); + char *no_underscore = str_replace(log_message, "\"", "\\\""); + snprintf(cmd, 1023, "echo -n \"%s\" >/dev/stderr", no_underscore); system(cmd); + free(no_underscore); if (strlen(_log_path) == 0) return; diff --git a/src/gameSwitcher/gameSwitcher.c b/src/gameSwitcher/gameSwitcher.c index da9154a6df..15be5c2ba7 100644 --- a/src/gameSwitcher/gameSwitcher.c +++ b/src/gameSwitcher/gameSwitcher.c @@ -252,8 +252,10 @@ void readHistory() sscanf(strstr(jsonContent, "\"type\":") + 7, "%d", &type); - if ((type != 5) && (type != 17)) + if ((type != 5) && (type != 17)) { + free(jsonContent); continue; + } print_debug("type 5"); @@ -450,7 +452,8 @@ int main(int argc, char *argv[]) bool show_time = config_flag_get("gameSwitcher/showTime"); bool show_total = !config_flag_get("gameSwitcher/hideTotal"); bool show_legend = !config_flag_get("gameSwitcher/hideLegend"); - int view_mode = view_min ? VIEW_MINIMAL : VIEW_NORMAL, view_restore; + int view_mode = view_min ? VIEW_MINIMAL : VIEW_NORMAL; + int view_restore = view_mode; SDLKey changed_key = SDLK_UNKNOWN; int button_y_repeat = 0; diff --git a/src/infoPanel/infoPanel.c b/src/infoPanel/infoPanel.c index e6158a4b95..7f17b3a9c3 100644 --- a/src/infoPanel/infoPanel.c +++ b/src/infoPanel/infoPanel.c @@ -93,13 +93,14 @@ static void drawInfoPanel(SDL_Surface *screen, SDL_Surface *video, theme_renderHeader(screen, has_title ? title_str : NULL, !has_title); if (has_message) { - const char *str = str_replace(message_str, "\\n", "\n"); + char *str = str_replace(message_str, "\\n", "\n"); message = theme_textboxSurface(str, resource_getFont(TITLE), theme()->list.color, ALIGN_CENTER); message_rect.x -= message->w / 2; message_rect.y -= message->h / 2; SDL_BlitSurface(message, NULL, screen, &message_rect); SDL_FreeSurface(message); + free(str); } } diff --git a/src/libgamename/Makefile b/src/libgamename/Makefile index ee03b5686b..287ed492ad 100644 --- a/src/libgamename/Makefile +++ b/src/libgamename/Makefile @@ -1,4 +1,5 @@ INCLUDE_UTILS = 0 +SANITIZE = 0 include ../common/config.mk TARGET = libgamename.so diff --git a/src/packageManager/fileActions.h b/src/packageManager/fileActions.h index 6048df8b91..4065075da9 100644 --- a/src/packageManager/fileActions.h +++ b/src/packageManager/fileActions.h @@ -47,8 +47,10 @@ bool checkAppInstalled(const char *basePath, int base_len, int level, bool compl else if (dp->d_type == DT_DIR) is_installed = checkAppInstalled(path, base_len, level + 1, complete); - if (!complete && level >= 2 && exists(pathInstalledApp)) + if (!complete && level >= 2 && exists(pathInstalledApp)) { + closedir(dir); return true; + } if ((complete || level < 2) && !is_installed) run = 0; @@ -81,11 +83,13 @@ bool getConfigPath(char *config_path, const char *data_path, const char *base_di continue; sprintf(config_path, "%s/%s/config.json", base_dir, dp->d_name); if (!is_file(config_path)) { + closedir(dir); return false; } break; } + closedir(dir); return true; } @@ -131,6 +135,7 @@ bool checkRomDir(const char *rom_dir, const char *extlist, int level) char subdir[PATH_MAX]; snprintf(subdir, PATH_MAX - 1, "%s/%s", rom_dir, dp->d_name); if (checkRomDir(subdir, extlist, level + 1)) { + closedir(dir); return true; } } @@ -141,10 +146,12 @@ bool checkRomDir(const char *rom_dir, const char *extlist, int level) if (!hasExtension(dp->d_name, extlist)) continue; + closedir(dir); return true; } } + closedir(dir); return false; } @@ -279,9 +286,11 @@ bool getPackageMainPath(char *out_path, const char *data_path, if (dp->d_type != DT_DIR) continue; sprintf(out_path, "/mnt/SDCARD/%s/%s", base_dir, dp->d_name); + closedir(dir); return is_dir(out_path); } + closedir(dir); return false; } diff --git a/src/packageManager/globals.h b/src/packageManager/globals.h index 85e9951d92..112604043b 100644 --- a/src/packageManager/globals.h +++ b/src/packageManager/globals.h @@ -48,7 +48,10 @@ static int nTab = 0; static int changes_installs[] = {0, 0, 0, 0}; static int changes_removals[] = {0, 0, 0, 0}; -static SDL_Surface *video = NULL, *screen = NULL, *surfaceBackground = NULL, +// Memory is free-ed by SDL_Quit +static SDL_Surface *video = NULL; + +static SDL_Surface *screen = NULL, *surfaceBackground = NULL, *surfaceSelection = NULL, *surfaceTableau = NULL, *surfaceScroller = NULL, *surfaceMarker = NULL, *surfaceCheck = NULL, *surfaceCross = NULL, @@ -107,6 +110,8 @@ void freeResources(void) TTF_CloseFont(font35); TTF_Quit(); + SDL_FreeSurface(screen); + SDL_FreeSurface(surfaceSummary); SDL_FreeSurface(surfaceCheck); SDL_FreeSurface(surfaceCross); SDL_FreeSurface(surfaceBackground); diff --git a/src/packageManager/summary.h b/src/packageManager/summary.h index 7f3de81fdc..7e14c166ab 100644 --- a/src/packageManager/summary.h +++ b/src/packageManager/summary.h @@ -94,6 +94,7 @@ void renderSummary() 0x000000FF, 0xFF000000); /* important */ SDL_FillRect(surfaceSummary, NULL, 0x000000FF); SDL_BlitSurface(surfaceTemp, NULL, surfaceSummary, NULL); + SDL_FreeSurface(surfaceTemp); } SDL_BlitSurface(surfaceSummary, &rectSummaryFrame, screen, &rectSummaryPos); diff --git a/src/playActivity/cacheDB.h b/src/playActivity/cacheDB.h index 2840752708..7923e2039b 100644 --- a/src/playActivity/cacheDB.h +++ b/src/playActivity/cacheDB.h @@ -103,21 +103,25 @@ CacheDBItem *cache_db_find(const char *path_or_name) CacheDBItem *cache_db_item = NULL; char cache_db_file_path[STR_MAX]; char cache_type[STR_MAX]; + char *_path_or_name = strdup(path_or_name); char rel_path[PATH_MAX]; if (!file_path_relative_to(rel_path, "/mnt/SDCARD/Roms", path_or_name)) { if (strstr(path_or_name, "../../Roms/") != NULL) { - strcpy(rel_path, str_split(strdup((const char *)path_or_name), "../../Roms/")); + strcpy(rel_path, str_split(_path_or_name, "../../Roms/")); } else { - strcpy(rel_path, str_replace(strdup((const char *)path_or_name), "/mnt/SDCARD/Roms/", "")); + char *tunc_path_or_name = str_replace(_path_or_name, "/mnt/SDCARD/Roms/", ""); + strcpy(rel_path, tunc_path_or_name); + free(tunc_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(file_basename(path_or_name)); + char *game_name = file_removeExtension(file_basename(_path_or_name)); + free(_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); diff --git a/src/playActivity/playActivityDB.h b/src/playActivity/playActivityDB.h index 3af8158838..3e6d5caaad 100644 --- a/src/playActivity/playActivityDB.h +++ b/src/playActivity/playActivityDB.h @@ -187,6 +187,8 @@ PlayActivities *play_activity_find_all(void) PlayActivity *entry = play_activities->play_activity[i] = (PlayActivity *)malloc(sizeof(PlayActivity)); ROM *rom = play_activities->play_activity[i]->rom = (ROM *)malloc(sizeof(ROM)); + entry->first_played_at = NULL; + entry->last_played_at = NULL; rom->id = sqlite3_column_int(stmt, 0); rom->type = strdup((const char *)sqlite3_column_text(stmt, 1)); @@ -220,6 +222,8 @@ PlayActivities *play_activity_find_all(void) void free_play_activities(PlayActivities *pa_ptr) { for (int i = 0; i < pa_ptr->count; i++) { + free(pa_ptr->play_activity[i]->first_played_at); + free(pa_ptr->play_activity[i]->last_played_at); free(pa_ptr->play_activity[i]->rom); free(pa_ptr->play_activity[i]); } @@ -461,7 +465,9 @@ void play_activity_start(char *rom_file_path) if (rom_id == ROM_NOT_FOUND) { exit(1); } - play_activity_db_execute(sqlite3_mprintf("INSERT INTO play_activity(rom_id) VALUES(%d);", rom_id)); + char *sql = sqlite3_mprintf("INSERT INTO play_activity(rom_id) VALUES(%d);", rom_id); + play_activity_db_execute(sql); + sqlite3_free(sql); } void play_activity_resume(void) @@ -472,7 +478,9 @@ void play_activity_resume(void) printf("Error: no active rom\n"); exit(1); } - play_activity_db_execute(sqlite3_mprintf("INSERT INTO play_activity(rom_id) VALUES(%d);", rom_id)); + char *sql = sqlite3_mprintf("INSERT INTO play_activity(rom_id) VALUES(%d);", rom_id); + play_activity_db_execute(sql); + sqlite3_free(sql); } void play_activity_stop(char *rom_file_path) @@ -482,7 +490,9 @@ void play_activity_stop(char *rom_file_path) if (rom_id == ROM_NOT_FOUND) { exit(1); } - play_activity_db_execute(sqlite3_mprintf("UPDATE play_activity SET play_time = (strftime('%%s', 'now')) - created_at, updated_at = (strftime('%%s', 'now')) WHERE rom_id = %d AND play_time IS NULL;", rom_id)); + char *sql = sqlite3_mprintf("UPDATE play_activity SET play_time = (strftime('%%s', 'now')) - created_at, updated_at = (strftime('%%s', 'now')) WHERE rom_id = %d AND play_time IS NULL;", rom_id); + play_activity_db_execute(sql); + sqlite3_free(sql); } void play_activity_stop_all(void) diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index a67ace6827..8a8004b8a9 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -152,7 +152,7 @@ int main(int argc, char *argv[]) bool footer_changed = true; bool battery_changed = true; - SDLKey changed_key; + SDLKey changed_key = SDLK_UNKNOWN; bool key_changed = false; #ifdef PLATFORM_MIYOOMINI diff --git a/src/randomGamePicker/randomGamePicker.c b/src/randomGamePicker/randomGamePicker.c index 1628cf87ba..f875cf82a8 100644 --- a/src/randomGamePicker/randomGamePicker.c +++ b/src/randomGamePicker/randomGamePicker.c @@ -225,7 +225,10 @@ bool addRandomFromJson(char *json_path) while (fgets(line, sizeof(line), fp)) { json_root = cJSON_Parse(line); - json_getInt(json_root, "type", (int *)&type); + if(!json_getInt(json_root, "type", (int *)&type)) { + print_debug("Malformed json; Skipping\n"); + continue; + } if (type == TYPE_GAME || type == TYPE_EXPERT) { GameEntry *game = &random_games[count]; diff --git a/src/read_uuid/read_uuid.c b/src/read_uuid/read_uuid.c index 5c51532613..3be2ee8ce1 100644 --- a/src/read_uuid/read_uuid.c +++ b/src/read_uuid/read_uuid.c @@ -55,6 +55,7 @@ int main() char *output = execute_command(commands[i]); if (output != NULL) { strcat(serial, output); + free(output); } else { fprintf(stderr, "Error executing command: %s\n", commands[i]); diff --git a/src/themeSwitcher/themeSwitcher.c b/src/themeSwitcher/themeSwitcher.c index b8d55f7f37..ab53e2e1b1 100644 --- a/src/themeSwitcher/themeSwitcher.c +++ b/src/themeSwitcher/themeSwitcher.c @@ -429,5 +429,9 @@ int main(int argc, char *argv[]) SDL_Quit(); + TTF_CloseFont(font40); + TTF_CloseFont(font21); + TTF_CloseFont(font30); + return EXIT_SUCCESS; } diff --git a/src/tweaks/icons.h b/src/tweaks/icons.h index ba0e23a2ba..9caa7dc103 100644 --- a/src/tweaks/icons.h +++ b/src/tweaks/icons.h @@ -273,7 +273,7 @@ bool _add_config_icon(const char *path, const char *name, char short_label[56]; str_trim(short_label, 55, label, false); - short_label[56] = 0; + short_label[55] = 0; if (mode != ICON_MODE_APP) snprintf(item.label, STR_MAX - 1, "%s (%s)", short_label, icon_name); diff --git a/src/tweaks/menus.h b/src/tweaks/menus.h index 8699991862..8490b42488 100644 --- a/src/tweaks/menus.h +++ b/src/tweaks/menus.h @@ -673,15 +673,18 @@ void menu_diagnostics(void *pt) .action = action_runDiagnosticScript, }; - const char *prefix = ""; + const char *prefix; if (strncmp(scripts[i].filename, "util", 4) == 0) { - prefix = "Util: "; + prefix = "Util:%.62s"; } else if (strncmp(scripts[i].filename, "fix", 3) == 0) { - prefix = "Fix: "; + prefix = "Fix:%.62s"; + } + else { + prefix = "%.62s"; } - snprintf(diagItem.label, DIAG_MAX_LABEL_LENGTH - 1, "%s%.62s", prefix, scripts[i].label); + snprintf(diagItem.label, DIAG_MAX_LABEL_LENGTH - 1, prefix, scripts[i].label); strncpy(diagItem.sticky_note, "Idle: Selected script not running", STR_MAX - 1); char *parsed_Tooltip = diags_parseNewLines(scripts[i].tooltip); diff --git a/src/tweaks/tweaks.c b/src/tweaks/tweaks.c index 9e5993a86e..d31ca42fbe 100644 --- a/src/tweaks/tweaks.c +++ b/src/tweaks/tweaks.c @@ -102,7 +102,8 @@ int main(int argc, char *argv[]) bool menu_combo_pressed = false; bool key_changed = false; - SDLKey changed_key; + SDLKey changed_key = SDLK_UNKNOWN; + bool show_help_tooltip = !config_flag_get(".tweaksHelpCompleted"); diff --git a/src/tweaks/values.h b/src/tweaks/values.h index 79f762e0a7..a7e3ba68e2 100644 --- a/src/tweaks/values.h +++ b/src/tweaks/values.h @@ -238,7 +238,7 @@ void value_setFrameThrottle(void) int value_getSwapTriggers(void) { - int l_btn, r_btn, l2_btn, r2_btn; + int l_btn = 0, r_btn = 0, l2_btn = 0, r2_btn = 0; char value[STR_MAX]; if (file_parseKeyValue(RETROARCH_CONFIG, "input_player1_l_btn", value, '=',