From aeb21bb4384efbe73fde3a6b7a0528d0341f3bec Mon Sep 17 00:00:00 2001 From: Marrub Date: Fri, 22 Sep 2017 11:37:31 -0400 Subject: [PATCH] Fix several pointer access errors --- .gitignore | 1 + Makefile | 4 +++- src/g_objdef.c | 2 +- src/m_darray.h | 3 +++ src/m_tokbuf.c | 49 +++++++++++++++++++++++++++++-------------------- src/m_tokbuf.h | 1 + src/m_token.c | 6 +----- src/m_types.h | 4 ++++ 8 files changed, 43 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 7319e8c..d4d4938 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ bin doc +ir src_crap data/codedefs data/fonts diff --git a/Makefile b/Makefile index ba04b33..4c3a93e 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,9 @@ SRC=src INC=inc CODEDEFS=data/codedefs BIN=bin -IR=bin/ir +IR=ir + +CFLAGS += -O2 -g GDCC_TARGET=--bc-target=Doominati GDCC_LFLAGS += $(GDCC_TARGET) diff --git a/src/g_objdef.c b/src/g_objdef.c index 767967a..a4ce3f6 100644 --- a/src/g_objdef.c +++ b/src/g_objdef.c @@ -337,7 +337,7 @@ static M_texid G_ObjDef_getFrameSprite(char const *fmt, char frame, int i) if(!bufV) return 0; M_texid id = DGE_Texture_Get(DGE_String_Create(bufV, bufC)); - free(bufV); + M_Vec_clear(buf); return id; } diff --git a/src/m_darray.h b/src/m_darray.h index 90908e1..ec2f005 100644 --- a/src/m_darray.h +++ b/src/m_darray.h @@ -22,4 +22,7 @@ #define M_Vec_next(vec) ((vec##V)[(vec##C)++]) +#define M_Vec_clear(vec) \ + (free((vec##V)), (vec##V) = NULL, (vec##C) = (vec##S) = 0) + #endif diff --git a/src/m_tokbuf.c b/src/m_tokbuf.c index aac7f32..7dd870d 100644 --- a/src/m_tokbuf.c +++ b/src/m_tokbuf.c @@ -19,6 +19,18 @@ static enum M_tkprc M_TokBuf_tokProcess(M_token *tok, void *udata) } } +// +// M_TokBuf_print +// +static void M_TokBuf_print(M_tkbuf *tb) +{ + for(int i = 0; i < tb->tend; i++) + printf("[%i]%i: %p %s%s\n", i, tb->toks[i].type, tb->toks[i].textV, + tb->toks[i].textV, i == tb->tpos ? " <-- cursor is here" : ""); + + printf("---\n"); +} + // Extern Functions ----------------------------------------------------------| // @@ -35,32 +47,36 @@ void M_TokBuf_Ctor(M_tkbuf *tb) // void M_TokBuf_Dtor(M_tkbuf *tb) { - /* TODO if(tb->toks) for(int i = 0; i < tb->bend; i++) - free(tb->toks[i].textV); + M_Vec_clear(tb->toks[i].text); free(tb->toks); - */ } // // M_TokBuf_Get // -[[__optional_args(1)]] M_token *M_TokBuf_Get(M_tkbuf *tb) { if(++tb->tpos < tb->tend) return &tb->toks[tb->tpos]; - /* TODO - for(int i = 0; i < tb->tend - tb->bbeg; i++) - free(tb->toks[i].textV); - */ + // Free beginning of buffer. + for(int i = 0; i < tb->bbeg; i++) { + M_Vec_clear(tb->toks[i].text); + tb->toks[i] = (M_token){0}; + } - memmove(&tb->toks[0], &tb->toks[tb->tend - tb->bbeg], - sizeof(M_token) * tb->bbeg); + // Move end of buffer to beginning. + if(tb->tend) + for(int i = tb->tend - tb->bbeg, j = 0; i < tb->tend; i++, j++) + { + tb->toks[j] = tb->toks[i]; + tb->toks[i] = (M_token){0}; + } + // Get new tokens. for(tb->tpos = tb->tend = tb->bbeg; tb->tend < tb->bend; tb->tend++) { skip: @@ -74,11 +90,6 @@ M_token *M_TokBuf_Get(M_tkbuf *tb) } done: - /* - for(int i = 0; i < tb->tend; i++) - printf("%i: %s%s\n", tb->toks[i].type, tb->toks[i].text, i == tb->tpos ? " <-- cursor is here" : ""); - */ - return &tb->toks[tb->tpos]; } @@ -103,12 +114,10 @@ M_token *M_TokBuf_ReGet(M_tkbuf *tb) // bool M_TokBuf_Drop(M_tkbuf *tb, M_tokty t) { - if(M_TokBuf_Get(tb)->type != t) { - M_TokBuf_UnGet(tb); - return false; - } else { + if(M_TokBuf_Get(tb)->type != t) + {M_TokBuf_UnGet(tb); return false;} + else return true; - } } // EOF diff --git a/src/m_tokbuf.h b/src/m_tokbuf.h index 2a7e1c9..b6d616c 100644 --- a/src/m_tokbuf.h +++ b/src/m_tokbuf.h @@ -6,6 +6,7 @@ // Extern Functions ----------------------------------------------------------| +struct M_tkbuf; void M_TokBuf_Ctor(struct M_tkbuf *tb); void M_TokBuf_Dtor(struct M_tkbuf *tb); M_token *M_TokBuf_Get(struct M_tkbuf *tb); diff --git a/src/m_token.c b/src/m_token.c index 8267424..9ea0853 100644 --- a/src/m_token.c +++ b/src/m_token.c @@ -28,11 +28,7 @@ void M_Tk_Parse(FILE *fp, M_token *tok) { if(!tok) return; - if(tok->textV) { - free(tok->textV); - tok->textV = NULL; - tok->textC = tok->textS = 0; - } + M_Vec_clear(tok->text); if(!fp || feof(fp)) { tok->type = tok_eof; diff --git a/src/m_types.h b/src/m_types.h index cba8cbc..096890d 100644 --- a/src/m_types.h +++ b/src/m_types.h @@ -26,12 +26,16 @@ typedef uint8_t mbyte; // Machine Byte typedef uint16_t hword; // Half Word typedef uint32_t mword; // Machine Word typedef uint64_t dword; // Double Word +#if __GDCC__ typedef uint96_t tword; // Triple Word +#endif typedef int8_t chara; // Character typedef int16_t int16; // Integer (16 bits) typedef int32_t integ; // Integer (Machine) typedef int64_t int64; // Integer (64 bits) +#if __GDCC__ typedef int96_t int96; // Integer (96 bits) +#endif typedef unsigned M_texid; // Texture ID #endif