Skip to content

Commit

Permalink
Respect number of bytes returned by value decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
soundmonster committed Dec 17, 2024
1 parent 84240e7 commit 0e950d9
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions internal/producer/MessageSerializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,19 @@ func decodeBytes(data []byte, encoding string) ([]byte, error) {
switch encoding {
case HEX:
out = make([]byte, hex.DecodedLen(len(data)))
if _, err := hex.Decode(out, data); err != nil {
bytelen, err := hex.Decode(out, data)
if err != nil {
return nil, err
}
return out, nil
return out[:bytelen], nil
case BASE64:
out = make([]byte, base64.StdEncoding.DecodedLen(len(data)))
if _, err := base64.StdEncoding.Decode(out, data); err != nil {
bytelen, err := base64.StdEncoding.Decode(out, data)
if err != nil {
return nil, err
}
return out[:clen(out)], nil
return out[:bytelen], nil
default:
return data, nil
}
}

// https://stackoverflow.com/a/27834860/12143351
func clen(n []byte) int {
for i := 0; i < len(n); i++ {
if n[i] == 0 {
return i
}
}
return len(n)
}

0 comments on commit 0e950d9

Please sign in to comment.