Fix cell increment error in RESIZE
[pforth] / csrc / pf_inner.c
index 7b2da55..15f764f 100644 (file)
@@ -1397,12 +1397,13 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql ));
 /* Resize memory allocated by ALLOCATE. */\r
                case ID_RESIZE:  /* ( addr1 u -- addr2 result ) */\r
                        {\r
 /* Resize memory allocated by ALLOCATE. */\r
                case ID_RESIZE:  /* ( addr1 u -- addr2 result ) */\r
                        {\r
-                               cell *FreePtr;\r
-                               \r
-                               FreePtr = (cell *) ( M_POP - sizeof(cell) );\r
+                               cell *Addr1 = (cell *) M_POP;\r
+                               // Point to validator below users address.\r
+                               cell *FreePtr = Addr1 - 1;\r
                                if( ((uint32)*FreePtr) != ((uint32)FreePtr ^ PF_MEMORY_VALIDATOR))\r
                                {\r
                                if( ((uint32)*FreePtr) != ((uint32)FreePtr ^ PF_MEMORY_VALIDATOR))\r
                                {\r
-                                       M_PUSH( 0 );\r
+                                       // 090218 - Fixed bug, was returning zero.\r
+                                       M_PUSH( Addr1 );\r
                                        TOS = -3;\r
                                }\r
                                else\r
                                        TOS = -3;\r
                                }\r
                                else\r
@@ -1414,15 +1415,18 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql ));
                                                /* Copy memory including validation. */\r
                                                pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell) );\r
                                                *CellPtr = (cell)(((uint32)CellPtr) ^ (uint32)PF_MEMORY_VALIDATOR);\r
                                                /* Copy memory including validation. */\r
                                                pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell) );\r
                                                *CellPtr = (cell)(((uint32)CellPtr) ^ (uint32)PF_MEMORY_VALIDATOR);\r
-                                               CellPtr++;\r
-                                   M_PUSH( (cell) ++CellPtr );\r
-                                               TOS = 0;\r
+                                               // 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub.\r
+                                               // Increment past validator to user address.\r
+                                   M_PUSH( (cell) (CellPtr + 1) );\r
+                                               TOS = 0; // Result code.\r
+                                               // Mark old cell as dead so we can't free it twice.\r
                                                FreePtr[0] = 0xDeadBeef;\r
                                                pfFreeMem((char *) FreePtr);\r
                                        }\r
                                        else\r
                                        {\r
                                                FreePtr[0] = 0xDeadBeef;\r
                                                pfFreeMem((char *) FreePtr);\r
                                        }\r
                                        else\r
                                        {\r
-                                               M_PUSH( 0 );\r
+                                               // 090218 - Fixed bug, was returning zero.\r
+                                               M_PUSH( Addr1 );\r
                                                TOS = -4;  /* FIXME Fix error code. */\r
                                        }\r
                                }\r
                                                TOS = -4;  /* FIXME Fix error code. */\r
                                        }\r
                                }\r