aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-10-01 02:07:50 +0000
committerdam <dam@gudinoff>2022-10-01 02:07:50 +0000
commit359e6b6df688b06deee7d7f99c40b5506a23ffc1 (patch)
treed150dda9bc694f8b37d13954d63d18a245e24daa
parent31923fe0598464bb9745ca8fc2d7f78463e896f8 (diff)
downloadtask-time-tracker-359e6b6df688b06deee7d7f99c40b5506a23ffc1.tar.zst
task-time-tracker-359e6b6df688b06deee7d7f99c40b5506a23ffc1.zip
Fixed UB caused by invalid pointer access.
-rw-r--r--main.c26
1 files changed, 13 insertions, 13 deletions
diff --git a/main.c b/main.c
index d239bef..8817d6d 100644
--- a/main.c
+++ b/main.c
@@ -55,14 +55,12 @@ const int64_t SECONDS_IN_YEAR = (int64_t)365*SECONDS_IN_DAY;
database_st database = { .tasks = NULL };
database_st archive = { .tasks = NULL };
database_st *db = NULL;
-// char *app_folder = NULL;
-// char *db_file_path = NULL;
-// char *ar_file_path = NULL;
-char *string_buffer = NULL;
-int size_x, size_y, pos_x, pos_y;
char *app_folder = NULL;
char *db_file_path = NULL;
char *ar_file_path = NULL;
+char *string_buffer = NULL;
+int size_x, size_y, pos_x, pos_y;
+
// Checks if file is exists and is accessible.
// Returns true when the file exists and is accessible.
@@ -382,6 +380,7 @@ bool load_database(database_st *db, const char *path) {
fread(&file_signature, sizeof(char), DB_FILE_SIGN_LENGTH, file);
if (strncmp(file_signature, DB_FILE_SIGN, DB_FILE_SIGN_LENGTH) != 0) {
fprintf(stderr, "Invalid file signature.\n");
+ fclose(file);
return false;
}
@@ -617,9 +616,6 @@ void update_total_timers(database_st *db) {
#define NUM_TABLE_ROWS (size_y - NUM_HEADER_ROWS - NUM_FOOTER_ROWS)
#define NUM_COLUMNS 9
-#define L_NORMAL 0
-#define L_COMPACT 1
-
#define L_TITLE_IDX 0
#define L_DAYS_IDX 1
#define L_TOTAL_IDX 8
@@ -630,13 +626,17 @@ void update_total_timers(database_st *db) {
#define THEME_D 4
#define THEME_E 5
-
+typedef enum {
+ L_NORMAL,
+ L_COMPACT,
+ NUM_LAYOUTS,
+} layouts_et;
typedef struct {
char *header;
int width;
- char alignment;
int alignment_offset;
+ char alignment;
} column_st;
typedef struct {
@@ -644,7 +644,7 @@ typedef struct {
char *archive_title;
} layout_st;
-layout_st layouts[2];
+layout_st layouts[NUM_LAYOUTS];
void initialize_tui() {
@@ -681,8 +681,8 @@ void initialize_tui() {
};
// Calculate alignment_offsets.
- for(layout_st *layout = layouts; layout <= layouts + 1; layout++) {
- for (column_st *col = layout->columns; col <= layout->columns + NUM_COLUMNS; col++) {
+ for(layout_st *layout = layouts; layout < layouts + NUM_LAYOUTS; layout++) {
+ for (column_st *col = layout->columns; col < layout->columns + NUM_COLUMNS; col++) {
int offset;
switch(col->alignment) {
default: