{"record":{"id":"e42c8f44d912e4e3","repo":"quarkusio/quarkus","slug":"count-count-must-be-0-file","errorCode":null,"errorMessage":"Count (${count}) must be >= 0: ${file}","messagePattern":"Count \\((.+?)\\) must be >= 0: (.+?)","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":42,"sourceCode":"\n    /**\n     * 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":24,"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#L24-L51","documentation":"IllegalArgumentException from the FilePart constructor: a partial-file response was requested with a negative byte count. The constructor validates that the file exists, is a regular readable file, and that offset/count are non-negative and within the file; here the count argument failed the >= 0 check.","triggerScenarios":"new FilePart(file, offset, count) with count < 0, typically from arithmetic like remaining = total - alreadySent going negative.","commonSituations":"Chunked upload logic that subtracts more than the remaining size; misparsed Content-Length or range values; sending the same chunk twice so 'remaining' underflows.","solutions":["Clamp count with Math.max(0, remaining) before constructing the part.","Fix the remaining-bytes computation to be monotonic (never decrement below zero).","Validate computed sizes before creating parts."],"exampleFix":"// before\nlong remaining = total - sent;\nnew FilePart(file, offset, remaining); // can be negative\n// after\nlong remaining = Math.max(0, total - sent);\nif (remaining > 0) new FilePart(file, offset, remaining);","handlingStrategy":"validation","validationCode":"if (count < 0) throw new IllegalStateException(\"Invalid count \" + count + \" for \" + file);\nFilePart part = new FilePart(file, offset, count);","typeGuard":"static boolean isValidRange(File f, long offset, long count) {\n    return f != null && offset >= 0 && count >= 0 && offset + count <= f.length();\n}","tryCatchPattern":"try {\n    return new FilePart(file, offset, count);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Count (\")) {\n        log.warn(\"Bad count {} for {}, skipping empty part\", count, file);\n        return null;\n    }\n    throw e;\n}","preventionTips":["Clamp counts with Math.max(0, remaining)","Make remaining-bytes calculations monotonic","Skip constructing parts entirely when count == 0"],"tags":["io","argument-validation","multipart"],"backgroundTag":"invalid-range-length","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"}