Skip to content

Conversation

@citydirector
Copy link

This PR addresses two critical issues identified during message serialization:

1. Fix VarInt Encoding for Negative Values ([Proto])

  • Issue: ProtoHelper.GetVarIntLength used uint.CreateSaturating which converted negative values to 0, causing IndexOutOfRangeException in VarIntLengths32.
  • Issue: ProtoWriter.EncodeVarInt performed a signed comparison (value < 0x80), causing negative integers (like -1) to be written as a single byte 0xFF, corrupting the protobuf stream (e.g., breaking Base64 image uploads).
  • Fix: Changed to CreateTruncating and forced ulong casting for unsigned comparison to preserve bit patterns.

2. Fix Forward Message Image Upload ([Core])

  • Issue: MultiMsgEntity.Preprocess only uploaded the outer container but failed to upload internal entities (like images). This resulted in empty MsgInfo during serialization and caused -400 Internal Server Error.
  • Fix: Added recursive preprocessing for internal entities using the parent message context.

Regression tests have been added to ensure stability.

Previously, MultiMsgEntity only uploaded the outer container but failed to upload internal entities (like images), causing empty MsgInfo and -400 errors.

This fix ensures all internal entities are recursively preprocessed using the parent message context.
This PR fixes two critical issues related to negative integer handling in VarInt encoding:

1. Fixed `IndexOutOfRangeException` in `ProtoHelper.GetVarIntLength`.
   - Previously, `uint.CreateSaturating` converted negative values to 0, causing `BitOperations.LeadingZeroCount(0)` to return 32, which exceeded the `VarIntLengths32` array bounds.
   - Changed to `CreateTruncating` to preserve the bit pattern.

2. Fixed packet corruption in `ProtoWriter.EncodeVarInt`.
   - The check `if (value < 0x80)` was performing a signed comparison. Negative values (e.g., -1) were treated as single-byte values, breaking the Protobuf MSB rule and corrupting the stream.
   - Fixed by casting to `ulong` for unsigned comparison.

Added regression tests to verify correctness for negative integer serialization.
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