Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] LRU Memory Bug #1511

Closed
ZuoTiJia opened this issue Sep 18, 2023 · 0 comments
Closed

[Bug] LRU Memory Bug #1511

ZuoTiJia opened this issue Sep 18, 2023 · 0 comments
Assignees
Labels
type/bug Something isn't working

Comments

@ZuoTiJia
Copy link
Contributor

ZuoTiJia commented Sep 18, 2023

Describe the bug

The lru cache has a memory bug. When calling get, a reference will be returned, which is essentially memory unsafe.

To Reproduce

#[test]
fn test_shared_cache() {
        #[derive(Debug)]
        struct DropPrint {
            v: Box<u32>
        }

        impl From<u32> for DropPrint {
            fn from(value: u32) -> Self {
                Self {
                    v: value.into()
                }
            }
        }

        impl Drop for DropPrint {
            fn drop(&mut self) {
                println!("drop: {}", self.v);
            }
        }
        let cache: ShardedCache<&str, DropPrint> = ShardedCache::with_capacity(1);
        let one = cache.insert("one", 1.into());
        let two = cache.insert("two", 2.into());
        let three = cache.insert("three", 3.into());
        let four = cache.insert("four", 4.into());
        let five = cache.insert("five", 5.into());
        let six = cache.insert("six", 6.into());
        let seven = cache.insert("seven", 7.into());
        let eight = cache.insert("eight", 8.into());
        let nine = cache.insert("nine", 9.into());
        debug_assert_eq!(1, *one.unwrap().v);
        debug_assert_eq!(2, *two.unwrap().v);
        debug_assert_eq!(3, *three.unwrap().v);
        debug_assert_eq!(4, *four.unwrap().v);
        debug_assert_eq!(5, *five.unwrap().v);
        debug_assert_eq!(6, *six.unwrap().v);
        debug_assert_eq!(7, *seven.unwrap().v);
        debug_assert_eq!(8, *eight.unwrap().v);
        debug_assert_eq!(9, *nine.unwrap().v);
}

Expected behavior

No response

Additional context

[No response]
The reason is that the cache uses this function, which essentially returns an unsafe pointer.

pub fn insert_and_return_value(
&mut self,
k: K,
v: V,
charge: usize,
after_remove: Option<AfterRemovedFnMut<K, V>>,
) -> Option<*const Value<K, V>> {
if self.capacity < charge {
)

@yukkit yukkit added the type/bug Something isn't working label Sep 22, 2023
@ZuoTiJia ZuoTiJia mentioned this issue Oct 12, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants