From 10bfa30b93a896c8c4a565ef3b9984c76ab4b592 Mon Sep 17 00:00:00 2001 From: electrodude Date: Mon, 23 May 2016 13:52:26 -0400 Subject: [PATCH] Fixed settings file leak and invalid io.close (#136) Before, init.lua called io.open on pipeworks.worldpath..'/pipeworks_settings.txt' to see if it existed, but did not close the resulting file handle if it was found to exist. It instead erroneously called io.close() with no argument, which does nothing if the default output file is set to stdout, which it is. Now, the result of io.open is saved to a local variable. If that value is not nil (i.e. if the world settings file exists), the file handle is passed to io.close before calling dofile. Also, this saves pipeworks.worldpath..'/pipeworks_settings.txt' to a local variable to reduce redundancy. --- init.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 92ce02c..8593fdb 100644 --- a/init.lua +++ b/init.lua @@ -16,9 +16,11 @@ pipeworks.modpath = minetest.get_modpath("pipeworks") dofile(pipeworks.modpath.."/default_settings.txt") -- Read the external config file if it exists. -if io.open(pipeworks.worldpath.."/pipeworks_settings.txt","r") then - dofile(pipeworks.worldpath.."/pipeworks_settings.txt") - io.close() +local worldsettingspath = pipeworks.worldpath.."/pipeworks_settings.txt","r" +local worldsettingsfile = io.open(worldsettingspath) +if worldsettingsfile then + worldsettingsfile:close() + dofile(worldsettingspath) end -- Random variables