{"record":{"id":"cde26f8a546417bf","repo":"didi/DoKit","slug":"max-size-must-be-positive","errorCode":null,"errorMessage":"Max size must be positive.","messagePattern":"Max size must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/LruCache.java","lineNumber":43,"sourceCode":"public class LruCache implements Cache {\n  final LinkedHashMap<String, Bitmap> map;\n  private final int maxSize;\n\n  private int size;\n  private int putCount;\n  private int evictionCount;\n  private int hitCount;\n  private int missCount;\n\n  /** Create a cache using an appropriate portion of the available RAM as the maximum size. */\n  public LruCache(Context context) {\n    this(Utils.calculateMemoryCacheSize(context));\n  }\n\n  /** Create a cache with a given maximum size in bytes. */\n  public LruCache(int maxSize) {\n    if (maxSize <= 0) {\n      throw new IllegalArgumentException(\"Max size must be positive.\");\n    }\n    this.maxSize = maxSize;\n    this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);\n  }\n\n  @Override public Bitmap get(String key) {\n    if (key == null) {\n      throw new NullPointerException(\"key == null\");\n    }\n\n    Bitmap mapValue;\n    synchronized (this) {\n      mapValue = map.get(key);\n      if (mapValue != null) {\n        hitCount++;\n        return mapValue;\n      }\n      missCount++;","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/LruCache.java#L25-L61","documentation":"LruCache's constructor throws IllegalArgumentException when maxSize is <= 0. The cache needs a positive byte budget to evict against; a zero or negative budget is a configuration mistake. Fail-fast guard, consistent with android.util.LruCache.","triggerScenarios":"Constructing new LruCache(0) or new LruCache(negative), e.g. computing a percentage of free memory that rounded/truncated to zero.","commonSituations":"Cache size computed from device metrics (free RAM, heap percentage) that yields 0 on constrained devices or emulators; hardcoded 0 during testing; integer overflow when computing a large size that wraps negative.","solutions":["Clamp the computed size to a sensible positive minimum before constructing: Math.max(size, 256 * 1024)","Use the LruCache(Context) constructor, which sizes the cache from available RAM automatically","Check the size-computation math for truncation or overflow"],"exampleFix":"// before\nint size = (int) (Runtime.getRuntime().maxMemory() / 10f * 0); // oops -> 0\nnew LruCache(size); // IllegalArgumentException\n\n// after\nint size = Math.max((int) (Runtime.getRuntime().maxMemory() / 10), 1024 * 1024);\nnew LruCache(size);","handlingStrategy":"validation","validationCode":"int size = Math.max(computeCacheBytes(context), 1024 * 1024);\nnew LruCache(size); // or simply new LruCache(context)","typeGuard":"boolean isValidCacheSize(int maxSize) { return maxSize > 0; }","tryCatchPattern":null,"preventionTips":["Clamp computed cache sizes to a positive minimum","Prefer the Context constructor, which derives a valid size from device RAM"],"tags":["android","picasso","cache","validation"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}