From f579a12d2ca7dcaed65c3e62ee38cb1bbe759597 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Sat, 14 May 2016 23:52:40 +0100
Subject: [PATCH] Fix up more Lua error messages to be more meaningful (and
 work properly, in some cases)

---
 src/lua_hudlib.c    |  2 +-
 src/lua_mathlib.c   |  4 +++-
 src/lua_mobjlib.c   | 11 ++++++++---
 src/lua_playerlib.c |  7 ++++++-
 4 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c
index 33cc1b3672..ed4ca4e48b 100644
--- a/src/lua_hudlib.c
+++ b/src/lua_hudlib.c
@@ -497,7 +497,7 @@ static int libd_getColormap(lua_State *L)
 	{
 		skinnum = (INT32)luaL_checkinteger(L, 1);
 		if (skinnum < TC_ALLWHITE || skinnum >= MAXSKINS)
-			return luaL_error(L, "argument #1 is out of range");
+			return luaL_error(L, "skin number %d is out of range (%d - %d)", skinnum, TC_ALLWHITE, MAXSKINS-1);
 	}
 	else // skin name
 	{
diff --git a/src/lua_mathlib.c b/src/lua_mathlib.c
index b98c515297..c1e13db9f9 100644
--- a/src/lua_mathlib.c
+++ b/src/lua_mathlib.c
@@ -188,7 +188,9 @@ static int lib_all7emeralds(lua_State *L)
 // Returns both color and frame numbers!
 static int lib_coloropposite(lua_State *L)
 {
-	int colornum = ((int)luaL_checkinteger(L, 1)) % MAXSKINCOLORS;
+	UINT8 colornum = (UINT8)luaL_checkinteger(L, 1);
+	if (colornum >= MAXSKINCOLORS)
+		return luaL_error(L, "skincolor %d out of range (0 - %d).", colornum, MAXSKINCOLORS-1);
 	lua_pushinteger(L, Color_Opposite[colornum*2]); // push color
 	lua_pushinteger(L, Color_Opposite[colornum*2+1]); // push frame
 	return 2;
diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c
index bf4ee0b286..7e1fe578ea 100644
--- a/src/lua_mobjlib.c
+++ b/src/lua_mobjlib.c
@@ -509,8 +509,13 @@ static int mobj_set(lua_State *L)
 		return luaL_error(L, "mobj.skin '%s' not found!", skin);
 	}
 	case mobj_color:
-		mo->color = ((UINT8)luaL_checkinteger(L, 3)) % MAXTRANSLATIONS;
+	{
+		UINT8 newcolor = (UINT8)luaL_checkinteger(L,3);
+		if (newcolor >= MAXTRANSLATIONS)
+			return luaL_error(L, "mobj.color %d out of range (0 - %d).", newcolor, MAXTRANSLATIONS-1);
+		mo->color = newcolor;
 		break;
+	}
 	case mobj_bnext:
 		return NOSETPOS;
 	case mobj_bprev:
@@ -524,8 +529,8 @@ static int mobj_set(lua_State *L)
 	case mobj_type: // yeah sure, we'll let you change the mobj's type.
 	{
 		mobjtype_t newtype = luaL_checkinteger(L, 3);
-		if (newtype > MT_LASTFREESLOT)
-			return luaL_error(L, "mobj.type %u is out of bounds.", newtype);
+		if (newtype >= NUMMOBJTYPES)
+			return luaL_error(L, "mobj.type %d out of range (0 - %d).", newtype, NUMMOBJTYPES-1);
 		mo->type = newtype;
 		mo->info = &mobjinfo[newtype];
 		P_SetScale(mo, mo->scale);
diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c
index 388fe3175f..5d5fe5aad9 100644
--- a/src/lua_playerlib.c
+++ b/src/lua_playerlib.c
@@ -389,7 +389,12 @@ static int player_set(lua_State *L)
 	else if (fastcmp(field,"flashpal"))
 		plr->flashpal = (UINT16)luaL_checkinteger(L, 3);
 	else if (fastcmp(field,"skincolor"))
-		plr->skincolor = ((UINT8)luaL_checkinteger(L, 3)) % MAXSKINCOLORS;
+	{
+		UINT8 newcolor = (UINT8)luaL_checkinteger(L,3);
+		if (newcolor >= MAXSKINCOLORS)
+			return luaL_error(L, "player.skincolor %d out of range (0 - %d).", newcolor, MAXSKINCOLORS-1);
+		plr->skincolor = newcolor;
+	}
 	else if (fastcmp(field,"score"))
 		plr->score = (UINT32)luaL_checkinteger(L, 3);
 	else if (fastcmp(field,"dashspeed"))
-- 
GitLab