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

ByteArrayOutputStream is not disposed in NPOI.Util.IOUtils.ToByteArray method. Potential Memory leak #1388

Open
1 task done
guangyu2002 opened this issue Jul 17, 2024 · 2 comments

Comments

@guangyu2002
Copy link
Contributor

guangyu2002 commented Jul 17, 2024

NPOI Version

2.7.1-rc1

File Type

  • DOCX

Issue Description

///


/// Reads up to {@code length} bytes from the input stream, and returns the bytes read.
///

///
///
///
public static byte[] ToByteArray(Stream stream, int length)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(length == Int32.MaxValue ? 4096 : length);

        byte[] buffer = new byte[4096];
        int totalBytes = 0, readBytes;
        do
        {
            readBytes = stream.Read(buffer, 0, Math.Min(buffer.Length, length - totalBytes));
            totalBytes += Math.Max(readBytes, 0);
            if (readBytes > 0)
            {
                baos.Write(buffer, 0, readBytes);
            }
        } while (totalBytes < length && readBytes > 0);

        if (length != Int32.MaxValue && totalBytes < length)
        {
            throw new IOException("unexpected EOF");
        }

        return baos.ToByteArray();
    }
@guangyu2002 guangyu2002 changed the title NPOI.Util.IOUtils中ToByteArray方法,ByteArrayOutputStream创建后是否未Dispose,是否会有内存泄露的bug呢? NPOI.Util.IOUtils中ToByteArray方法,ByteArrayOutputStream创建后未Dispose,是否会有内存泄露的bug呢? Jul 17, 2024
@tonyqus tonyqus changed the title NPOI.Util.IOUtils中ToByteArray方法,ByteArrayOutputStream创建后未Dispose,是否会有内存泄露的bug呢? ByteArrayOutputStream is not disposed in NPOI.Util.IOUtils.ToByteArray method. Potential Memory leak Jul 17, 2024
@tonyqus
Copy link
Member

tonyqus commented Jul 17, 2024

Yes, it can be an issue. Can you create a PR for this?

@guangyu2002
Copy link
Contributor Author

Yes, it can be an issue. Can you create a PR for this?

Of course, I'll create a PR for this issue as soon as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants