bluele / gcache

An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC
MIT License
2.6k stars 271 forks source link

panic happens in v0.0.2 #83

Closed zhou-xingxing closed 2 years ago

zhou-xingxing commented 2 years ago

image This kind of error happens by accident, I don’t know what happened

ByronLiang commented 2 years ago

image This kind of error happens by accident, I don’t know what happened

do you mind to show the code how you use

biblical-text commented 2 years ago

I am quite curious to see how you might trigger this. (We run multithreaded and don't have any problems, so I am wondering how you might create code to cause this problem)

zhou-xingxing commented 2 years ago

Sorry, it is our problem. We used unsafe conversions in the code. The string variable obtained in this way can be modified.

package main

import (
    "fmt"
    "unsafe"
)

func Bytes2String(b []byte) string {
    return *(*string)(unsafe.Pointer(&b))
}
func main() {
    testBytes := []byte{'a', 'b', 'c'}
    //b2s
    testString := Bytes2String(testBytes)
    fmt.Println(testString)
    //modify
    testBytes[0] = 'd'
    fmt.Println(testString)
}
//output
//abc
//dbc

In other words, the value of string was modified for unexpected reasons. Cause the consistency of the keys maintained by the arcItem and arcList structures in the cache to be destroyed

bluele commented 2 years ago

@zhou-xingxing thanks for your investigation. In gcache, it assumes that the given key is immutable. If you find any other related problems, please reopen the issue.