{"record":{"id":"c8205d0cd702e054","repo":"MuntashirAkon/AppManager","slug":"negative-size","errorCode":null,"errorMessage":"Negative size","messagePattern":"Negative size","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/RemoteFileChannel.java","lineNumber":253,"sourceCode":"            throw new IOException(e);\n        }\n    }\n\n    @Override\n    public long size() throws IOException {\n        ensureOpen();\n        try {\n            return fs.size(handle).tryAndGet();\n        } catch (RemoteException e) {\n            throw new IOException(e);\n        }\n    }\n\n    @Override\n    public RemoteFileChannel truncate(long size) throws IOException {\n        ensureOpen();\n        if (size < 0)\n            throw new IllegalArgumentException(\"Negative size\");\n        if (!writable())\n            throw new NonWritableChannelException();\n        try {\n            fs.ftruncate(handle, size).checkException();\n            return this;\n        } catch (RemoteException e) {\n            throw new IOException(e);\n        }\n    }\n\n    @Override\n    public void force(boolean metaData) throws IOException {\n        ensureOpen();\n        try {\n            fs.sync(handle, metaData).checkException();\n        } catch (RemoteException e) {\n            throw new IOException(e);\n        }","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/RemoteFileChannel.java#L235-L271","documentation":"RemoteFileChannel.truncate(size) throws IllegalArgumentException(\"Negative size\") when the requested truncation length is below zero, mirroring FileChannel's contract. The channel checks this before any IPC to avoid sending an invalid size to the remote ftruncate.","triggerScenarios":"Calling truncate(size) with size < 0, typically from a computed value like fileSize - extra where fileSize was -1 (unknown) or an arithmetic underflow.","commonSituations":"Using a size obtained from a failed stat/length() call (-1); subtracting a padding larger than the file; passing an uninitialized sentinel value.","solutions":["Clamp the target size: use Math.max(0, size) before calling truncate.","Validate the source of the size (e.g. length()) and treat -1 as an error before truncating.","Check the subtraction that produced the size for underflow.","Assert size >= 0 in the calling code path with a clear failure message."],"exampleFix":"// before\nlong size = channel.size() - padding;\nchannel.truncate(size); // may be negative\n// after\nlong size = Math.max(0, channel.size() - padding);\nchannel.truncate(size);","handlingStrategy":"validation","validationCode":"if (size < 0) {\n    throw new IllegalArgumentException(\"truncate size must be >= 0, got \" + size);\n}\nchannel.truncate(size);","typeGuard":null,"tryCatchPattern":"try {\n    channel.truncate(size);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Bad truncate size {}\", size, e);\n}","preventionTips":["Clamp computed sizes with Math.max(0, x).","Treat length()/size() returning -1 as an error, not a truncatable value.","Validate user/config-supplied sizes before channel operations."],"tags":["illegal-argument","file-channel","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}