Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix Unicode string null termination in StringToBytes #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
rkttu merged 3 commits into master from copilot/fix-queue-label-issue
Nov 30, 2025
Merged
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
5 changes: 3 additions & 2 deletions Experimental.System.Messaging/Messaging/Message.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2416,10 +2416,11 @@ internal static string StringFromBytes(byte[] bytes, int len)
/// <internalonly/>
internal static byte[] StringToBytes(string value)
{
int size = value.Length * 2 + 1;
int size = value.Length * 2 + 2;
byte[] byteBuffer = new byte[size];
byteBuffer[size - 1] = 0;
Encoding.Unicode.GetBytes(value.ToCharArray(), 0, value.Length, byteBuffer, 0);
byteBuffer[size - 2] = 0;
byteBuffer[size - 1] = 0;
Comment on lines +2422 to +2423

Copilot AI Nov 30, 2025

Copy link

Choose a reason for hiding this comment

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

The explicit setting of the last two bytes to 0 is redundant since byte arrays in C# are zero-initialized by default. While this doesn't affect correctness, it can be simplified:

internal static byte[] StringToBytes(string value)
{
 int size = value.Length * 2 + 2;
 byte[] byteBuffer = new byte[size];
 Encoding.Unicode.GetBytes(value.ToCharArray(), 0, value.Length, byteBuffer, 0);
 // Last 2 bytes are already 0 by default
 return byteBuffer;
}

Alternatively, if you want to make the null terminator explicit for clarity, consider adding a comment explaining why.

Suggested change
byteBuffer[size - 2] = 0;
byteBuffer[size - 1] = 0;
// Last 2 bytes are already 0 by default (null terminator for Unicode string)

Copilot uses AI. Check for mistakes.
return byteBuffer;
Comment on lines 2417 to 2424

Copilot AI Nov 30, 2025

Copy link

Choose a reason for hiding this comment

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

The StringToBytes method lacks test coverage for this critical bug fix. Consider adding unit tests to verify:

  1. Correct 2-byte null termination for Unicode strings
  2. Empty string handling
  3. Strings with special characters
  4. Round-trip consistency with StringFromBytes

This is especially important since this bug affected multiple user-facing APIs (MessageQueue.Label, Message.Label, etc.) and caused data persistence issues with MSMQ.

Copilot uses AI. Check for mistakes.
}

Expand Down
7 changes: 7 additions & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ This package is a counterfeit of the .NET Framework version System.Messaging ass

The source code for this package is excerpted from the .NET Framework reference source code.

## Disclaimer

This source code is based on Microsoft's Reference Source, but may contain differences from the original. Bug fixes have been applied in the public interest based on community-reported issues. If Microsoft officially releases an MSMQ client package for .NET Core/.NET 5+, that official package should take priority over this experimental package. Users are encouraged to migrate to the official package when it becomes available.

## Release Note

### v1.2.0 (2025年11月30日)
Fixed Unicode string null termination in `StringToBytes` method. This bug affected `MessageQueue.Label`, `MessageQueue.MulticastAddress`, `Message.Label`, `Message.AuthenticationProviderName`, and queue format name properties, causing them not to persist correctly to MSMQ.

### v1.1.0 (2019年11月06日)
Trustee class namespace was moved from System.Messaging to Experimental.System.Messaging. It could break your existing build process.

Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /