From 72d1d96c5c9b8f9f31e21ef4a3d542a27fc6c3f8 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Wed, 3 Dec 2014 18:58:55 +0000 Subject: Always close script linemap file after reading and always dispose of other streams in the script engine even if exceptions are thrown. --- .../ScriptEngine/Shared/CodeTools/Compiler.cs | 49 +++++++++++----------- .../ScriptEngine/Shared/Instance/ScriptInstance.cs | 9 ++-- 2 files changed, 29 insertions(+), 29 deletions(-) (limited to 'OpenSim/Region/ScriptEngine') diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs index 9379f8f..de5f92d 100644 --- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs @@ -663,9 +663,8 @@ namespace SecondLife try { - FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read); - fs.Read(data, 0, data.Length); - fs.Close(); + using (FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read)) + fs.Read(data, 0, data.Length); } catch (Exception) { @@ -680,9 +679,8 @@ namespace SecondLife Byte[] buf = Encoding.ASCII.GetBytes(filetext); - FileStream sfs = File.Create(assembly + ".text"); - sfs.Write(buf, 0, buf.Length); - sfs.Close(); + using (FileStream sfs = File.Create(assembly + ".text")) + sfs.Write(buf, 0, buf.Length); return assembly; } @@ -775,7 +773,6 @@ namespace SecondLife return message; } - private static void WriteMapFile(string filename, Dictionary, KeyValuePair> linemap) { string mapstring = String.Empty; @@ -787,40 +784,42 @@ namespace SecondLife } Byte[] mapbytes = Encoding.ASCII.GetBytes(mapstring); - FileStream mfs = File.Create(filename); - mfs.Write(mapbytes, 0, mapbytes.Length); - mfs.Close(); - } + using (FileStream mfs = File.Create(filename)) + mfs.Write(mapbytes, 0, mapbytes.Length); + } private static Dictionary, KeyValuePair> ReadMapFile(string filename) { Dictionary, KeyValuePair> linemap; try { - StreamReader r = File.OpenText(filename); - linemap = new Dictionary, KeyValuePair>(); - - string line; - while ((line = r.ReadLine()) != null) + using (StreamReader r = File.OpenText(filename)) { - String[] parts = line.Split(new Char[] { ',' }); - int kk = System.Convert.ToInt32(parts[0]); - int kv = System.Convert.ToInt32(parts[1]); - int vk = System.Convert.ToInt32(parts[2]); - int vv = System.Convert.ToInt32(parts[3]); + linemap = new Dictionary, KeyValuePair>(); - KeyValuePair k = new KeyValuePair(kk, kv); - KeyValuePair v = new KeyValuePair(vk, vv); + string line; + while ((line = r.ReadLine()) != null) + { + String[] parts = line.Split(new Char[] { ',' }); + int kk = System.Convert.ToInt32(parts[0]); + int kv = System.Convert.ToInt32(parts[1]); + int vk = System.Convert.ToInt32(parts[2]); + int vv = System.Convert.ToInt32(parts[3]); + + KeyValuePair k = new KeyValuePair(kk, kv); + KeyValuePair v = new KeyValuePair(vk, vv); - linemap[k] = v; + linemap[k] = v; + } } } catch { linemap = new Dictionary, KeyValuePair>(); } + return linemap; } } -} +} \ No newline at end of file diff --git a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs index 9da2168..637c556 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs @@ -1065,10 +1065,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance { try { - FileStream fs = File.Create(Path.Combine(Path.GetDirectoryName(assembly), ItemID.ToString() + ".state")); - Byte[] buf = Util.UTF8NoBomEncoding.GetBytes(xml); - fs.Write(buf, 0, buf.Length); - fs.Close(); + using (FileStream fs = File.Create(Path.Combine(Path.GetDirectoryName(assembly), ItemID.ToString() + ".state"))) + { + Byte[] buf = Util.UTF8NoBomEncoding.GetBytes(xml); + fs.Write(buf, 0, buf.Length); + } } catch(Exception) { -- cgit v1.1