Class ResourceLimits
java.lang.Object
opennlp.tools.util.ResourceLimits
Shared upper bounds for counts read from user-supplied resources, so a crafted
file cannot force an outsized allocation before validation completes.
- Since:
- 3.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUpper bound on count fields and resource sizes that drive allocations (matrix dimensions, lexicon entries, model outcome counts, and similar).static final StringSystem property for overridingMAX_ENTRIES.static final intUpper bound on the cell count of a two-dimensional cost table, whose entries are far smaller than the record-sized entriesMAX_ENTRIESbounds.static final StringSystem property for overridingMAX_MATRIX_CELLS. -
Method Summary
Modifier and TypeMethodDescriptionstatic longReads a positive long limit from a system property.static voidrequireWithinMaxEntries(long count, String label) Checks a count read from a resource againstMAX_ENTRIES.static <E extends Exception>
voidrequireWithinMaxEntries(long count, String label, Function<String, E> exceptionFactory) Checks a count read from a resource againstMAX_ENTRIESand reports a violation through an exception of the caller's choice.
-
Field Details
-
MAX_ENTRIES_PROPERTY
System property for overridingMAX_ENTRIES. Set at JVM startup, e.g.-DOPENNLP_MAX_ENTRIES=5000000. Falls back to10_000_000if absent or invalid.- See Also:
-
MAX_ENTRIES
public static final int MAX_ENTRIESUpper bound on count fields and resource sizes that drive allocations (matrix dimensions, lexicon entries, model outcome counts, and similar). Configurable viaMAX_ENTRIES_PROPERTY. -
MAX_MATRIX_CELLS_PROPERTY
System property for overridingMAX_MATRIX_CELLS. Set at JVM startup, e.g.-Dopennlp.max.matrix.cells=20000000. Falls back to134_217_728if absent or invalid.- See Also:
-
MAX_MATRIX_CELLS
public static final int MAX_MATRIX_CELLSUpper bound on the cell count of a two-dimensional cost table, whose entries are far smaller than the record-sized entriesMAX_ENTRIESbounds. The default of 2^27 cells caps a 16-bit cost matrix at 256 MiB, which admits every published MeCab-format distribution (mecab-ko-dic 2.1.1 alone declares 3822 x 2693, aboveMAX_ENTRIES) while still refusing the roughly 4 GiB allocation a crafted46340 46340header would force. Configurable viaMAX_MATRIX_CELLS_PROPERTY.
-
-
Method Details
-
initLimit
Reads a positive long limit from a system property. The value is trimmed before parsing.- Parameters:
property- The system property name. Must not benull.defaultValue- The value used when the property is absent, not a number, or not positive.- Returns:
- The configured limit, or
defaultValue. - Throws:
IllegalArgumentException- Thrown ifpropertyisnull.
-
requireWithinMaxEntries
Checks a count read from a resource againstMAX_ENTRIES.- Parameters:
count- The count to check.label- Describes the count in the error message, e.g."Outcome count". Must not benull.- Throws:
IllegalArgumentException- Thrown iflabelisnull, or ifcountis negative or exceedsMAX_ENTRIES.
-
requireWithinMaxEntries
@Internal public static <E extends Exception> void requireWithinMaxEntries(long count, String label, Function<String, E> exceptionFactory) throws EChecks a count read from a resource againstMAX_ENTRIESand reports a violation through an exception of the caller's choice.- Type Parameters:
E- The type of exception thrown on a violation.- Parameters:
count- The count to check.label- Describes the count in the error message, e.g."unigram count". Must not benull.exceptionFactory- Creates the exception from the error message. Must not benull.- Throws:
E- Thrown ifcountis negative or exceedsMAX_ENTRIES.IllegalArgumentException- Thrown iflabelorexceptionFactoryisnull.
-