diff options
author | snowleo <schneeleo@gmail.com> | 2013-05-03 01:26:51 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2013-05-03 01:26:51 +0200 |
commit | ddb3b13931976a3c63aec9859690ee3ec8cdf672 (patch) | |
tree | fa013a3e085dca0c741cfaf3b1b8707a7985c3a1 | |
parent | c62f762724991f5cd55a4698b9d885d4bb8e42be (diff) | |
download | Essentials-ddb3b13931976a3c63aec9859690ee3ec8cdf672.tar Essentials-ddb3b13931976a3c63aec9859690ee3ec8cdf672.tar.gz Essentials-ddb3b13931976a3c63aec9859690ee3ec8cdf672.tar.lz Essentials-ddb3b13931976a3c63aec9859690ee3ec8cdf672.tar.xz Essentials-ddb3b13931976a3c63aec9859690ee3ec8cdf672.zip |
Don't trust File.length()
-rw-r--r-- | Essentials/src/com/earth2me/essentials/EssentialsConf.java | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index da2a4c6aa..bf651e6ee 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -114,14 +114,26 @@ public class EssentialsConf extends YamlConfiguration final FileInputStream inputStream = new FileInputStream(configFile); try { - final ByteBuffer buffer = ByteBuffer.allocate((int)configFile.length()); + long startSize = configFile.length(); + if (startSize > Integer.MAX_VALUE) { + throw new InvalidConfigurationException("File too big"); + } + ByteBuffer buffer = ByteBuffer.allocate((int)startSize); int length; while ((length = inputStream.read(bytebuffer)) != -1) { + if (length > buffer.remaining()) { + ByteBuffer resize = ByteBuffer.allocate(buffer.capacity()+length-buffer.remaining()); + int resizePosition = buffer.position(); + buffer.rewind(); + resize.put(buffer); + resize.position(resizePosition); + buffer = resize; + } buffer.put(bytebuffer, 0, length); } buffer.rewind(); - final CharBuffer data = CharBuffer.allocate((int)configFile.length()); + final CharBuffer data = CharBuffer.allocate(buffer.capacity()); CharsetDecoder decoder = UTF8.newDecoder(); CoderResult result = decoder.decode(buffer, data, true); if (result.isError()) |