From 2bafab723394a741be0876795ec7c658ee28bcab Mon Sep 17 00:00:00 2001 From: mjagdis Date: Fri, 1 Nov 2024 22:17:27 +0000 Subject: Fix access to uninitialized space (#5576) * Fix access to uninitialized space Signed-off-by: Mike Jagdis * Same fix to other noise generate functions Signed-off-by: Mike Jagdis * Skip the last Cell.Move Signed-off-by: Mike Jagdis * Add comments Signed-off-by: Mike Jagdis --------- Signed-off-by: Mike Jagdis --- src/Noise/Noise.cpp | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'src/Noise/Noise.cpp') diff --git a/src/Noise/Noise.cpp b/src/Noise/Noise.cpp index cbdc6bc72..136d124a0 100644 --- a/src/Noise/Noise.cpp +++ b/src/Noise/Noise.cpp @@ -734,20 +734,26 @@ void cCubicNoise::Generate2D( // Calculate query values using Cell: int FromY = 0; - for (int y = 0; y < NumSameY; y++) + for (int y = 0; y < NumSameY;) { int ToY = FromY + SameY[y]; int FromX = 0; int CurFloorY = FloorY[FromY]; - for (int x = 0; x < NumSameX; x++) + for (int x = 0; x < NumSameX;) { int ToX = FromX + SameX[x]; Cell.Generate(FromX, ToX, FromY, ToY); - Cell.Move(FloorX[ToX], CurFloorY); - FromX = ToX; + if (++x < NumSameX) // Call Move() every time except for the last loop iteration + { + Cell.Move(FloorX[ToX], CurFloorY); + FromX = ToX; + } + } + if (++y < NumSameY) // Call Move() every time except for the last loop iteration + { + Cell.Move(FloorX[0], FloorY[ToY]); + FromY = ToY; } - Cell.Move(FloorX[0], FloorY[ToY]); - FromY = ToY; } } @@ -795,28 +801,37 @@ void cCubicNoise::Generate3D( // Calculate query values using Cell: int FromZ = 0; - for (int z = 0; z < NumSameZ; z++) + for (int z = 0; z < NumSameZ;) { int ToZ = FromZ + SameZ[z]; int CurFloorZ = FloorZ[FromZ]; int FromY = 0; - for (int y = 0; y < NumSameY; y++) + for (int y = 0; y < NumSameY;) { int ToY = FromY + SameY[y]; int CurFloorY = FloorY[FromY]; int FromX = 0; - for (int x = 0; x < NumSameX; x++) + for (int x = 0; x < NumSameX;) { int ToX = FromX + SameX[x]; Cell.Generate(FromX, ToX, FromY, ToY, FromZ, ToZ); - Cell.Move(FloorX[ToX], CurFloorY, CurFloorZ); - FromX = ToX; + if (++x < NumSameX) // Call Move() every time except for the last loop iteration + { + Cell.Move(FloorX[ToX], CurFloorY, CurFloorZ); + FromX = ToX; + } + } + if (++y < NumSameY) // Call Move() every time except for the last loop iteration + { + Cell.Move(FloorX[0], FloorY[ToY], CurFloorZ); + FromY = ToY; } - Cell.Move(FloorX[0], FloorY[ToY], CurFloorZ); - FromY = ToY; } // for y - Cell.Move(FloorX[0], FloorY[0], FloorZ[ToZ]); - FromZ = ToZ; + if (++z < NumSameZ) // Call Move() every time except for the last loop iteration + { + Cell.Move(FloorX[0], FloorY[0], FloorZ[ToZ]); + FromZ = ToZ; + } } // for z } -- cgit v1.2.3