Sergio0694 / BinaryPack

The fastest and most memory efficient binary serialization library for .NET Standard 2.1, powered by dynamic IL generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

non-ASCII characters => ArgumentOutOfRangeException: Index and count must refer to a location within the string

cda963 opened this issue · comments

I'm getting the following error when trying to serialize certain objects:

System.ArgumentOutOfRangeException: Index and count must refer to a location within the string. (Parameter 's')
   at System.Text.UTF8Encoding.GetBytes(String s, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex)
   at __IL__BinarySerializer`1_1(String , BinaryWriter& )
   at BinaryPack.BinaryConverter.Serialize[T](T obj)

I am having this exact same issue.

cojocaru, have you had any luck figuring fixing it?

If I had to guess this is probably related to some incorrect handling of characters encoded with longer UTF-8 sequences here. As the disclaimer in the readme says, I'm not really maintaining this library anymore so I don't expect to fix this any time soon, but PRs are always welcome, and I'be happy to review and merge one if anyone wanted to have a look and fix this 😄

BinaryPack is aggressively fast and MessagePack is very well
but if you don't care about speed very much i have a srializer, this is a sample use of the serializer in my library

however the bug is like:

            var x = "Pastilă";
            var ArrayLen = Encoding.UTF8.GetByteCount(x.AsSpan());
            var array = new byte[ArrayLen];
            var StrLen = ArrayLen; //bug
            Encoding.UTF8.GetBytes(x, 0, StrLen, array, 0);

here:

il.EmitLoadLocal(Locals.Write.Length);

"but this bug makes it unusable in production for us :("

@cojocaru-dragos-alexandru just for clarity, allow me to point out that:

  • The readme of this repo explicitly says this library is lo longer maintained, is provided as-is, and isn't tested for production
  • You claim you're using this library in production, yet you or your company aren't sponsoring this library at all. This isn't how you help build a healthy OSS community, and you shouldn't really expect maintainers to work for free fixing bugs for libraries that are being used by company without even giving credit back to authors. This is an issue with OSS in general, with companies just expecting maintainers to do unpaid work to maintain libraries they rely on as if they had some support contract in place.
  • I would still be willing to take a look at PRs if anyone wanted to jump in and fix that bit. @rootflood already identified where the bug is, it's just a matter of patching that bit of codegen to address the incorrect charset decoding there. I don't have time to work on this right now between work, other personal projects I'm maintaining, and just having some personal time as well (and to be honest, updating BinaryPack is really down in my priorities list), but anyone's welcome to jump in. I can't guarantee I'll find the time to review and merge PRs immediately, but I will definitely take a look at some point 😄

Cheers!

If it's that much of a blocking issue, certainly you could justify the time to fix the issue and make a PR to your employer.

i think use this lines and replace to that buggy line could fix it

buggy code:

il.EmitLoadLocal(Locals.Write.Length);

fix via replace that buggy code to:

il.EmitLoadArgument(Arguments.Write.T);
il.EmitReadMember(typeof(string).GetProperty(nameof(string.Length)));

i think use this lines and replace to that buggy line could fix it

buggy code:

il.EmitLoadLocal(Locals.Write.Length);

fix via replace that buggy code to:

il.EmitLoadArgument(Arguments.Write.T);
il.EmitReadMember(typeof(string).GetProperty(nameof(string.Length)));

I'm not very familiar with IL coding. Can you please help with some more clear words what you recommend?

Which part should be replaced with what code?

Thanks a lot for your efforts.