Open LukasGelbmann opened 5 years ago
Can you confirm which version you are using? Because we saw a problem related to this before, and wrote TestByteOverwrite to diagnose and fix it in commit 9b0c08d0a258cc23ff5d51ff91c029d7532f5d7c.
The design is that the returned structures point to the data in the underlying buffer. If that does not work for you, you'll need to copy the bytes before you feed them to protobuf.Decode.
Thank you for your response.
As you mentioned, this behaviour is by design. I would argue that a slightly different design is better, with the bytes being copied into a new array (as is already done for non-byte slices). This is for two reasons:
This behavior has caused many headaches for me and my friends working on the DSE Homeworks and it took a long time to track down to exactly this issue - because of the memory aliasing some byte[] that I assumed are immutable become changed unexpectedly much later.
Even if this is by design, it would be good to emphasize this somewhere in the documentation (as currently I didn't find any information about it), because it's not intuitive as @LukasGelbmann mentioned.
If you decode a struct which contains a slice of bytes, the returned slice shares the same underlying array with the buffer passed to
Decode()
.The consequences are:
I think these consequences aren't intended. It seems better for
Decode()
to return a copy of the slice of bytes.