{"record":{"id":"ed1a82b18cc12a71","repo":"CarGuo/GSYVideoPlayer","slug":"max-size-must-be-positive-number","errorCode":null,"errorMessage":"Max size must be positive number!","messagePattern":"Max size must be positive number!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gsyVideoPlayer-proxy_cache/src/main/java/com/danikula/videocache/file/TotalSizeLruDiskUsage.java","lineNumber":16,"sourceCode":"package com.danikula.videocache.file;\n\nimport java.io.File;\n\n/**\n * {@link DiskUsage} that uses LRU (Least Recently Used) strategy and trims cache size to max size if needed.\n *\n * @author Alexey Danilov (danikula@gmail.com).\n */\npublic class TotalSizeLruDiskUsage extends LruDiskUsage {\n\n    private final long maxSize;\n\n    public TotalSizeLruDiskUsage(long maxSize) {\n        if (maxSize <= 0) {\n            throw new IllegalArgumentException(\"Max size must be positive number!\");\n        }\n        this.maxSize = maxSize;\n    }\n\n    @Override\n    protected boolean accept(File file, long totalSize, int totalCount) {\n        return totalSize <= maxSize;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/CarGuo/GSYVideoPlayer/blob/e5d74d3aa9d7fb1393a879e33ee380f8f41354f1/gsyVideoPlayer-proxy_cache/src/main/java/com/danikula/videocache/file/TotalSizeLruDiskUsage.java#L1-L26","documentation":"IllegalArgumentException thrown by the TotalSizeLruDiskUsage constructor (TotalSizeLruDiskUsage.java:14-18) when maxSize <= 0. TotalSizeLruDiskUsage is the default LRU trimming strategy of HttpProxyCacheServer, capping total cache bytes; the Builder instantiates it in build() at HttpProxyCacheServer.java:402 via maxCacheSize(long), or with DEFAULT_MAX_SIZE (512 MB, HttpProxyCacheServer.java:357) when no size is set. Like error 21 it is fail-fast configuration validation — a non-positive byte budget is invalid.","triggerScenarios":"Calling new HttpProxyCacheServer.Builder(context).maxCacheSize(0) or a negative long, then .build() — build() runs new TotalSizeLruDiskUsage(maxSize) at HttpProxyCacheServer.java:402 and throws. Also direct construction new TotalSizeLruDiskUsage(size) with 0/negative, typically from a computed value such as (long) (freeDiskBytes * ratio) that evaluates to 0 when freeDiskBytes is 0 or the ratio is misconfigured.","commonSituations":"Cache size derived from available disk space (StatFs / StorageUtils) that returns 0 on inaccessible storage or is computed with integer arithmetic that truncates to 0 (e.g. 200 * 1024 * 1024 in an int overflow becoming negative); a remote-config max-size placeholder of 0; developers writing maxCacheSize(0) expecting 'no limit' (the default of 512 MB applies instead only if the call is omitted); MB-vs-bytes confusion leading to values like maxCacheSize(200) still being valid but far smaller than intended, then '0' when adjusted with the wrong multiplier.","solutions":["Pass a positive byte size to maxCacheSize(), e.g. maxCacheSize(200L * 1024 * 1024) — note the L to avoid int overflow.","If 'unlimited' was intended, remove the maxCacheSize call; the Builder defaults to DEFAULT_MAX_SIZE = 512 MB.","Clamp computed sizes: long size = Math.max(1L, computedCacheBytes); builder.maxCacheSize(size).","When deriving from free disk space, check StatFs/StorageUtils availability first and fall back to a fixed constant (e.g. 256 MB) when the reported free space is 0."],"exampleFix":"// before: int overflow -> negative -> IllegalArgumentException\nnew HttpProxyCacheServer.Builder(context)\n        .maxCacheSize(200 * 1024 * 1024) // int math overflows for values >= 2GB, and 0/neg throws\n        .build();\n\n// after: long literal with clamp\nlong maxSize = Math.max(1L, 200L * 1024 * 1024);\nnew HttpProxyCacheServer.Builder(context)\n        .maxCacheSize(maxSize)\n        .build();","handlingStrategy":"validation","validationCode":"// Validate before calling maxCacheSize()\nlong requestedBytes = computeCacheBudgetBytes(context); // any external source\nif (requestedBytes <= 0) {\n    throw new IllegalStateException(\"video cache maxCacheSize must be > 0 bytes, got \" + requestedBytes);\n}\nHttpProxyCacheServer server = new HttpProxyCacheServer.Builder(context)\n        .maxCacheSize(requestedBytes)\n        .build();","typeGuard":null,"tryCatchPattern":"try {\n    server = builder.maxCacheSize(maxBytes).build();\n} catch (IllegalArgumentException e) {\n    if (\"Max size must be positive number!\".equals(e.getMessage())) {\n        server = builder.build(); // fall back to builder default (512 MB)\n    } else {\n        throw e;\n    }\n}","preventionTips":["Always use long literals for byte math (1024L * 1024 * 200) to avoid int overflow producing 0/negative.","Clamp externally sourced sizes to a sane floor, e.g. Math.max(1L, value).","When deriving size from free disk space, guard against StatFs reporting 0 and fall back to a constant.","Remember bytes, not MB: maxCacheSize(200) is 200 bytes; omit the call entirely for the 512 MB default."],"tags":["android","configuration","validation","cache","builder","arithmetic-overflow","videocache"],"backgroundTag":null,"analyzedSha":"e5d74d3aa9d7fb1393a879e33ee380f8f41354f1","analyzedAt":"2026-08-14T11:56:11.997Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}