Skip to content

ByteString.ToString is sometimes broken for Unicode encoding #3147

@tintoy

Description

@tintoy

Hi.

Currently, ByteString.ToString(Encoding) calls Encoding.GetString for each underlying segment that comprises the ByteString. Unfortunately, this breaks when you use an encoding such as System.Text.Encoding.Unicode and the underlying segments do not represent entire characters.

Here's a quick repro that works on Akka.NET 1.3.1 (tested on Windows, but it's probably the same everywhere):

[Fact]
public void UnicodeByteString_Failure()
{
    const string expected = "ABC";
    Encoding encoding = Encoding.Unicode;

    byte[] rawData = encoding.GetBytes(expected);

    ByteString data = ByteString.Empty;
    data += ByteString.CopyFrom(rawData, 0, 3); // 1 and a half characters
    data += ByteString.CopyFrom(rawData, 3, 3); // 1 and a half characters
    Assert.Equal(rawData.Length, data.Count);

    string actual = data.ToString(encoding);
    Assert.Equal(expected, actual);
}

Note that if you compact the ByteString before calling ToString then it works fine.

[Fact]
public void UnicodeByteString_Success()
{
    const string expected = "ABC";
    Encoding encoding = Encoding.Unicode;

    byte[] rawData = encoding.GetBytes(expected);

    ByteString data = ByteString.Empty;
    data += ByteString.CopyFrom(rawData, 0, 3);
    data += ByteString.CopyFrom(rawData, 3, 3);
    Assert.Equal(rawData.Length, data.Count);

    data = data.Compact();

    string actual = data.ToString(encoding);
    Assert.Equal(expected, actual);
}

I'd suggest that perhaps ToString should either call Compact or manually assemble a byte array before calling Encoding.GetString.

I'm happy to open a PR for this but wanted to discuss it with you, first, since it technically represents a change in behaviour.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions