shithub: rgbds

Download patch

ref: e4f5df1306c4eb09de57728f9756b951b41565aa
parent: 71d8aeb4c276d29764bf0f76879a2b6047c85803
parent: dc62d60e9b3ff28667e4d6652c7d51491ff8e918
author: Eldred Habert <eldredhabert0@gmail.com>
date: Mon Oct 12 08:26:44 EDT 2020

Merge pull request #603 from NieDzejkob/rpn-realloc

reserveSpace: don't assume one doubling is enough

--- a/src/asm/rpn.c
+++ b/src/asm/rpn.c
@@ -46,17 +46,19 @@
 		/* If there isn't enough room to reserve the space, realloc */
 		if (!expr->tRPN)
 			expr->nRPNCapacity = 256; /* Initial size */
-		else if (expr->nRPNCapacity >= MAXRPNLEN)
-			/*
-			 * To avoid generating humongous object files, cap the
-			 * size of RPN expressions
-			 */
-			fatalerror("RPN expression cannot grow larger than "
-				   EXPAND_AND_STR(MAXRPNLEN) " bytes\n");
-		else if (expr->nRPNCapacity > MAXRPNLEN / 2)
-			expr->nRPNCapacity = MAXRPNLEN;
-		else
-			expr->nRPNCapacity *= 2;
+		while (expr->nRPNCapacity - expr->nRPNLength < size) {
+			if (expr->nRPNCapacity >= MAXRPNLEN)
+				/*
+				 * To avoid generating humongous object files, cap the
+				 * size of RPN expressions
+				 */
+				fatalerror("RPN expression cannot grow larger than "
+					   EXPAND_AND_STR(MAXRPNLEN) " bytes\n");
+			else if (expr->nRPNCapacity > MAXRPNLEN / 2)
+				expr->nRPNCapacity = MAXRPNLEN;
+			else
+				expr->nRPNCapacity *= 2;
+		}
 		expr->tRPN = realloc(expr->tRPN, expr->nRPNCapacity);
 
 		if (!expr->tRPN)
--- a/test/asm/long-rpn-expression.asm
+++ b/test/asm/long-rpn-expression.asm
@@ -27,3 +27,10 @@
        X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X
 
 x: db 0
+
+; this tests long RPN expressions being used as the RHS, as this once triggered
+; a realloc bug
+    db 1+(x+X)
+
+; likewise, a long symbol could result in an insufficient *initial* allocation
+    db A00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000+0+0