From ae77893021c9c728d1ded0eda78aa13ab419dd1f Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sun, 16 Aug 2020 22:18:34 +0200 Subject: [PATCH] Fix file name reporting As noted in the function's code, this is very error-prone, but will do the job; this needs rewriting due to #491 anyways, so, temporary. --- src/asm/fstack.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 99eb9662..45082ee8 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -309,7 +309,30 @@ char *fstk_DumpToStr(void) char const *fstk_GetFileName(void) { - return contextStack->fileName; + /* FIXME: this is awful, but all callees copy the buffer anyways */ + static char fileName[_MAX_PATH + 1]; + size_t remainingChars = _MAX_PATH + 1; + char *dest = fileName; + char const *src = contextStack->fileName; + +#define append(...) do { \ + int nbChars = snprintf(dest, remainingChars, __VA_ARGS__); \ + \ + if (nbChars >= remainingChars) \ + fatalerror("File stack entry too large"); \ + remainingChars -= nbChars; \ + dest += nbChars; \ +} while (0) + + while (*src && --remainingChars) /* Leave room for terminator */ + *dest++ = *src++; + if (remainingChars && contextStack->macro) + append("::%s", contextStack->macro->name); + for (size_t i = 0; i < contextStack->reptDepth; i++) + append("::REPT~%" PRIu32, contextStack->reptIters[i]); + + *dest = '\0'; + return fileName; } uint32_t fstk_GetLine(void)