Fixed some segfaults due not checking for null pointers

This commit is contained in:
Gustavo Henrique Santos Souza de Miranda 2024-04-28 23:38:34 -03:00
parent a5e4be6a09
commit 13880cca63
10 changed files with 73 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -18,5 +18,7 @@ typedef struct taccount {
} TAccount;
TAccount *initializeTAccount();
void destroyTAccount(TAccount *taccount);
Account *getListAccountsPointers(TAccount *tAccount);
#endif

View File

@ -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;

View File

@ -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){
if (current != NULL) {
if (current->next == NULL) {
destroyMoney(current->value);
free(current);
}else{
} else {
destroyEntry(current->next);
}
}
free(entry);
}

View File

@ -1,9 +1,30 @@
#include <stdio.h>
#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;
}

View File

@ -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){
if (current != NULL) {
if (current->next == NULL) {
destroyMoney(current->value);
destroyEntry(current->entries);
free(current);
}else{
} else {
destroySubAccount(current->next);
}
}
free(subaccount);