Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion id3v2frames.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ func decodeUTF16WithBOM(b []byte) (string, error) {

func decodeUTF16(b []byte, bo binary.ByteOrder) (string, error) {
if len(b)%2 != 0 {
return "", errors.New("invalid encoding: expected even number of bytes for UTF-16 encoded text")
// Some encoders append a stray trailing byte (often a null
// terminator); drop it rather than failing.
b = b[:len(b)-1]
}
s := make([]uint16, 0, len(b)/2)
for i := 0; i < len(b); i += 2 {
Expand Down
34 changes: 34 additions & 0 deletions id3v2frames_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tag

import (
"encoding/binary"
"testing"
)

func TestDecodeUTF16OddLength(t *testing.T) {
// "AB" in UTF-16BE with a stray trailing null byte appended by the encoder.
b := []byte{0x00, 0x41, 0x00, 0x42, 0x00}
got, err := decodeUTF16(b, binary.BigEndian)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "AB" {
t.Fatalf("got %q, want %q", got, "AB")
}
}

func TestDecodeTextUTF16OddLength(t *testing.T) {
// encoding byte 0x01 (UTF-16 with BOM), little-endian BOM, "AB", stray null.
b := []byte{0x01, 0xFF, 0xFE, 0x41, 0x00, 0x42, 0x00, 0x00}
got, err := decodeText(b[0], b[1:])
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "AB" {
t.Fatalf("got %q, want %q", got, "AB")
}
}