{"record":{"id":"7db84b03567e87ce","repo":"quarkusio/quarkus","slug":"offset-count-offset-count-larger-than-fil","errorCode":null,"errorMessage":"Offset + count (${offset + count}) larger than file size (${file.length()}): ${file}","messagePattern":"Offset \\+ count \\((.+?)\\) larger than file size \\((.+?)\\): (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/FilePart.java","lineNumber":44,"sourceCode":"     * Create a new partial {@link File} object.\n     *\n     * @param file The file to send\n     * @param offset The starting byte of the file (must be >= 0)\n     * @param count The number of bytes to send (must be >= 0 and offset+count <= file size)\n     */\n    public FilePart(File file, long offset, long count) {\n        if (!file.exists())\n            throw new IllegalArgumentException(\"File does not exist: \" + file);\n        if (!file.isFile())\n            throw new IllegalArgumentException(\"File is not a regular file: \" + file);\n        if (!file.canRead())\n            throw new IllegalArgumentException(\"File cannot be read: \" + file);\n        if (offset < 0)\n            throw new IllegalArgumentException(\"Offset (\" + offset + \") must be >= 0: \" + file);\n        if (count < 0)\n            throw new IllegalArgumentException(\"Count (\" + count + \") must be >= 0: \" + file);\n        if ((offset + count) > file.length())\n            throw new IllegalArgumentException(\n                    \"Offset + count (\" + (offset + count) + \") larger than file size (\" + file.length() + \"): \" + file);\n        this.file = file;\n        this.offset = offset;\n        this.count = count;\n    }\n}\n","sourceCodeStart":26,"sourceCodeEnd":51,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/FilePart.java#L26-L51","documentation":"FilePart validates that the requested range (offset + count) fits within the actual file size. Requesting more bytes than the file contains would produce a truncated/invalid transfer, so the constructor throws this IllegalArgumentException listing the computed range, file size, and file.","triggerScenarios":"new FilePart(file, offset, count) where offset + count > file.length(), e.g. using a stale file-length captured before the file was rewritten to a smaller size.","commonSituations":"Caching file length from a previous version of the file that was later truncated or replaced; hardcoding counts instead of using file.length(); race where another process shrinks the file between length check and construction.","solutions":["Re-read file.length() immediately before constructing the FilePart instead of using a cached size.","Clamp the range: count = Math.min(count, file.length() - offset).","Use Files.size(Path) on a freshly opened/locked snapshot of the file if it changes concurrently.","Log the actual file size and requested range to diagnose which is stale."],"exampleFix":"// before\nlong size = cachedLength; // stale\nnew FilePart(file, offset, size);\n// after\nlong size = file.length();\nlong count = Math.min(size - offset, requestedCount);\nnew FilePart(file, offset, count);","handlingStrategy":"validation","validationCode":"long len = file.length();\nif (offset + count > len) {\n    count = Math.max(0, len - offset); // clamp to actual size\n}\nFilePart part = new FilePart(file, offset, count);","typeGuard":"static boolean rangeFits(File f, long offset, long count) {\n    return f != null && offset >= 0 && count >= 0 && offset <= f.length() && offset + count <= f.length();\n}","tryCatchPattern":"try {\n    return new FilePart(file, offset, count);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Offset + count\")) {\n        log.warn(\"Range [{}..{}] exceeds size {} of {}, reclamping\", offset, offset + count, file.length(), file);\n        return new FilePart(file, offset, Math.max(0, file.length() - offset));\n    }\n    throw e;\n}","preventionTips":["Always read file.length() fresh, never cache sizes across file rewrites","Clamp ranges to the actual file size before constructing parts","Re-validate if files can be modified concurrently by other processes"],"tags":["io","argument-validation","multipart"],"backgroundTag":"range-exceeds-file-size","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}