From aa156fb9330d739558c40a5f3f9a3911781a3fea Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Wed, 25 Jul 2012 18:13:09 +0100 Subject: [PATCH] TPT: Fix infinite loop when flood_INST finds some INST with a non zero lif... 9ee316c3e9 --- src/simulation/Simulation.cpp | 38 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 5a40c82e6..17bbc9bc2 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -369,7 +369,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) int x1, x2, dy = (c>8].life!=0) return 1; + coord_stack = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit); coord_stack[coord_stack_size][0] = x; coord_stack[coord_stack_size][1] = y; coord_stack_size++; @@ -404,7 +405,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) // go left as far as possible while (x1>=CELL) { - if ((pmap[y][x1-1]&0xFF)!=cm) + if ((pmap[y][x1-1]&0xFF)!=cm || parts[pmap[y][x1-1]>>8].life!=0) { break; } @@ -413,7 +414,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) // go right as far as possible while (x2>8].life!=0) { break; } @@ -433,20 +434,23 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1+1], cm)) { // travelling vertically up, skipping a horizontal line - if ((pmap[y-2][x1]&0xFF)==cm) + if ((pmap[y-2][x1]&0xFF)==cm && !parts[pmap[y-2][x1]>>8].life) { coord_stack[coord_stack_size][0] = x1; coord_stack[coord_stack_size][1] = y-2; coord_stack_size++; if (coord_stack_size>=coord_stack_limit) + { + free(coord_stack); return -1; + } } } else if (y>=CELL+1) { for (x=x1; x<=x2; x++) { - if ((pmap[y-1][x]&0xFF)==cm) + if ((pmap[y-1][x]&0xFF)==cm && !parts[pmap[y-1][x]>>8].life) { if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm)) { @@ -455,7 +459,10 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) coord_stack[coord_stack_size][1] = y-1; coord_stack_size++; if (coord_stack_size>=coord_stack_limit) + { + free(coord_stack); return -1; + } } } } @@ -466,20 +473,23 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1+1], cm)) { // travelling vertically down, skipping a horizontal line - if ((pmap[y+2][x1]&0xFF)==cm) + if ((pmap[y+2][x1]&0xFF)==cm && !parts[pmap[y+2][x1]>>8].life) { coord_stack[coord_stack_size][0] = x1; coord_stack[coord_stack_size][1] = y+2; coord_stack_size++; if (coord_stack_size>=coord_stack_limit) + { + free(coord_stack); return -1; + } } } else if (y>8].life) { if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm)) { @@ -488,7 +498,10 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) coord_stack[coord_stack_size][1] = y+1; coord_stack_size++; if (coord_stack_size>=coord_stack_limit) + { + free(coord_stack); return -1; + } } } @@ -506,7 +519,7 @@ int Simulation::FloodParts(int x, int y, int fullc, int cm, int bm, int flags) int x1, x2, dy = (c=coord_stack_limit) + { + free(coord_stack); return -1; + } } if (y=coord_stack_limit) + { + free(coord_stack); return -1; + } } } while (coord_stack_size>0); free(coord_stack);