From 13880cca63aeb0fe02ac19acf0162b8cf4e555dd Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Santos Souza de Miranda Date: Sun, 28 Apr 2024 23:38:34 -0300 Subject: [PATCH] Fixed some segfaults due not checking for null pointers --- include/account.h | 4 ++++ include/entry.h | 6 +++++- include/money.h | 10 ++++++++-- include/subaccount.h | 5 +++++ include/taccount.h | 4 +++- src/account.c | 4 ++-- src/entry.c | 17 +++++++++++------ src/main.c | 21 +++++++++++++++++++++ src/subaccount.c | 22 +++++++++++++--------- src/taccount.c | 2 +- 10 files changed, 73 insertions(+), 22 deletions(-) diff --git a/include/account.h b/include/account.h index c5fb6a5..3ba7cd9 100644 --- a/include/account.h +++ b/include/account.h @@ -20,5 +20,9 @@ typedef struct account { } Account; +Account *initializeAccount(char *name, char *description); +void updateAccountValue(Account *account, Money *value); +void addSubAccountToAccount(Account *account, SubAccount *subaccount); +void destroyAccount(Account *account); #endif diff --git a/include/entry.h b/include/entry.h index 4a33551..bcef60f 100644 --- a/include/entry.h +++ b/include/entry.h @@ -20,6 +20,10 @@ typedef struct entry { struct entry *next; ///< Pointer to the next entry in the list of entries } Entry; - +Entry *initializeEntry(char *name, char *description); +void setEntryValue(Entry *entry, Money *value); +void appendEntry(Entry *entry, Entry *newEntry); +void traverseEntry(Entry *entry); +void destroyEntry(Entry *entry); #endif \ No newline at end of file diff --git a/include/money.h b/include/money.h index ad2707e..a357e14 100644 --- a/include/money.h +++ b/include/money.h @@ -19,9 +19,15 @@ typedef struct money { int cents; ///< The Cents parts of the monetary value } Money; -Money *initializeMoney(int full, int cents); +Money *initalizeMoney(int full, int cents); void addMoneyToMoney(Money *dest, Money *src); void subtractMoneyToMoney(Money *dest, Money *src); - +bool checkOverflow(Money *m); +bool checkUnderflow(Money *m); +void handleOverflow(Money *m); +void handleUnderflow(Money *m); +void setMoneyValue(Money *m, int full, int cents); +char *getMoneyString(Money *m); +void destroyMoney(Money *money); #endif \ No newline at end of file diff --git a/include/subaccount.h b/include/subaccount.h index af534d7..b6f505f 100644 --- a/include/subaccount.h +++ b/include/subaccount.h @@ -24,5 +24,10 @@ typedef struct subaccount { struct subaccount *next; ///< Reference to the next subaccount } SubAccount; +SubAccount *initializeSubAccount(char *name, char *description); +void updateSubAccountValue(SubAccount *subaccount, Money *value); +void addEntryToSubAccount(SubAccount *subaccount, Entry *entry); +void appendSubAccount(SubAccount *subaccount, SubAccount *newSubaccount); +void destroySubAccount(SubAccount *subaccount); #endif \ No newline at end of file diff --git a/include/taccount.h b/include/taccount.h index 132ead6..53151a6 100644 --- a/include/taccount.h +++ b/include/taccount.h @@ -18,5 +18,7 @@ typedef struct taccount { } TAccount; - +TAccount *initializeTAccount(); +void destroyTAccount(TAccount *taccount); +Account *getListAccountsPointers(TAccount *tAccount); #endif \ No newline at end of file diff --git a/src/account.c b/src/account.c index ccb4145..cf25322 100644 --- a/src/account.c +++ b/src/account.c @@ -3,7 +3,7 @@ * Implementation file to use the account structure */ #include "account.h" - +#include "money.h" /** * * @brief Initialize account structure, @@ -19,7 +19,7 @@ Account *initializeAccount(char *name, char *description) { Account *account = malloc(sizeof(Account)); account->name = name; account->description = description; - account->value = initalizeMoney(0, 0); + account->value = initalizeMoney(0,0); account->subaccounts = NULL; account->numberOfSubaccounts = 0; return account; diff --git a/src/entry.c b/src/entry.c index 9496ae6..dda28b5 100644 --- a/src/entry.c +++ b/src/entry.c @@ -23,6 +23,7 @@ Entry *initializeEntry(char *name, char *description) { entry->name = name; entry->description = description; entry->next = NULL; + return entry; } @@ -89,18 +90,22 @@ void traverseEntry(Entry *entry) { * */ void destroyEntry(Entry *entry){ + if(entry == NULL){ + return; + } if (entry->value != NULL){ destroyMoney(entry->value); entry->value = NULL; } Entry *current = entry-> next; - - if (current->next == NULL){ - destroyMoney(current->value); - free(current); - }else{ - destroyEntry(current->next); + if (current != NULL) { + if (current->next == NULL) { + destroyMoney(current->value); + free(current); + } else { + destroyEntry(current->next); + } } free(entry); } diff --git a/src/main.c b/src/main.c index d673cba..2a7d76f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,30 @@ #include +#include "taccount.h" +#include "account.h" +#include "subaccount.h" + int main(){ + TAccount *t1 = initializeTAccount(); + SubAccount *chainsaw = initializeSubAccount("Chainsaw Sales", "Annual revenue of hte sale of chainsaws"); + Entry *q1Chainsaw = initializeEntry("Q1 Chainsaw Sales","Sales figures for Q1 of sales of chainsaws"); + setEntryValue(q1Chainsaw, initalizeMoney(1500000,99)); + SubAccount *chaps = initializeSubAccount("Chaps Sales", "Annual revenue of hte sale of chaps"); + Entry *q1Chaps= initializeEntry("Q1 Chap Sales","Sales figures for Q1 of sales of chaps"); + setEntryValue(q1Chaps, initalizeMoney(1500,99)); + Entry *q2Chaps= initializeEntry("Q2 Chap Sales","Sales figures for Q2 of sales of chaps"); + setEntryValue(q2Chaps, initalizeMoney(1500,99)); + addSubAccountToAccount(t1->credits,chainsaw); + addEntryToSubAccount(chainsaw,q1Chainsaw); + addSubAccountToAccount(t1->credits,chaps); + addEntryToSubAccount(chaps,q1Chaps); + addEntryToSubAccount(chaps,q2Chaps); + + destroyTAccount(t1); printf("Contarius is Being Made (SomeHow)...\n"); + return 0; } \ No newline at end of file diff --git a/src/subaccount.c b/src/subaccount.c index 32fbaa4..6e6a005 100644 --- a/src/subaccount.c +++ b/src/subaccount.c @@ -97,21 +97,25 @@ void appendSubAccount(SubAccount *subaccount, SubAccount *newSubaccount) { * */ void destroySubAccount(SubAccount *subaccount){ - + if (subaccount == NULL){ + return; + } if (subaccount->value != NULL){ destroyMoney(subaccount->value); subaccount->value = NULL; } - + destroyEntry(subaccount->entries); SubAccount *current = subaccount->next; - - if (current->next == NULL){ - destroyMoney(current->value); - destroyEntry(current->entries); - free(current); - }else{ - destroySubAccount(current->next); + if (current != NULL) { + if (current->next == NULL) { + destroyMoney(current->value); + destroyEntry(current->entries); + free(current); + } else { + destroySubAccount(current->next); + } } + free(subaccount); diff --git a/src/taccount.c b/src/taccount.c index 20d6b52..0bef471 100644 --- a/src/taccount.c +++ b/src/taccount.c @@ -39,4 +39,4 @@ void destroyTAccount(TAccount *taccount){ destroyAccount(taccount->debits); } free(taccount); -} \ No newline at end of file +}