{"record":{"id":"80d9a9ba03e8a121","repo":"JessYanCoding/AndroidAutoSize","slug":"negative-size","errorCode":null,"errorMessage":"negative size: ","messagePattern":"negative size: ","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"autosize/src/main/java/me/jessyan/autosize/utils/Preconditions.java","lineNumber":121,"sourceCode":"    }\n\n    /**\n     * Throws {@link IllegalStateException} if the calling thread is not the application's main\n     * thread.\n     *\n     * @throws IllegalStateException If the calling thread is not the application's main thread.\n     */\n    public static void checkMainThread() {\n        if (Looper.myLooper() != Looper.getMainLooper()) {\n            throw new IllegalStateException(\"Not in applications main thread\");\n        }\n    }\n\n    private static String badElementIndex(int index, int size, String desc) {\n        if (index < 0) {\n            return format(\"%s (%s) must not be negative\", new Object[]{desc, Integer.valueOf(index)});\n        } else if (size < 0) {\n            throw new IllegalArgumentException((new StringBuilder(26)).append(\"negative size: \").append(size).toString());\n        } else {\n            return format(\"%s (%s) must be less than size (%s)\", new Object[]{desc, Integer.valueOf(index), Integer.valueOf(size)});\n        }\n    }\n\n    public static int checkPositionIndex(int index, int size) {\n        return checkPositionIndex(index, size, \"index\");\n    }\n\n    public static int checkPositionIndex(int index, int size, String desc) {\n        if (index >= 0 && index <= size) {\n            return index;\n        } else {\n            throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));\n        }\n    }\n\n    private static String badPositionIndex(int index, int size, String desc) {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/JessYanCoding/AndroidAutoSize/blob/e402ecdd998ea07549513ac08c12e9f097f3b48c/autosize/src/main/java/me/jessyan/autosize/utils/Preconditions.java#L103-L139","documentation":"badElementIndex() is the private formatter behind checkElementIndex(). If the size argument passed alongside an index is negative, it throws IllegalArgumentException(\"negative size: N\") because an element count can never be negative. This indicates a caller bug: a computed count (e.g. list.size() from a mangled data structure or an off-by-one subtraction) produced a negative value.","triggerScenarios":"Calling checkElementIndex(index, size) with size < 0 — typically size computed as (end - start) or (count - n) where the subtraction underflowed, or a field initialized to -1 used as a size.","commonSituations":"Off-by-one arithmetic on collection bounds, sentinel value -1 leaking into a size parameter, or parsing/substring logic where a found-index of -1 is reused as a length.","solutions":["Fix the upstream computation so the size is a real, non-negative count.","Clamp or reject negative values at the source: if (size < 0) throw ... or Math.max(0, size) where 0 is a valid empty case.","Replace sentinel -1 usages (e.g. indexOf results) with explicit not-found handling before deriving a size.","Log the index/size arguments to identify which call site passes the negative size."],"exampleFix":"// before\nint size = end - start; // can be negative\nPreconditions.checkElementIndex(index, size);\n\n// after\nint size = Math.max(0, end - start);\nPreconditions.checkElementIndex(index, size);","handlingStrategy":"validation","validationCode":"public static void safeCheckElementIndex(int index, int size) {\n    if (size < 0) throw new IllegalArgumentException(\"size must be >= 0, got \" + size);\n    Preconditions.checkElementIndex(index, size);\n}","typeGuard":"public static boolean isValidSize(int size) {\n    return size >= 0;\n}","tryCatchPattern":"try {\n    Preconditions.checkElementIndex(index, size);\n} catch (IllegalArgumentException e) {\n    Log.e(\"Bounds\", \"bad index/size: \" + index + \"/\" + size, e);\n    throw new IllegalStateException(\"caller bug: negative or out-of-range size\", e);\n}","preventionTips":["Never use -1 as a size; use 0 for 'empty' and separate Optionals/booleans for 'not found'.","Validate ranges (start <= end) before computing derived sizes.","Prefer collection APIs (list.size(), list.get()) over manual index math where possible.","Enable assertions or unit tests on boundary arithmetic in index-computation code."],"tags":["android","arguments","index-check","negative-value"],"backgroundTag":"argument-out-of-range","analyzedSha":"e402ecdd998ea07549513ac08c12e9f097f3b48c","analyzedAt":"2026-09-07T15:05:42.094Z","contentChangedAt":"2026-09-07T15:05:42.094Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}