bluele / gcache

An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

panic happens in v0.0.2

zhou-xingxing opened this issue · comments

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

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

do you mind to show the code how you use

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)

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

@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.