Bugfix out of bounds file mode

This commit is contained in:
Alessandro Proto 2023-02-12 12:44:45 +01:00
parent 963d47afb2
commit b477e74141

View file

@ -91,20 +91,26 @@ public class FileHandle : IPlugin
private static char CheckMode(string mode) private static char CheckMode(string mode)
{ {
var modes = "nlLa"; var modes = "nlLa";
var i = modes.IndexOf(mode.TrimStart('*')[0]); mode = mode.TrimStart('*');
if(string.IsNullOrEmpty(mode)) {
return '\0';
}
var i = modes.IndexOf(mode[0]);
if(i == -1)
return '\0';
return modes[i]; return modes[i];
} }
private static Stream ToStream(Lua L, bool gc = false) private static Stream ToStream(Lua L, bool gc = false)
{ {
return ObjectManager.ToObject<Stream>(L, 1, gc); return ObjectManager.ToObject<Stream>(L, 1, gc);
//return L.CheckObject<Stream>(1, ObjectType, gc);
} }
private static Stream CheckStream(Lua L, bool gc = false) private static Stream CheckStream(Lua L, bool gc = false)
{ {
var obj = ObjectManager.CheckObject<Stream>(L, 1, ObjectType, gc); var obj = ObjectManager.CheckObject<Stream>(L, 1, ObjectType, gc);
//var obj = L.CheckObject<Stream>(1, ObjectType, gc);
if (obj is null) if (obj is null)
{ {
L.Error("attempt to use a closed file"); L.Error("attempt to use a closed file");
@ -160,8 +166,6 @@ public class FileHandle : IPlugin
private static int L_Read(IntPtr state) private static int L_Read(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
bool success = false;
var stream = CheckStream(L); var stream = CheckStream(L);
if (!stream.CanRead) if (!stream.CanRead)
@ -177,6 +181,7 @@ public class FileHandle : IPlugin
L.PushString("l"); L.PushString("l");
} }
bool success;
if (L.Type(2) == LuaType.Number) if (L.Type(2) == LuaType.Number)
{ {
success = ReadChars(L, stream, (int)L.ToNumber(2)); success = ReadChars(L, stream, (int)L.ToNumber(2));