Skip to content
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

Fix for System.Buffers.ArrayPool #52

Merged
merged 1 commit into from
Feb 27, 2020

Commits on Feb 26, 2020

  1. Fix for System.Buffers.ArrayPool

    Context: dotnet/android#4320
    
    After using libZipSharp 1.0.9 in xamarin-android, one of our tests
    surfaced a bug:
    
        Xamarin.Tools.Zip.ZipIOException : Stream is not a ZIP archive
    
    The test created a small text file inside a zip using a `MemoryStream`.
    
    Something like:
    
        using (var zip = ZipArchive.Create (zipStream)) {
            zip.AddEntry ("foo", "bar", encoding);
        }
    
    In 750c780, there was one mistake:
    
        case SourceCommand.Write:
            buffer = ArrayPool<byte>.Shared.Rent (length);
            try {
                Marshal.Copy (data, buffer, 0, length);
                stream.Write (buffer, 0, length);
                return buffer.Length; // Whoops!
            } finally {
                ArrayPool<byte>.Shared.Return (buffer);
            }
    
    In the test case above, the size of the buffer will be very small: the
    equivalent of `Encoding.UTF8.GetBytes("bar")`. If `ArrayPool` returns
    a *larger* buffer than we asked for, the code is actually wrong.
    
    We need to return `length` instead of `buffer.Length`. I would suspect
    that almost everything worked with this bug in place, except for this
    case. `xaprepare` was able to do its work: downloading and unzipping
    the Android SDK/NDK.
    
    I was able to add a unit test showing this problem.
    jonathanpeppers committed Feb 26, 2020
    Configuration menu
    Copy the full SHA
    ebe04d2 View commit details
    Browse the repository at this point in the history