ObjectInputStream::readObject() should handle negative array sizes without throwing NegativeArraySizeExceptions

XMLWordPrintable

    • Type: Backport
    • Resolution: Unresolved
    • Priority: P3
    • 17, 17.0.18
    • Affects Version/s: 8, 11, 17, 20, 21
    • Component/s: core-libs
    • None

      Reported by: Yakov Shafranovich (yakovsh@amazon.com)

      Problem:
      ObjectInputStream::readObject() doesn't validate negative array lengths before calling Array::newInstance(), which throws
      NegativeArraySizeException - an unchecked exception not declared in the method signature or documented in the API.

      Impact:
      • Users aren't aware they need to handle NegativeArraySizeException
      • ObjectInputFilter.FilterInfo::arrayLength() violates its contract by returning negative values instead of the documented "non-negative
      number"

      Solution:
      Add explicit negative array length validation in ObjectInputStream::readObject() before filtering, and wrap negative lengths in
      InvalidClassException (a checked exception already declared in the method signature).

      Benefits:
      • Consistent exception handling through documented InvalidClassException
      • Fixes ObjectInputFilter.FilterInfo::arrayLength() contract violation
      • Better API usability and predictability

            Assignee:
            Roland Mesde
            Reporter:
            Volker Simonis
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: