diff options
| author | dam <dam@gudinoff> | 2022-11-06 01:16:33 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-11-06 01:16:33 +0000 |
| commit | 7ae8985de75a2ce680ea5c5961ed5d9e36505aba (patch) | |
| tree | c2236e927941b2715d2a4cc330fc1584ad31f925 | |
| parent | d8e4a73577c21e13268bc0884562a73c6368c578 (diff) | |
| download | task-time-tracker-7ae8985de75a2ce680ea5c5961ed5d9e36505aba.tar.zst task-time-tracker-7ae8985de75a2ce680ea5c5961ed5d9e36505aba.zip | |
Check for memory allocation errors.
| -rw-r--r-- | main.c | 31 |
1 files changed, 24 insertions, 7 deletions
@@ -573,10 +573,15 @@ bool load_database(database_st *db, const char *path) { // Read database structure. fread(db, SIZEOF_DATABASE_ST, 1, file); - // Restore database capacity. - db->tasks = malloc(db->capacity * SIZEOF_TASK_ST); + // Reserve database capacity for tasks. + size_t capacity_bytes = db->capacity * SIZEOF_TASK_ST; + db->tasks = malloc(capacity_bytes); + if (db->tasks == NULL && capacity_bytes > 0) { + fprintf(stderr, "Failed to allocate memory while loading database: %s.\n", strerror(errno)); + return false; + } - // Read database entries. + // Read database tasks. fread(db->tasks, SIZEOF_TASK_ST, db->count, file); // Make sure we are reading all the file. @@ -1062,7 +1067,10 @@ bool initialize_app_folder() { { temp_size = strlen(home_path) + 1 + strlen(APP_FOLDER_NAME) + 1; // Add space for folder separator and '\0'. app_folder = malloc(temp_size); - // TODO Check malloc result. + if (app_folder == NULL && temp_size > 0) { + fprintf(stderr, "Failed to allocate memory for app folder: %s.\n", strerror(errno)); + return false; + } snprintf(app_folder, temp_size, "%s/%s", home_path, APP_FOLDER_NAME); // Create app folder. @@ -1075,20 +1083,29 @@ bool initialize_app_folder() { else { temp_size = 3; app_folder = malloc(temp_size); - // TODO Check malloc result. + if (app_folder == NULL && temp_size > 0) { + fprintf(stderr, "Failed to allocate memory for app folder: %s.\n", strerror(errno)); + return false; + } snprintf(app_folder, temp_size, "./"); } // Set database file path. temp_size = strlen(app_folder) + 1 + strlen(DB_FILE_NAME) + 1; // Add space for folder separator and '\0'. db_file_path = malloc(temp_size); - // TODO Check malloc result. + if (db_file_path == NULL && temp_size > 0) { + fprintf(stderr, "Failed to allocate memory for database file path: %s.\n", strerror(errno)); + return false; + } snprintf(db_file_path, temp_size, "%s/%s", app_folder, DB_FILE_NAME); // Set archive file path. temp_size = strlen(app_folder) + 1 + strlen(AR_FILE_NAME) + 1; // Add space for folder separator and '\0'. ar_file_path = malloc(temp_size); - // TODO Check malloc result. + if (ar_file_path == NULL && temp_size > 0) { + fprintf(stderr, "Failed to allocate memory for archive file path: %s.\n", strerror(errno)); + return false; + } snprintf(ar_file_path, temp_size, "%s/%s", app_folder, AR_FILE_NAME); return true; |
