1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 03:00:30 +00:00

Ensure files cannot be closed multiple times

Also fix an NPE if we try to close them twice.

Fixes #230
This commit is contained in:
SquidDev 2019-06-04 21:34:19 +01:00
parent ffa4cc241b
commit d8e1c73d26
6 changed files with 39 additions and 2 deletions

View File

@ -212,6 +212,7 @@ public class BinaryReadableHandle extends HandleGeneric
} }
} }
case 3: // close case 3: // close
checkOpen();
close(); close();
return null; return null;
case 4: // seek case 4: // seek

View File

@ -95,6 +95,7 @@ public class BinaryWritableHandle extends HandleGeneric
return null; return null;
} }
case 2: // close case 2: // close
checkOpen();
close(); close();
return null; return null;
case 3: // seek case 3: // seek

View File

@ -152,6 +152,7 @@ public class EncodedReadableHandle extends HandleGeneric
return null; return null;
} }
case 3: // close case 3: // close
checkOpen();
close(); close();
return null; return null;
default: default:

View File

@ -93,6 +93,7 @@ public class EncodedWritableHandle extends HandleGeneric
return null; return null;
} }
case 3: // close case 3: // close
checkOpen();
close(); close();
return null; return null;
default: default:

View File

@ -37,8 +37,13 @@ public abstract class HandleGeneric implements ILuaObject
protected final void close() protected final void close()
{ {
m_open = false; m_open = false;
IoUtil.closeQuietly( m_closable );
m_closable = null; Closeable closeable = m_closable;
if( closeable != null )
{
IoUtil.closeQuietly( closeable );
m_closable = null;
}
} }
/** /**

View File

@ -54,6 +54,20 @@ describe("The fs library", function()
expect { fs.open("rom/x", "r") }:same { nil, "/rom/x: No such file" } expect { fs.open("rom/x", "r") }:same { nil, "/rom/x: No such file" }
expect { fs.open("x", "r") }:same { nil, "/x: No such file" } expect { fs.open("x", "r") }:same { nil, "/x: No such file" }
end) end)
it("errors when closing twice", function()
local handle = fs.open("rom/startup.lua", "r")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end)
describe("reading in binary mode", function()
it("errors when closing twice", function()
local handle = fs.open("rom/startup.lua", "rb")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end) end)
describe("writing", function() describe("writing", function()
@ -64,6 +78,20 @@ describe("The fs library", function()
it("fails on read-only mounts", function() it("fails on read-only mounts", function()
expect { fs.open("rom/x", "w") }:same { nil, "/rom/x: Access denied" } expect { fs.open("rom/x", "w") }:same { nil, "/rom/x: Access denied" }
end) end)
it("errors when closing twice", function()
local handle = fs.open("test-files/out.txt", "w")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end)
describe("writing in binary mode", function()
it("errors when closing twice", function()
local handle = fs.open("test-files/out.txt", "wb")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end) end)
describe("appending", function() describe("appending", function()