From 54b32b19d1764c8344bd057aa110944ec92adb58 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 24 Sep 2018 18:59:10 +0200 Subject: [PATCH] Fix stack overflow in Perlin noise generator. --- src/lol/math/noise/perlin.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lol/math/noise/perlin.h b/src/lol/math/noise/perlin.h index 85c736fc..b53478fd 100644 --- a/src/lol/math/noise/perlin.h +++ b/src/lol/math/noise/perlin.h @@ -65,11 +65,15 @@ public: /* Compute all gradient contributions, for each of the 2^N corners * of the hypercube. */ - for (int i = 0; i < (1 << N); ++i) + for (int i = 0; ; ++i) { /* Accumulate Perlin noise */ ret += multiplier * dot(delta, this->get_gradient(origin)); + /* Avoid buffer overflow below in origin[bit] */ + if (i + 1 == (1 << N)) + break; + /* Don’t use the binary pattern for “i” but use its Gray code * “j” instead, so we know we only have one component to alter * in “origin” and in “delta”. We know which bit was flipped by