Skip to content

Commit

Permalink
fix bugs in To/FromUTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Feb 4, 2024
1 parent a9bda00 commit d854131
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
6 changes: 2 additions & 4 deletions internal/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,8 @@ func (p *PartitionEntry) SetAttributes(attr PartitionAttributes) {
func (p *PartitionEntry) ReadName(b []byte) (int, error) {
// Find the length of the name.
nameLen := 0
for ; nameLen < pteNameLen; nameLen++ {
if p.data[pteNameOff+nameLen] == 0 {
break
}
for nameLen < pteNameLen && p.data[pteNameOff+nameLen] != 0 {
nameLen++
}

n, err := utf16x.ToUTF8(b, p.data[pteNameOff:pteNameOff+nameLen], binary.LittleEndian)
Expand Down
6 changes: 3 additions & 3 deletions internal/utf16x/utf16x.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ToUTF8(dstUTF8, srcUTF16 []byte, order16 binary.ByteOrder) (int, error) {
r, size := DecodeRune(srcUTF16, order16)
if r == utf8.RuneError {
return n, errInvalidUTF16
} else if utf8.RuneLen(r) > len(dstUTF8) {
} else if utf8.RuneLen(r) > len(dstUTF8[n:]) {
return n, errShortDst
}
srcUTF16 = srcUTF16[size:]
Expand All @@ -55,13 +55,13 @@ func ToUTF8(dstUTF8, srcUTF16 []byte, order16 binary.ByteOrder) (int, error) {
func FromUTF8(dst16, src8 []byte, order16 binary.ByteOrder) (int, error) {
n := 0
for len(src8) > 0 {
if len(dst16) < 2 {
if len(dst16[n:]) < 2 {
return n, errShortDst
}
r1, size := utf8.DecodeRune(src8)
if r1 == utf8.RuneError {
return n, errInvalidUTF8
} else if len(dst16) < 4 && utf16.IsSurrogate(r1) {
} else if len(dst16[n:]) < 4 && utf16.IsSurrogate(r1) {
return n, errShortDst
}
n += EncodeRune(dst16[n:], r1, order16)
Expand Down

0 comments on commit d854131

Please sign in to comment.