diff --git a/src/core/org/luaj/vm2/LuaTable.java b/src/core/org/luaj/vm2/LuaTable.java index c2148b84..054d953d 100644 --- a/src/core/org/luaj/vm2/LuaTable.java +++ b/src/core/org/luaj/vm2/LuaTable.java @@ -898,6 +898,11 @@ public class LuaTable extends LuaValue implements Metatable { /** Unpack the elements from i to j inclusive */ public Varargs unpack(int i, int j) { + if (j < i) return NONE; + int count = j - i; + if (count < 0) throw new LuaError("too many results to unpack: greater " + Integer.MAX_VALUE); // integer overflow + int max = 0x00ffffff; + if (count >= max) throw new LuaError("too many results to unpack: " + count + " (max is " + max + ')'); int n = j + 1 - i; switch (n) { case 0: return NONE; @@ -906,10 +911,14 @@ public class LuaTable extends LuaValue implements Metatable { default: if (n < 0) return NONE; - LuaValue[] v = new LuaValue[n]; - while (--n >= 0) - v[n] = get(i+n); - return varargsOf(v); + try { + LuaValue[] v = new LuaValue[n]; + while (--n >= 0) + v[n] = get(i+n); + return varargsOf(v); + } catch (OutOfMemoryError e) { + throw new LuaError("too many results to unpack [out of memory]: " + n); + } } }