Skip to content

Throw exception if output buffer size is greater than max buffer size #418

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
merged 1 commit into from
Apr 10, 2016

Conversation

WilliamDo
Copy link
Contributor

Currently, the Output class allows the initial buffer size to be greater than the max buffer size. In some cases, we can observe an ArrayIndexOutOfBoundsException during an attempt to expand the buffer. The following test cases demonstrate the behaviour.

    @Test
    public void testNewOutputMaxBufferSizeLessThanBufferSize() {
        int bufferSize = 2;
        int maxBufferSize = 1;

        Output output = new Output(bufferSize, maxBufferSize);

        for (int i = 0; i < bufferSize; i++) {
            output.writeByte(0);
        }

        output.writeByte(0);

    }

    @Test
    public void testSetOutputMaxBufferSizeLessThanBufferSize() {
        int bufferSize = 2;
        int maxBufferSize = 1;

        Output output = new Output(bufferSize, bufferSize);
        output.setBuffer(new byte[bufferSize], maxBufferSize);

        for (int i = 0; i < bufferSize; i++) {
            output.writeByte(0);
        }

        output.writeByte(0);

    }

Suggestion is to throw an IllegalArgumentException when setting the buffer sizes to pre-empt this error.

@magro
Copy link
Collaborator

magro commented Apr 10, 2016

LGTM. Only the test might go into InputOutputTest.java, right?

@WilliamDo
Copy link
Contributor Author

Yes that makes sense. Moved the tests and as a result, also found my mistake with maxBufferSize=-1 which is valid case being less than the initial buffer size. I've also updated the code for this.

@magro
Copy link
Collaborator

magro commented Apr 10, 2016

Cool. Can you squash the commits? Then I'll merge. Thanks!

…n when setting a buffer size greater than the max buffer size
@WilliamDo
Copy link
Contributor Author

No worries, all done.

@magro magro merged commit 6769443 into EsotericSoftware:master Apr 10, 2016
@magro
Copy link
Collaborator

magro commented Apr 10, 2016

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants