From 51ffcbb6664483893472a3cf2502ce6c58e808eb Mon Sep 17 00:00:00 2001 From: bennyboy Date: Sun, 7 Jun 2020 02:32:23 -0400 Subject: [PATCH] Fix for ArrayPool allocating buffers larger than requested --- Maploader/World/SubChunkData.cs | 1 + Maploader/World/World.cs | 31 ++++++++++++++++++------------- leveldb-sharp-std/DB.cs | 11 ++++++----- leveldb-sharp-std/Native.cs | 4 ++-- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Maploader/World/SubChunkData.cs b/Maploader/World/SubChunkData.cs index c354776..34a4fa8 100644 --- a/Maploader/World/SubChunkData.cs +++ b/Maploader/World/SubChunkData.cs @@ -7,6 +7,7 @@ public class SubChunkData { public byte[] Key { get; set; } public byte[] Data { get; set; } + public int DataLength { get; set; } public UInt32 Crc32 { get; set; } public byte Index { get; set; } public bool FoundInDb { get; set; } diff --git a/Maploader/World/World.cs b/Maploader/World/World.cs index e1310a7..89fafa9 100644 --- a/Maploader/World/World.cs +++ b/Maploader/World/World.cs @@ -47,16 +47,17 @@ public ChunkData GetOverworldChunkData(int x, int z) for (byte subChunkIdx = 0; subChunkIdx < 15; subChunkIdx++) { key[9] = subChunkIdx; - - var data = db.Get(key); + UIntPtr length; + var data = db.Get(key, out length); if (data != null) { var subChunkData = new SubChunkData() { Index = subChunkIdx, Data = data, + DataLength = (int)length, Key = key, - Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data) + Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length) }; ret.SubChunks.Add(subChunkData); } @@ -100,8 +101,8 @@ public Chunk GetChunk(int x, int z) for (byte subChunkIdx = 0; subChunkIdx < 15; subChunkIdx++) { key[9] = subChunkIdx; - - var data = db.Get(key); + UIntPtr length; + var data = db.Get(key, out length); if (data != null) { subChunks[subChunkIdx] = data; @@ -461,16 +462,17 @@ public ChunkData GetChunkData(GroupedChunkSubKeys groupedChunkSubKeys) foreach (var kvp in groupedChunkSubKeys.Subchunks) { var key = kvp.Value; - - var data = db.Get(key.Key); + UIntPtr length; + var data = db.Get(key.Key, out length); if (data != null) { var subChunkData = new SubChunkData() { Index = kvp.Key, Data = data, + DataLength = (int)length, Key = kvp.Value.Key, - Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data), + Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length), }; ret.SubChunks.Add(subChunkData); } @@ -499,16 +501,17 @@ public ChunkData GetChunkData(IGrouping groupedChunkSub foreach (var kvp in groupedChunkSubKeys) { var key = kvp; - - var data = db.Get(key.Key); + UIntPtr length; + var data = db.Get(key.Key, out length); if (data != null) { var subChunkData = new SubChunkData() { Index = key.SubChunkId, Data = data, + DataLength = (int)length, Key = key.Key, - // Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data), + // Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length), }; ret.SubChunks.Add(subChunkData); } @@ -534,15 +537,17 @@ public ChunkData GetChunkData(int x, int z) var key = CreateKey(x, z); key[9] = (byte)kvp; - var data = db.Get(key); + UIntPtr length; + var data = db.Get(key, out length); if (data != null) { var subChunkData = new SubChunkData() { Index = (byte)kvp, Data = data, + DataLength = (int)length, Key = key, - Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data), + Crc32 = Force.Crc32.Crc32CAlgorithm.Compute(data, 0, (int)length), }; ret.SubChunks.Add(subChunkData); } diff --git a/leveldb-sharp-std/DB.cs b/leveldb-sharp-std/DB.cs index 56f25a6..1365644 100644 --- a/leveldb-sharp-std/DB.cs +++ b/leveldb-sharp-std/DB.cs @@ -62,7 +62,8 @@ public class DB : IDisposable, IEnumerable> public byte[] this[byte[] key] { get { - return Get(null, key); + UIntPtr unusedLength; + return Get(null, key, out unusedLength); } set { Put(null, key, value); @@ -183,18 +184,18 @@ public void Write(WriteBatch writeBatch) Write(null, writeBatch); } - public byte[] Get(ReadOptions options, byte[] key) + public byte[] Get(ReadOptions options, byte[] key, out UIntPtr length) { CheckDisposed(); if (options == null) { options = new ReadOptions(); } - return Native.leveldb_get(Handle, options.Handle, key); + return Native.leveldb_get(Handle, options.Handle, key, out length); } - public byte[] Get(byte[] key) + public byte[] Get(byte[] key, out UIntPtr length) { - return Get(null, key); + return Get(null, key, out length); } IEnumerator IEnumerable.GetEnumerator() diff --git a/leveldb-sharp-std/Native.cs b/leveldb-sharp-std/Native.cs index aba1e7b..27d4022 100644 --- a/leveldb-sharp-std/Native.cs +++ b/leveldb-sharp-std/Native.cs @@ -196,7 +196,8 @@ public static extern IntPtr leveldb_get(IntPtr db, public static byte[] leveldb_get(IntPtr db, IntPtr readOptions, - byte[] key) + byte[] key, + out UIntPtr valueLength) { @@ -204,7 +205,6 @@ public static byte[] leveldb_get(IntPtr db, { fixed (byte* p = key) { - UIntPtr valueLength; string error; var keyLength = (UIntPtr) key.Length; IntPtr keyPtr = (IntPtr)p;