pool
pool copied to clipboard
missing extra bytes
I see this project is not very active, unfortunately. Anyway, we use it in some internal project and I've discovered and fixed a bug more than a year ago. Noticed I haven't upstreamed it, so I'm going to submit a PR in a moment. Here is a context.
The problem arises when using extra
> 0 in Pool::with_capacity<F>(count: usize, mut extra: usize, init: F)
. We see that in PoolInner::entry(&self, idx: usize)
pointer is calculated based on size of Entry<T>
only, not on size_of::<Entry<T>>() + extra
. Essentially, extra
argument is ignored.
My simple fix is the following:
@@ -293,7 +293,7 @@ impl<T> PoolInner<T> {
fn entry(&self, idx: usize) -> &Entry<T> {
unsafe {
debug_assert!(idx < self.count, "invalid index");
- let ptr = self.ptr.offset(idx as isize);
+ let ptr = (self.ptr as *mut u8).offset((idx * self.entry_size) as isize) as *mut Entry<T>;
mem::transmute(ptr)
}
}
Note that self.ptr
is of *mut Entry<T>
and self.entry_size
is defined at https://github.com/carllerche/pool/blob/77eee7c6de50ccb7a6c3770b136e3f9220d1f059/src/lib.rs#L205