Corruption of bytes when storing in map[*][]byte

152 views
Skip to the first unread message

Alex B

unread,
30 Nov 2015, 15:28:5030/11/2015
to golang-nuts

Does anyone else have trouble when storing byte arrays in a map?

I have noticed this issue when storing ciphertext, as sometimes the ciphertext was unable to be decrypted, or when hashed, it resulted in a different digest when storing as []byte rather than string.

I just ran into this issue again, where cached messages which are sent over websockets were sometimes breaking the client due to invalid UTF8.

Since I changed the map to string values, the problem disappeared again.

Is it bad practice to store maps of []byte, or has anyone else had this problem?

Dave Cheney

unread,
30 Nov 2015, 15:38:4630/11/2015
to golang-nuts
I recommend running your tests with the race detector enabled.

map[T][]byte will give the caller []byte slice values. slices are reference types that share the same backing array so the callers of your map will be able to mutate the contents the map's key points to.


Hope that helps

Dave

James Bardin

unread,
30 Nov 2015, 17:03:3630/11/2015
to golang-nuts
There are also methods that return a []byte, but document that the underlying array may be reused (e.g. https://golang.org/pkg/bufio/#Scanner.Bytes), so you need to copy the contents to a new []byte for storage.
Reply all
Reply to author
Forward
0 new messages