{"record":{"id":"23a634e6ac77f50f","repo":"FasterXML/jackson-databind","slug":"cannot-set-maxserializercachesize-to-a-negative-va","errorCode":null,"errorMessage":"Cannot set maxSerializerCacheSize to a negative value","messagePattern":"Cannot set maxSerializerCacheSize to a negative value","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java","lineNumber":184,"sourceCode":"                throw new IllegalArgumentException(\"Cannot set maxDeserializerCacheSize to a negative value\");\n            }\n            _maxDeserializerCacheSize = maxDeserializerCacheSize;\n            return this;\n        }\n\n        /**\n         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forSerializerCache(SerializationConfig)}\n         * and {@link #_buildCache(int)}\n         * <p>\n         * Note that specifying a maximum size of zero prevents values from being retained in the cache.\n         *\n         * @param maxSerializerCacheSize Size for the {@link LookupCache} to use within {@link SerializerCache}\n         * @return this builder\n         * @throws IllegalArgumentException if {@code maxSerializerCacheSize} is negative\n         */\n        public Builder maxSerializerCacheSize(int maxSerializerCacheSize) {\n            if (maxSerializerCacheSize < 0) {\n                throw new IllegalArgumentException(\"Cannot set maxSerializerCacheSize to a negative value\");\n            }\n            _maxSerializerCacheSize = maxSerializerCacheSize;\n            return this;\n        }\n\n        /**\n         * Define the maximum size of the {@link LookupCache} instance constructed by {@link #forTypeFactory()}\n         * and {@link #_buildCache(int)}\n         * <p>\n         * Note that specifying a maximum size of zero prevents values from being retained in the cache.\n         *\n         * @param maxTypeFactoryCacheSize Size for the {@link LookupCache} to use within {@link tools.jackson.databind.type.TypeFactory}\n         * @return this builder\n         * @throws IllegalArgumentException if {@code maxTypeFactoryCacheSize} is negative\n         */\n        public Builder maxTypeFactoryCacheSize(int maxTypeFactoryCacheSize) {\n            if (maxTypeFactoryCacheSize < 0) {\n                throw new IllegalArgumentException(\"Cannot set maxTypeFactoryCacheSize to a negative value\");","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/FasterXML/jackson-databind/blob/a50c7d2a1d57234ac4adf70dbd88ac90db6436e4/src/main/java/tools/jackson/databind/cfg/DefaultCacheProvider.java#L166-L202","documentation":"DefaultCacheProvider.Builder.maxSerializerCacheSize(int) sets the cap for the serializer cache (SerializerCache) and rejects negative values with IllegalArgumentException. Zero disables caching (serializers are rebuilt each time) but a negative size is invalid. This guards the serializer side symmetrically with the deserializer cache guard.","triggerScenarios":"Calling builder.maxSerializerCacheSize(-1) or any negative; computing the cap from a formula or external config that underflows; reusing a '-1 sentinel' convention from another caching library.","commonSituations":"Property-driven cache tuning where the default is -1; arithmetic like (availableMemory - reserved) going negative on small heaps; migrating from 2.x cache settings that used different sentinels.","solutions":["Use 0 to disable caching or a large positive int for 'effectively unlimited'; never pass a negative.","Sanitize external config values: Math.max(0, configured) at the read boundary.","Document the 'unlimited' choice explicitly as a large int in your config schema.","Add a test for the builder rejecting negatives."],"exampleFix":"// before\nDefaultCacheProvider p = DefaultCacheProvider.builder()\n    .maxSerializerCacheSize(config.getSerCacheMax()).build(); // throws when -1\n// after\nint ser = Math.max(0, config.getSerCacheMax());\nDefaultCacheProvider p = DefaultCacheProvider.builder()\n    .maxSerializerCacheSize(ser).build();","handlingStrategy":"validation","validationCode":"int cap = configuredSerCache;\nif (cap < 0) throw new IllegalArgumentException(\"ser cache < 0: \" + cap);\nDefaultCacheProvider.builder().maxSerializerCacheSize(cap).build();","typeGuard":"// primitive int range check","tryCatchPattern":"try {\n    return DefaultCacheProvider.builder().maxSerializerCacheSize(cap).build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"negative\")) {\n        return DefaultCacheProvider.builder().maxSerializerCacheSize(0).build();\n    }\n    throw e;\n}","preventionTips":["Floor external config: Math.max(0, configured).","Treat -1 as 'large positive' at the read boundary.","Add tests for 0 and large-positive values."],"tags":["cache","configuration","builder","validation"],"analyzedSha":"a50c7d2a1d57234ac4adf70dbd88ac90db6436e4","analyzedAt":"2026-08-06T20:31:51.404Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}