]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
noise: Throw exception on noise allocation failure
authorkwolekr <kwolekr@minetest.net>
Sat, 29 Nov 2014 21:50:18 +0000 (16:50 -0500)
committerkwolekr <kwolekr@minetest.net>
Sat, 29 Nov 2014 21:52:45 +0000 (16:52 -0500)
src/exceptions.h
src/noise.cpp
src/script/lua_api/l_noise.cpp

index 6d6ad333a442bcb5690fbce59e1edf2a9bb25ec2..abd8c68ae31ee9b0917eb8fd529e6b826a425f16 100644 (file)
@@ -125,6 +125,18 @@ class ClientStateError : public BaseException {
        Some "old-style" interrupts:
 */
 
+class InvalidNoiseParamsException : public BaseException {
+public:
+       InvalidNoiseParamsException():
+               BaseException("One or more noise parameters were invalid or require "
+                       "too much memory")
+       {}
+
+       InvalidNoiseParamsException(const std::string &s):
+               BaseException(s)
+       {}
+};
+
 class InvalidPositionException : public BaseException
 {
 public:
index c0249a4373678075bbe7575f1b12897f8a0d87ce..2d1b8d6240e855f83cb549883180043cc760eb04 100644 (file)
@@ -29,6 +29,7 @@
 #include <string.h> // memset
 #include "debug.h"
 #include "util/numeric.h"
+#include "exceptions.h"
 
 #define NOISE_MAGIC_X    1619
 #define NOISE_MAGIC_Y    31337
@@ -336,8 +337,12 @@ Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz)
        this->noisebuf = NULL;
        resizeNoiseBuf(sz > 1);
 
-       this->buf    = new float[sx * sy * sz];
-       this->result = new float[sx * sy * sz];
+       try {
+               this->buf    = new float[sx * sy * sz];
+               this->result = new float[sx * sy * sz];
+       } catch (std::bad_alloc &e) {
+               throw InvalidNoiseParamsException();
+       }
 }
 
 
@@ -360,8 +365,12 @@ void Noise::setSize(int sx, int sy, int sz)
 
        delete[] buf;
        delete[] result;
-       this->buf    = new float[sx * sy * sz];
-       this->result = new float[sx * sy * sz];
+       try {
+               this->buf    = new float[sx * sy * sz];
+               this->result = new float[sx * sy * sz];
+       } catch (std::bad_alloc &e) {
+               throw InvalidNoiseParamsException();
+       }
 }
 
 
@@ -399,7 +408,11 @@ void Noise::resizeNoiseBuf(bool is3d)
 
        if (noisebuf)
                delete[] noisebuf;
-       noisebuf = new float[nlx * nly * nlz];
+       try {
+               noisebuf = new float[nlx * nly * nlz];
+       } catch (std::bad_alloc &e) {
+               throw InvalidNoiseParamsException();
+       }
 }
 
 
@@ -615,7 +628,7 @@ float *Noise::perlinMap2DModulated(float x, float y, float *persist_map)
 
                f *= 2.0;
        }
-       
+
        delete[] g;
        return result;
 }
index 4f230b76e0074dc084ae27df4852f20df1ad7344..96ed9364363c2ff2dcf16f965aae5fa0ee34b019 100644 (file)
@@ -171,7 +171,7 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
        n->perlinMap2D(p.X, p.Y);
 
        int maplen = n->sx * n->sy;
-       
+
        lua_newtable(L);
        for (int i = 0; i != maplen; i++) {
                float noiseval = n->np->offset + n->np->scale * n->result[i];
@@ -220,7 +220,7 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
 
 
        int maplen = n->sx * n->sy * n->sz;
-       
+
        lua_newtable(L);
        for (int i = 0; i != maplen; i++) {
                float noiseval = n->np->offset + n->np->scale * n->result[i];
@@ -231,7 +231,11 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
 }
 
 LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size) {
-       noise = new Noise(np, seed, size.X, size.Y, size.Z);
+       try {
+               noise = new Noise(np, seed, size.X, size.Y, size.Z);
+       } catch (InvalidNoiseParamsException &e) {
+               throw LuaError(e.what());
+       }
 }
 
 LuaPerlinNoiseMap::~LuaPerlinNoiseMap()