{"record":{"id":"bb04deb102ad2569","repo":"nostra13/Android-Universal-Image-Loader","slug":"cachemaxsize-argument-must-be-positive-number","errorCode":null,"errorMessage":"cacheMaxSize argument must be positive number","messagePattern":"cacheMaxSize argument must be positive number","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/LruDiskCache.java","lineNumber":88,"sourceCode":"\t}\n\n\t/**\n\t * @param cacheDir          Directory for file caching\n\t * @param reserveCacheDir   null-ok; Reserve directory for file caching. It's used when the primary directory isn't available.\n\t * @param fileNameGenerator {@linkplain com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator\n\t *                          Name generator} for cached files. Generated names must match the regex\n\t *                          <strong>[a-z0-9_-]{1,64}</strong>\n\t * @param cacheMaxSize      Max cache size in bytes. <b>0</b> means cache size is unlimited.\n\t * @param cacheMaxFileCount Max file count in cache. <b>0</b> means file count is unlimited.\n\t * @throws IOException if cache can't be initialized (e.g. \"No space left on device\")\n\t */\n\tpublic LruDiskCache(File cacheDir, File reserveCacheDir, FileNameGenerator fileNameGenerator, long cacheMaxSize,\n\t\t\tint cacheMaxFileCount) throws IOException {\n\t\tif (cacheDir == null) {\n\t\t\tthrow new IllegalArgumentException(\"cacheDir\" + ERROR_ARG_NULL);\n\t\t}\n\t\tif (cacheMaxSize < 0) {\n\t\t\tthrow new IllegalArgumentException(\"cacheMaxSize\" + ERROR_ARG_NEGATIVE);\n\t\t}\n\t\tif (cacheMaxFileCount < 0) {\n\t\t\tthrow new IllegalArgumentException(\"cacheMaxFileCount\" + ERROR_ARG_NEGATIVE);\n\t\t}\n\t\tif (fileNameGenerator == null) {\n\t\t\tthrow new IllegalArgumentException(\"fileNameGenerator\" + ERROR_ARG_NULL);\n\t\t}\n\n\t\tif (cacheMaxSize == 0) {\n\t\t\tcacheMaxSize = Long.MAX_VALUE;\n\t\t}\n\t\tif (cacheMaxFileCount == 0) {\n\t\t\tcacheMaxFileCount = Integer.MAX_VALUE;\n\t\t}\n\n\t\tthis.reserveCacheDir = reserveCacheDir;\n\t\tthis.fileNameGenerator = fileNameGenerator;\n\t\tinitCache(cacheDir, reserveCacheDir, cacheMaxSize, cacheMaxFileCount);","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/LruDiskCache.java#L70-L106","documentation":"LruDiskCache's constructor throws IllegalArgumentException when cacheMaxSize is negative. The documented contract is: 0 means unlimited (normalized internally to Long.MAX_VALUE), positive means a byte cap; negative is meaningless and rejected. Note the message text ('must be positive number') is slightly misleading — 0 is actually accepted.","triggerScenarios":"Calling new LruDiskCache(..., cacheMaxSize, ...) with a negative long, e.g. from diskCacheExtraConfiguration(...) with a negative maxCacheSize, or from arithmetic like (availableBytes - reserve) going negative.","commonSituations":"Computing cache size from free disk space that underflows when the reserve exceeds available; configuration constants defaulting to -1 as 'unset' and passed through unnormalized; typos in size config.","solutions":["Pass a positive byte size (e.g. 50 * 1024 * 1024) or 0 for unlimited.","Clamp computed sizes: Math.max(0, available - reserve).","If -1 is your 'unset' sentinel, map it to 0 before constructing the cache."],"exampleFix":"// before\nlong size = freeDiskBytes - RESERVE; // negative on nearly-full disk\nnew LruDiskCache(dir, null, gen, size, 0);\n\n// after\nlong size = Math.max(0, freeDiskBytes - RESERVE);\nnew LruDiskCache(dir, null, gen, size, 0);","handlingStrategy":"validation","validationCode":"long sizeArg = (cacheMaxSize >= 0) ? cacheMaxSize : 0; // 0 == unlimited\nnew LruDiskCache(dir, reserve, gen, sizeArg, fileCount);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp size computations: Math.max(0, available - reserve).","Map -1 'unset' sentinels to 0 before constructing the cache.","Remember 0 is legal (unlimited); only negatives throw."],"tags":["disk-cache","lru","validation","configuration","constructor"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}