shithub: rgbds

Download patch

ref: 476ccc9f6bc0d292f0336132670de0dfacee7aef
parent: 3cc67c48cf89af221291761611269b37d9b22641
author: ISSOtm <[email protected]>
date: Sun Sep 1 22:09:59 EDT 2019

Fix undefined behavior in yyunputstr
Refer to comment at lexer.c:100 for more info

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -97,10 +97,16 @@
 
 	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);
 }