Fixed some segfaults due not checking for null pointers
This commit is contained in:
parent
a5e4be6a09
commit
13880cca63
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -18,5 +18,7 @@ typedef struct taccount {
|
|||
|
||||
} TAccount;
|
||||
|
||||
|
||||
TAccount *initializeTAccount();
|
||||
void destroyTAccount(TAccount *taccount);
|
||||
Account *getListAccountsPointers(TAccount *tAccount);
|
||||
#endif
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
17
src/entry.c
17
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);
|
||||
}
|
||||
|
|
|
|||
21
src/main.c
21
src/main.c
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -39,4 +39,4 @@ void destroyTAccount(TAccount *taccount){
|
|||
destroyAccount(taccount->debits);
|
||||
}
|
||||
free(taccount);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue