From 0709770730481144a924210cd89d7d7a5cac40a3 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Sat, 25 Jan 2020 14:15:45 -0500 Subject: [PATCH] Enabled forgotten cleanup scripts and add logic to catch ?alloc errors --- cc.c | 4 +--- cc.h | 8 +++++--- cc_core.c | 2 ++ cc_reader.c | 6 ++---- cc_strings.c | 2 ++ cc_types.c | 17 ++++++++++++++++- functions/number_pack.c | 5 ++++- makefile | 2 ++ test/test.answers | 8 ++++---- test/test100/cleanup.sh | 1 + test/test100/proof.answer | 2 +- 11 files changed, 40 insertions(+), 17 deletions(-) diff --git a/cc.c b/cc.c index a229d1e..5802781 100644 --- a/cc.c +++ b/cc.c @@ -25,13 +25,11 @@ struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* fi struct token_list* reverse_list(struct token_list* head); void program(); void recursive_output(struct token_list* i, FILE* out); -int match(char* a, char* b); -void file_print(char* s, FILE* f); -char* parse_string(char* string); int main(int argc, char** argv) { hold_string = calloc(MAX_STRING, sizeof(char)); + require(NULL != hold_string, "Impossible Exhustion has occured\n"); int DEBUG = FALSE; FILE* in = stdin; FILE* destination_file = stdout; diff --git a/cc.h b/cc.h index 84ab639..911e0b1 100644 --- a/cc.h +++ b/cc.h @@ -38,11 +38,13 @@ #define ARMV7L 4 -void file_print(char* s, FILE* f); -int match(char* a, char* b); char* copy_string(char* target, char* source); -void reset_hold_string(); int in_set(int c, char* s); +int match(char* a, char* b); +void file_print(char* s, FILE* f); +void require(int bool, char* error); +void reset_hold_string(); + struct type { diff --git a/cc_core.c b/cc_core.c index a9c7581..fc9a36a 100644 --- a/cc_core.c +++ b/cc_core.c @@ -49,6 +49,7 @@ void require(int bool, char* error); struct token_list* emit(char *s, struct token_list* head) { struct token_list* t = calloc(1, sizeof(struct token_list)); + require(NULL != t, "Exhusted memory while generating token to emit\n"); t->next = head; t->s = s; return t; @@ -73,6 +74,7 @@ void uniqueID_out(char* s, char* num) struct token_list* sym_declare(char *s, struct type* t, struct token_list* list) { struct token_list* a = calloc(1, sizeof(struct token_list)); + require(NULL != a, "Exhusted memory while attempting to declare a symbol\n"); a->next = list; a->s = s; a->type = t; diff --git a/cc_reader.c b/cc_reader.c index dbd2a07..9ad4609 100644 --- a/cc_reader.c +++ b/cc_reader.c @@ -17,10 +17,6 @@ #include "cc.h" -/* imported functions */ -int in_set(int c, char* s); -void require(int bool, char* error); - /* Globals */ FILE* input; struct token_list* token; @@ -106,6 +102,7 @@ void reset_hold_string() int get_token(int c) { struct token_list* current = calloc(1, sizeof(struct token_list)); + require(NULL != current, "Exhusted memory while getting token\n"); reset: reset_hold_string(); @@ -173,6 +170,7 @@ reset: /* More efficiently allocate memory for string */ current->s = calloc(string_index + 2, sizeof(char)); + require(NULL != current->s, "Exhusted memory while trying to copy a token\n"); copy_string(current->s, hold_string); current->prev = token; diff --git a/cc_strings.c b/cc_strings.c index 0a7ad42..0a7205c 100644 --- a/cc_strings.c +++ b/cc_strings.c @@ -126,6 +126,7 @@ collect_regular_string_reset: hold_string[string_index] = '"'; hold_string[string_index + 1] = '\n'; char* message = calloc(string_index + 3, sizeof(char)); + require(NULL != message, "Exhusted memory while storing regular string\n"); copy_string(message, hold_string); reset_hold_string(); return message; @@ -163,6 +164,7 @@ collect_weird_string_reset: hold_string[string_index + 4] = '\n'; char* hold = calloc(string_index + 6, sizeof(char)); + require(NULL != hold, "Exhusted available memory while attempting to collect a weird string\n"); copy_string(hold, hold_string); reset_hold_string(); return hold; diff --git a/cc_types.c b/cc_types.c index 08ce16e..4feda01 100644 --- a/cc_types.c +++ b/cc_types.c @@ -31,6 +31,7 @@ void initialize_types() /* Define void */ global_types = calloc(1, sizeof(struct type)); + require(NULL != global_types, "Exhusted memory while intializing VOID\n"); global_types->name = "void"; global_types->is_signed = FALSE; global_types->size = register_size; @@ -38,8 +39,9 @@ void initialize_types() /* void* has the same properties as void */ global_types->indirect = global_types; - /* Define UNis_signed LONG */ + /* Define unsigned LONG */ struct type* a = calloc(1, sizeof(struct type)); + require(NULL != a, "Exhusted memory while intializing SCM\n"); a->name = "SCM"; a->is_signed = FALSE; a->size = register_size; @@ -48,6 +50,7 @@ void initialize_types() /* Define LONG */ struct type* b = calloc(1, sizeof(struct type)); + require(NULL != b, "Exhusted memory while intializing LONG\n"); b->name = "long"; b->is_signed = TRUE; b->size = register_size; @@ -56,6 +59,7 @@ void initialize_types() /* Define UNSIGNED */ struct type* c = calloc(1, sizeof(struct type)); + require(NULL != c, "Exhusted memory while intializing UNSIGNE\n"); c->name = "unsigned"; c->is_signed = FALSE; c->size = register_size; @@ -65,6 +69,7 @@ void initialize_types() /* Define int */ struct type* d = calloc(1, sizeof(struct type)); + require(NULL != d, "Exhusted memory while intializing INT\n"); d->name = "int"; d->is_signed = TRUE; d->size = register_size; @@ -74,6 +79,7 @@ void initialize_types() /* Define char* */ struct type* e = calloc(1, sizeof(struct type)); + require(NULL != e, "Exhusted memory while intializing CHAR*\n"); e->name = "char*"; e->is_signed = FALSE; e->size = register_size; @@ -81,6 +87,7 @@ void initialize_types() /* Define char */ struct type* f = calloc(1, sizeof(struct type)); + require(NULL != f, "Exhusted memory while intializing CHAR\n"); f->name = "char"; f->is_signed = FALSE; f->size = 1; @@ -88,6 +95,7 @@ void initialize_types() /* Define char** */ struct type* g = calloc(1, sizeof(struct type)); + require(NULL != g, "Exhusted memory while intializing CHAR**\n"); g->name = "char**"; g->is_signed = FALSE; g->size = register_size; @@ -100,6 +108,7 @@ void initialize_types() /* Define FILE */ struct type* h = calloc(1, sizeof(struct type)); + require(NULL != h, "Exhusted memory while intializing FILE\n"); h->name = "FILE"; h->is_signed = FALSE; h->size = register_size; @@ -109,6 +118,7 @@ void initialize_types() /* Define FUNCTION */ struct type* i = calloc(1, sizeof(struct type)); + require(NULL != i, "Exhusted memory while intializing FUNCTION\n"); i->name = "FUNCTION"; i->is_signed = FALSE; i->size = register_size; @@ -118,12 +128,14 @@ void initialize_types() /* Primitives mes.c wanted */ struct type* j = calloc(1, sizeof(struct type)); + require(NULL != j, "Exhusted memory while intializing SIZE_T\n"); j->name = "size_t"; j->is_signed = FALSE; j->size = register_size; j->indirect = j; struct type* k = calloc(1, sizeof(struct type)); + require(NULL != k, "Exhusted memory while intializing SSIZE_T\n"); k->name = "ssize_t"; k->is_signed = FALSE; k->size = register_size; @@ -182,6 +194,7 @@ int member_size; struct type* build_member(struct type* last, int offset) { struct type* i = calloc(1, sizeof(struct type)); + require(NULL != i, "Exhusted memory while building a struct member\n"); i->members = last; i->offset = offset; @@ -240,7 +253,9 @@ void create_struct() int offset = 0; member_size = 0; struct type* head = calloc(1, sizeof(struct type)); + require(NULL != head, "Exhusted memory while creating a struct\n"); struct type* i = calloc(1, sizeof(struct type)); + require(NULL != i, "Exhusted memory while creating a struct indirection\n"); head->name = global_token->s; head->type = head; i->name = global_token->s; diff --git a/functions/number_pack.c b/functions/number_pack.c index 5ca16a4..d5b1143 100644 --- a/functions/number_pack.c +++ b/functions/number_pack.c @@ -19,11 +19,13 @@ #include #include // void* calloc(int count, int size); -void file_print(char* s, FILE* f); int hex2char(int c); +void file_print(char* s, FILE* f); +void require(int bool, char* error); char* number_to_hex(int a, int bytes) { + require(bytes > 0, "number to hex must have a positive number of bytes greater than zero\n"); char* result = calloc(1 + (bytes << 1), sizeof(char)); if(NULL == result) { @@ -33,6 +35,7 @@ char* number_to_hex(int a, int bytes) int i = 0; int divisor = (bytes << 3); + require(divisor > 0, "unexpected wrap around in number_to_hex\n"); /* Simply collect numbers until divisor is gone */ while(0 != divisor) diff --git a/makefile b/makefile index b090f34..fbb78b7 100644 --- a/makefile +++ b/makefile @@ -71,6 +71,8 @@ clean: ./test/test22/cleanup.sh ./test/test23/cleanup.sh ./test/test24/cleanup.sh + ./test/test25/cleanup.sh + ./test/test26/cleanup.sh ./test/test99/cleanup.sh ./test/test100/cleanup.sh diff --git a/test/test.answers b/test/test.answers index 79dc4d6..bd542fc 100644 --- a/test/test.answers +++ b/test/test.answers @@ -53,10 +53,10 @@ a0ae067746e7a2b01d33950da1cf640e12c3a70a045ab331ea2025af59dec9af test/results/t 1154f39f25dcd6d914e9a542306f95280926baf985d011b2152c7ea0b87ab42d test/results/test10-knight-native-binary c1b5a2a3cd46c5e95e5540e871c2a916e028684ca80f51c001ef489342e27625 test/results/test10-knight-posix-binary b3e13d54aab689137628fb9c4487bfd8288f9bd18bef8fe756577c8d2dce1f1f test/results/test10-x86-binary -9f8083a7b8a546ed6d88e8b0b6a40d198a71f22940d8b8916a07c784f9c6eff8 test/results/test100-amd64-binary -f4c4ecf1c76bbda3f5bd0ad1e8ebf02a2a65191b082b631e183b47c2f77af905 test/results/test100-armv7l-binary -bc9552bede2942ee73b66d95e226119a0e743dcd89333a53c14fd97b99f7f038 test/results/test100-knight-posix-binary -5c2e4304819ccfa154b5ea732e9eab58efd7ba1dd1baf8cc88a18447c34b9256 test/results/test100-x86-binary +61036eb3bc1b7039b5f6a240fb661728b57b0d16b641fa5f8d2740fe6b6e50e3 test/results/test100-amd64-binary +3987986885bfc3edb542b361b3f8d56f418b197f6974af2dcba320363faf24a6 test/results/test100-armv7l-binary +120233fe5773c21d83c40201cda4c858214593896919f62f42282cb5eb7b86e7 test/results/test100-knight-posix-binary +c7726290b1bd72e519da56e2a82a8fc11da22aecdd144e0498e7f2469c847649 test/results/test100-x86-binary 34e6d535e30ef8826a4ad1a4d08b76cfa370c54595599ad3be784b64c9cd8ec5 test/results/test11-amd64-binary 893695e6f300a0fe055fad935a56abd549bba70d1d39c535a680f41bbb73f117 test/results/test11-armv7l-binary 955b564d2c89abf2cfc6c80d766cd11479d146b828dec69e654b0958a62d5e6e test/results/test11-knight-native-binary diff --git a/test/test100/cleanup.sh b/test/test100/cleanup.sh index a4b1b12..00f96c2 100755 --- a/test/test100/cleanup.sh +++ b/test/test100/cleanup.sh @@ -16,6 +16,7 @@ ## along with M2-Planet. If not, see . rm -f test/test100/cc.M1 +rm -f test/test100/cc-footer.M1 rm -f test/test100/cc.hex2 rm -f test/test100/proof exit 0 diff --git a/test/test100/proof.answer b/test/test100/proof.answer index 492d9c9..b24b456 100644 --- a/test/test100/proof.answer +++ b/test/test100/proof.answer @@ -1 +1 @@ -fd4328489c1e301b162557e6d21a44753690e002c43ba3b5bd942d6cb810d74c test/test100/proof +c2e77ffce6bd293ef6f268626b7807dfbb05f0a8c17e9bd52f12c978a36fc830 test/test100/proof