{"record":{"id":"b92925afc88a814c","repo":"oracle/graal","slug":"negative-array-size","errorCode":null,"errorMessage":"Negative array size: {}","messagePattern":"Negative array size: (.+?)","errorType":"validation","errorClass":"NegativeArraySizeException","httpStatus":null,"severity":"error","filePath":"espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java","lineNumber":584,"sourceCode":"                    throw new IllegalArgumentException(\"Element \" + i + \" should be an espresso object constant, got \" + safeGetClass(element));\n                }\n                try {\n                    array.setArrayElement(i, objectElement.getValue());\n                } catch (ClassCastException e) {\n                    throw new IllegalArgumentException(\"Element \" + i + \" is not an instance of the component type\", e);\n                }\n            }\n        }\n        return new EspressoExternalObjectConstant(this, array);\n    }\n\n    @Override\n    public JavaConstant createPrimitiveArray(JavaKind kind, int length) {\n        if (kind == null || !kind.isPrimitive() || kind == JavaKind.Void) {\n            throw new IllegalArgumentException(\"Expected a non-void primitive kind, got \" + kind);\n        }\n        if (length < 0) {\n            throw new NegativeArraySizeException(\"Negative array size: \" + length);\n        }\n        Value array = invokeJVMCIHelper(\"newPrimitiveArray\", (int) kind.getTypeChar(), 1, length);\n        return new EspressoExternalObjectConstant(this, array);\n    }\n\n    @Override\n    public JavaConstant clonePrimitiveArray(JavaConstant primitiveArray) {\n        if (!(primitiveArray instanceof EspressoExternalObjectConstant objectConstant)) {\n            throw new IllegalArgumentException(\"Expected an EspressoExternalObjectConstant, got \" + safeGetClass(primitiveArray));\n        }\n        EspressoResolvedObjectType arrayType = objectConstant.getType();\n        if (!(arrayType.isArray() && arrayType.getComponentType().isPrimitive())) {\n            throw new IllegalArgumentException(\"Expected a primitive array constant, got \" + arrayType);\n        }\n        Value copy = invokeJVMCIHelper(\"clonePrimitiveArray\", objectConstant.getValue());\n        return new EspressoExternalObjectConstant(this, copy);\n    }\n","sourceCodeStart":566,"sourceCodeEnd":602,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java#L566-L602","documentation":"createPrimitiveArray throws NegativeArraySizeException when length < 0, mirroring the standard Java behavior for negative array allocation ('Negative array size: N'). The guest allocation is never attempted; the check is host-side so the error surfaces before any cross-language call.","triggerScenarios":"Passing a computed length that underflows or is derived from unvalidated user input, e.g. size() of an empty collection minus one, or a parsed negative integer.","commonSituations":"Length arithmetic like list.size() - offset; deserialized or user-supplied dimensions; sign errors when converting unsigned guest values to host ints.","solutions":["Validate length >= 0 before the call and reject/handle the input that produced it.","Clamp or default to 0 when an empty array is acceptable.","Fix the upstream arithmetic (use Math.max(0, n) or explicit bounds checks)."],"exampleFix":"// before\nJavaConstant a = vmAccess.createPrimitiveArray(JavaKind.Int, list.size() - 1);\n\n// after\nint n = Math.max(0, list.size() - 1);\nJavaConstant a = vmAccess.createPrimitiveArray(JavaKind.Int, n);","handlingStrategy":"validation","validationCode":"if (length < 0) {\n    throw new IllegalArgumentException(\"length must be >= 0: \" + length);\n}","typeGuard":null,"tryCatchPattern":"catch (NegativeArraySizeException e) -> fix the length computation; do not retry with abs().","preventionTips":["Use Math.max(0, n) when zero-length is acceptable.","Validate sizes coming from user input or deserialization at the boundary.","Watch for size()-offset underflow patterns."],"tags":["jvmci","espresso","arrays","negative-size","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}