Skip to content

Bad use of OutOfMemoryError to validate buffer size #629

Closed

Description

@duongkame

In SnappyInputStream, OutOfMemoryError is used to validate the buffer size to allocated.

try {
compressed = new byte[chunkSize];
}
catch (java.lang.OutOfMemoryError e) {
throw new SnappyError(SnappyErrorCode.INVALID_CHUNK_SIZE, e.getMessage());
}

This's a bad way to validate buffer size as explain here.
And another serious consequence, catching OutOfMemoryError in Java code requires the applications not to use JVM options like CrashOnOutOfMemoryError or ExitOnOutOfMemoryError to automatically restart the JVM when it runs out of memory.

I'm wondering if there is a specific reason why that could not be a simple conditional validation?

Activity

  1. xerial commented on Feb 4, 2025

    @xerial
    Owner

    There wasn't any specific reason for that. It's better to be fixed as you suggested.

  2. duongkame commented on Feb 26, 2025

    @duongkame
    ContributorAuthor
  3. xerial commented on Jul 20, 2025

    @xerial
    Owner

    Fixed in #647. Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions