{"record":{"id":"2bf0ba381872c36c","repo":"java-native-access/jna","slug":"structure-size-must-be-greater-than-zero-size","errorCode":null,"errorMessage":"Structure size must be greater than zero: \" + size","messagePattern":"Structure size must be greater than zero: \" \\+ size","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Structure.java","lineNumber":433,"sourceCode":"\n    private void allocateMemory(boolean avoidFFIType) {\n        allocateMemory(calculateSize(true, avoidFFIType));\n    }\n\n\n    /** Provided for derived classes to indicate a different\n     * size than the default.  Returns whether the operation was successful.\n     * Will leave memory untouched if it is non-null and not allocated\n     * by this class.\n     * @param size how much memory to allocate\n     */\n    protected void allocateMemory(int size) {\n        if (size == CALCULATE_SIZE) {\n            // Analyze the struct, but don't worry if we can't yet do it\n            size = calculateSize(false);\n        }\n        else if (size <= 0) {\n            throw new IllegalArgumentException(\"Structure size must be greater than zero: \" + size);\n        }\n        // May need to defer size calculation if derived class not fully\n        // initialized\n        if (size != CALCULATE_SIZE) {\n            if (this.memory == null\n                || this.memory instanceof AutoAllocated) {\n                this.memory = autoAllocate(size);\n            }\n            this.size = size;\n        }\n    }\n\n    /** Returns the size in memory occupied by this Structure.\n     * @return Native size of this structure, in bytes.\n     */\n    public int size() {\n        ensureAllocated();\n        return this.size;","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Structure.java#L415-L451","documentation":"Structure.allocateMemory(int) rejects non-positive sizes with this IllegalArgumentException. When allocateMemory is called with an explicit size (not CALCULATE_SIZE), that size must be positive because JNA cannot allocate a zero- or negative-length native block for the structure.","triggerScenarios":"Calling the protected allocateMemory(size) with size <= 0 from a subclass (e.g. custom allocateMemory logic or a Structure subclass overriding size calculation), or calculateSize(false) returning a non-positive value that then flows into allocation.","commonSituations":"Structures whose calculateSize returns 0 because they have no public/recognized fields (all fields non-public, static, or of unsupported types); subclasses that hard-code a size of 0; structs with only transient/ignored fields.","solutions":["Give the Structure at least one mappable public instance field (JNA-aligned types), so calculateSize() returns a positive size.","If calling allocateMemory manually, pass a positive byte count (or Structure.CALCULATE_SIZE to defer to layout analysis).","Check that field types are supported JNA types (int, long, Pointer, Structure, etc.); unsupported fields are skipped and can leave the size at 0.","Ensure the subclass is fully initialized when allocateMemory runs — fields added after allocation can yield 0 size; call calculateSize(true) to diagnose."],"exampleFix":"// before\nclass Empty extends Structure {\n    private int hidden; // not public -> size 0\n}\nnew Empty().allocateMemory(0); // IllegalArgumentException\n\n// after\nclass Empty extends Structure {\n    public int value; // mappable field -> positive size\n}\nnew Empty().allocateMemory(new Empty().size());","handlingStrategy":"validation","validationCode":"int size = structure.calculateSize(false);\nif (size <= 0) {\n    throw new IllegalStateException(\"structure has no mappable fields; size=\" + size);\n}","typeGuard":"static boolean hasMappableFields(Structure s) { return !s.getFields().isEmpty(); }","tryCatchPattern":"try {\n    allocateMemory(requestedSize);\n} catch (IllegalArgumentException e) {\n    int computed = calculateSize(true);\n    if (computed > 0) allocateMemory(computed);\n    else throw new IllegalStateException(\"no mappable fields in \" + getClass(), e);\n}","preventionTips":["Ensure every Structure has at least one public, non-static, JNA-supported instance field.","Never hard-code 0 or negative sizes in allocateMemory overrides; use CALCULATE_SIZE.","Verify all field types are JNA-mappable when a struct unexpectedly computes size 0."],"tags":["jna","structure","size-validation","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}