Skip to content

Conversation

@Nepomuk5665
Copy link

Summary

Fix a bug where decoded fixed type values share the same underlying array as the input buffer and other decoded values, causing unexpected behavior when appending to these slices.

Problem

This is the same issue as #269 but for the fixed type. PR #270 fixes the bytes type but does not address the fixed type which has the identical problem.

When decoding binary fixed types in makeFixedCodec, the nativeFromBinary function returns buf[:size] directly - a subslice of the input buffer. This means:

  1. All decoded fixed values share the same underlying array as the input
  2. Appending to a decoded slice can overwrite data in other decoded slices
  3. Modifications can corrupt the original input buffer

Solution

Allocate a new slice and copy the data instead of returning a subslice:

result := make([]byte, size)
copy(result, buf[:size])
return result, buf[size:], nil

This ensures each decoded fixed value has its own independent backing array.

Testing

Added TestFixedSlicesUseIndependentBackingArrays which:

  1. Decodes a record with two fixed fields from a binary buffer
  2. Verifies the initial decoded values are correct
  3. Appends to one field and verifies the other field and original buffer are unchanged

All existing tests continue to pass.

Related

When decoding binary fixed types, the returned byte slice was a subslice
of the input buffer. This meant all decoded fixed values shared the same
underlying array, causing unexpected behavior when appending to these
slices - modifications could affect other decoded values or the original
input buffer.

This is the same issue as linkedin#269 but for the fixed type. PR linkedin#270 fixes
the bytes type but does not address the fixed type which has the same
problem.

The fix allocates a new slice and copies the data, ensuring each decoded
fixed value has its own independent backing array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant