Add few checks to prevent game crashing if netgame archive becomes corrupted
https://github.com/alufolie91/saturn-32p/commit/1e47728ea8c60a1f04130170ddfab9b02e1a7e4c#diff-0c6396d5a9ad45f461741cd44ffb2c0d3cf0eb2ec7b0500053005196fe53f1a6R1517
This commit is contained in:
1 parent
7f1217b2ab
commit
d0c50c49a7
1 file changed
+39
-37
+39
-37
@@ -1021,6 +1021,9 @@ static UINT8 UnArchiveValue(UINT8 **p, int TABLESINDEX)
|
||||
break;
|
||||
case ARCH_TEND:
|
||||
return 1;
|
||||
default:
|
||||
CONS_Alert(CONS_ERROR, "Unknown value type unarchived, save is corrupted!\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1199,37 +1202,37 @@ static void UnArchiveTables(UINT8 **p, boolean network)
|
||||
|
||||
n = (UINT16)lua_objlen(gL, TABLESINDEX);
|
||||
|
||||
if (network)
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
for (i = 1; i <= n; i++)
|
||||
lua_rawgeti(gL, TABLESINDEX, i);
|
||||
|
||||
if (!lua_istable(gL, -1))
|
||||
{
|
||||
lua_rawgeti(gL, TABLESINDEX, i);
|
||||
while (true)
|
||||
CONS_Alert(CONS_ERROR, "Value in tables list #%d is not a table! (corrupted save?)\n", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
UINT8 ret;
|
||||
|
||||
if (network)
|
||||
{
|
||||
if (UnArchiveValue(p, TABLESINDEX) == 1) // read key
|
||||
break;
|
||||
if (UnArchiveValue(p, TABLESINDEX) == 2) // read value
|
||||
n++;
|
||||
if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved)
|
||||
{
|
||||
CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i);
|
||||
lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors
|
||||
}
|
||||
else
|
||||
lua_rawset(gL, -3);
|
||||
}
|
||||
lua_pop(gL, 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
lua_rawgeti(gL, TABLESINDEX, i);
|
||||
while (true)
|
||||
{
|
||||
UINT8 ret;
|
||||
|
||||
ret = UnArchiveValue(p, TABLESINDEX);
|
||||
if (ret == 1)
|
||||
{
|
||||
CONS_Alert(CONS_ERROR, "Unexpected end of save reached (Corrupted save?)\n");
|
||||
lua_pop(gL, 1); // Pop key
|
||||
break;
|
||||
}
|
||||
else if (ret == 2) // read value
|
||||
n++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = UnArchiveValueDemo(p, TABLESINDEX, NULL);
|
||||
if (ret == 3)
|
||||
lua_pushnil(gL);
|
||||
@@ -1237,24 +1240,23 @@ static void UnArchiveTables(UINT8 **p, boolean network)
|
||||
break;
|
||||
|
||||
ret = UnArchiveValueDemo(p, TABLESINDEX, NULL);
|
||||
|
||||
if (ret == 3)
|
||||
lua_pushnil(gL);
|
||||
else if (ret == 2) // read value
|
||||
n++;
|
||||
|
||||
if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved)
|
||||
{
|
||||
CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i);
|
||||
lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors
|
||||
}
|
||||
else
|
||||
lua_rawset(gL, -3);
|
||||
}
|
||||
lua_pop(gL, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (lua_isnil(gL, -2)) // if key is nil (if a function etc was accidentally saved)
|
||||
{
|
||||
CONS_Alert(CONS_ERROR, "A nil key in table %d was found! (Invalid key type or corrupted save?)\n", i);
|
||||
lua_pop(gL, 2); // pop key and value instead of setting them in the table, to prevent Lua panic errors
|
||||
}
|
||||
else
|
||||
lua_rawset(gL, -3);
|
||||
}
|
||||
|
||||
lua_pop(gL, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void LUA_Step(void)
|
||||
|
||||
Reference in new issue
Block a user