Overlapping Encoder Slices
ID |
go.overlapping_encode_slices |
Severity |
high |
Remediation Complexity |
simple |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
correctness, reliability |
Description
Reports an encoder call Encode(dst, src) where dst and src are the same expression, so the
destination and source share memory. Applies to the encoders in encoding/hex,
encoding/ascii85, encoding/base32 and encoding/base64.
Rationale
These encoders write more than one output byte per input byte. When the destination and source reference the same memory, the encoder overwrites source bytes before it has finished reading them, producing corrupted output. The destination must be a distinct buffer.
func fn() {
a := make([]byte, 8)
hex.Encode(a, a) // FLAW — overlapping dst and src corrupts the output
}
Remediation
Pass a separate destination buffer, sized for the encoded length, instead of reusing the source slice.