From 8c7fe2ae95d5d59f1065033505d9479708b9c5e2 Mon Sep 17 00:00:00 2001 From: Alam Ed Arias Date: Wed, 21 Feb 2024 21:23:38 -0500 Subject: [PATCH] Make tokenizer keep a copy of string in normal memory --- src/doomdef.h | 2 +- src/m_misc.c | 4 ++-- src/m_tokenizer.c | 7 +++++-- src/m_tokenizer.h | 3 ++- src/p_setup.c | 13 ++++++++++++- src/r_translation.c | 8 ++++---- src/w_wad.c | 4 ++-- 7 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index 4c843f9e2..50f6f8a8b 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -552,7 +552,7 @@ void *M_Memcpy(void* dest, const void* src, size_t n); char *va(const char *format, ...) FUNCPRINTF; char *M_GetToken(const char *inputString); void M_UnGetToken(void); -void M_TokenizerOpen(const char *inputString); +void M_TokenizerOpen(const char *inputString, size_t len); void M_TokenizerClose(void); const char *M_TokenizerRead(UINT32 i); const char *M_TokenizerReadZDoom(UINT32 i); diff --git a/src/m_misc.c b/src/m_misc.c index 797aca739..55c5485a1 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -1978,9 +1978,9 @@ void M_UnGetToken(void) static tokenizer_t *globalTokenizer = NULL; -void M_TokenizerOpen(const char *inputString) +void M_TokenizerOpen(const char *inputString, size_t len) { - globalTokenizer = Tokenizer_Open(inputString, 2); + globalTokenizer = Tokenizer_Open(inputString, len, 2); } void M_TokenizerClose(void) diff --git a/src/m_tokenizer.c b/src/m_tokenizer.c index f36f7f6f3..18569b24e 100644 --- a/src/m_tokenizer.c +++ b/src/m_tokenizer.c @@ -12,11 +12,13 @@ #include "m_tokenizer.h" #include "z_zone.h" -tokenizer_t *Tokenizer_Open(const char *inputString, unsigned numTokens) +tokenizer_t *Tokenizer_Open(const char *inputString, size_t len, unsigned numTokens) { tokenizer_t *tokenizer = Z_Malloc(sizeof(tokenizer_t), PU_STATIC, NULL); - tokenizer->input = inputString; + tokenizer->zdup = malloc(len); + + tokenizer->input = M_Memcpy(tokenizer->zdup, inputString, len); tokenizer->startPos = 0; tokenizer->endPos = 0; tokenizer->inputLength = 0; @@ -51,6 +53,7 @@ void Tokenizer_Close(tokenizer_t *tokenizer) Z_Free(tokenizer->token[i]); Z_Free(tokenizer->capacity); Z_Free(tokenizer->token); + free(tokenizer->zdup); Z_Free(tokenizer); } diff --git a/src/m_tokenizer.h b/src/m_tokenizer.h index f51117301..013a310e8 100644 --- a/src/m_tokenizer.h +++ b/src/m_tokenizer.h @@ -16,6 +16,7 @@ typedef struct Tokenizer { + void *zdup; const char *input; unsigned numTokens; UINT32 *capacity; @@ -29,7 +30,7 @@ typedef struct Tokenizer const char *(*get)(struct Tokenizer*, UINT32); } tokenizer_t; -tokenizer_t *Tokenizer_Open(const char *inputString, unsigned numTokens); +tokenizer_t *Tokenizer_Open(const char *inputString, size_t len, unsigned numTokens); void Tokenizer_Close(tokenizer_t *tokenizer); const char *Tokenizer_Read(tokenizer_t *tokenizer, UINT32 i); diff --git a/src/p_setup.c b/src/p_setup.c index a39750003..396639b72 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -1525,6 +1525,12 @@ static boolean TextmapCount(size_t size) numvertexes = 0; numsectors = 0; + if(!tkn) + { + CONS_Alert(CONS_ERROR, "No text in lump!\n"); + return true; + } + // Look for namespace at the beginning. if (!fastcmp(tkn, "namespace")) { @@ -3109,7 +3115,12 @@ static boolean P_LoadMapData(const virtres_t *virt) if (udmf) // Count how many entries for each type we got in textmap. { virtlump_t *textmap = vres_Find(virt, "TEXTMAP"); - M_TokenizerOpen((char *)textmap->data); + if (textmap->size == 0) + { + CONS_Alert(CONS_ERROR, "Emtpy TEXTMAP Lump!\n"); + return false; + } + M_TokenizerOpen((char *)textmap->data, textmap->size); if (!TextmapCount(textmap->size)) { M_TokenizerClose(); diff --git a/src/r_translation.c b/src/r_translation.c index 3b123f14c..390545802 100644 --- a/src/r_translation.c +++ b/src/r_translation.c @@ -784,9 +784,9 @@ static struct PaletteRemapParseResult *PaletteRemap_ParseString(tokenizer_t *sc) return NULL; } -static struct PaletteRemapParseResult *PaletteRemap_ParseTranslation(const char *translation) +static struct PaletteRemapParseResult *PaletteRemap_ParseTranslation(const char *translation, size_t len) { - tokenizer_t *sc = Tokenizer_Open(translation, 1); + tokenizer_t *sc = Tokenizer_Open(translation, len, 1); struct PaletteRemapParseResult *result = PaletteRemap_ParseString(sc); Tokenizer_Close(sc); return result; @@ -918,7 +918,7 @@ void R_ParseTrnslate(INT32 wadNum, UINT16 lumpnum) text[lumpLength] = '\0'; Z_Free(lumpData); - sc = Tokenizer_Open(text, 1); + sc = Tokenizer_Open(text, lumpLength, 1); tkn = sc->get(sc, 0); struct NewTranslation *list = NULL; @@ -963,7 +963,7 @@ void R_ParseTrnslate(INT32 wadNum, UINT16 lumpnum) // Parse all of the translations do { - struct PaletteRemapParseResult *parse_result = PaletteRemap_ParseTranslation(tkn); + struct PaletteRemapParseResult *parse_result = PaletteRemap_ParseTranslation(tkn, strlen(tkn)); if (parse_result->error) { PrintError(name, "%s", parse_result->error); diff --git a/src/w_wad.c b/src/w_wad.c index dbf9d1ad8..0b391f0c4 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -2704,7 +2704,7 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) UINT8 *wadData = W_CacheLumpNum(lumpnum, PU_LEVEL); filelump_t *fileinfo = (filelump_t *)(wadData + ((wadinfo_t *)wadData)->infotableofs); numlumps = ((wadinfo_t *)wadData)->numlumps; - vlumps = Z_Malloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL); + vlumps = Z_Calloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL); // Build the lumps. for (i = 0; i < numlumps; i++) @@ -2728,7 +2728,7 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) break; numlumps++; - vlumps = Z_Malloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL); + vlumps = Z_Calloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL); for (i = 0; i < numlumps; i++, lumpnum++) { vlumps[i].size = W_LumpLength(lumpnum);