From 0e950d926fa603cc51fb8fadf6b8f5ceabb22409 Mon Sep 17 00:00:00 2001 From: Leo B <404293+soundmonster@users.noreply.github.com> Date: Wed, 18 Dec 2024 00:13:42 +0100 Subject: [PATCH] Respect number of bytes returned by value decoders --- internal/producer/MessageSerializer.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/internal/producer/MessageSerializer.go b/internal/producer/MessageSerializer.go index 51ff436..3051a92 100644 --- a/internal/producer/MessageSerializer.go +++ b/internal/producer/MessageSerializer.go @@ -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) -}