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

Fixing issues caught with address sanitizer #1664

Merged
merged 5 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
2 changes: 1 addition & 1 deletion src/batmon/batmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/batmon/batmonDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int get_best_session_time(void)
}
sqlite3_finalize(stmt);
}
close_battery_log_db();
}
return best_time;
}
Expand Down
1 change: 1 addition & 0 deletions src/bootScreen/bootScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/common/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/common/recipes.mk
Original file line number Diff line number Diff line change
@@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion src/common/system/device_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/common/system/lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 4 additions & 1 deletion src/common/theme/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/common/utils/apply_icons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/common/utils/asan.c
Original file line number Diff line number Diff line change
@@ -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";
}
1 change: 1 addition & 0 deletions src/common/utils/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions src/common/utils/keystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/common/utils/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/gameSwitcher/gameSwitcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/infoPanel/infoPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/libgamename/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INCLUDE_UTILS = 0
SANITIZE = 0
include ../common/config.mk

TARGET = libgamename.so
Expand Down
11 changes: 10 additions & 1 deletion src/packageManager/fileActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion src/packageManager/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/packageManager/summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/playActivity/cacheDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 13 additions & 3 deletions src/playActivity/playActivityDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/prompt/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/randomGamePicker/randomGamePicker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading
Loading