Fix undefined behavior in yyunputstr

Refer to comment at lexer.c:100 for more info
This commit is contained in:
ISSOtm
2019-09-02 02:09:59 +02:00
parent 3cc67c48cf
commit 476ccc9f6b

View File

@@ -97,11 +97,17 @@ void yyunputstr(const char *s)
len = strlen(s);
pLexBuffer -= len;
if (pLexBuffer < pLexBufferRealStart)
/*
* It would be undefined behavior to subtract `len` from pLexBuffer and
* potentially have it point outside of pLexBufferRealStart's buffer,
* this is why the check is done this way.
* Refer to https://github.com/rednex/rgbds/pull/411#discussion_r319779797
*/
if (pLexBuffer - pLexBufferRealStart < len)
fatalerror("Buffer safety margin exceeded");
pLexBuffer -= len;
memcpy(pLexBuffer, s, len);
}