Skip to content
Open
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
23 changes: 15 additions & 8 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,29 @@ func (dst *Bitmap) UnmarshalJSON(data []byte) (err error) {

// fromHex reads a hexadecimal string and converts it to bitmap, character at index 0 is the most significant
func fromHex(hexString string) (Bitmap, error) {
bytes, err := hex.DecodeString(hexString)
b, err := hex.DecodeString(hexString)

switch {
case err != nil:
return nil, err
case len(bytes) == 0:
case len(b) == 0:
return nil, nil
}

// reverse bytes to maintain bytes significance order (least significant = hexString tail = list head)
for l, r := 0, len(bytes)-1; l < r; l, r = l+1, r-1 {
bytes[l], bytes[r] = bytes[r], bytes[l]
// reverse bytes into little-endian order (least significant = hexString tail = list head)
for l, r := 0, len(b)-1; l < r; l, r = l+1, r-1 {
b[l], b[r] = b[r], b[l]
}

for len(bytes)%8 != 0 {
bytes = append(bytes, 0)
for len(b)%8 != 0 {
b = append(b, 0)
}
return FromBytes(bytes), nil

// Use explicit little-endian decoding instead of unsafe pointer cast
// to produce correct results on big-endian architectures (e.g. s390x).
out := make(Bitmap, len(b)/8)
for i := range out {
out[i] = binary.LittleEndian.Uint64(b[i*8 : (i+1)*8])
}
Comment on lines +191 to +196

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Replacing the FromBytes call with explicit little-endian decoding correctly addresses the endianness issue for the fromHex function. However, as noted in the pull request description, the FromBytes function itself uses an unsafe.Pointer cast that is sensitive to host byte order. If FromBytes is still used elsewhere in the package (for example, in binary deserialization logic), those call sites will remain broken on big-endian architectures like s390x. It is recommended to either update FromBytes to be platform-independent or audit all other usages to ensure they are not intended for portable data formats.

return out, nil
}