{"record":{"id":"21eb907af2906456","repo":"oracle/graal","slug":"length-of-target-array-must-equal-the-size-of-the","errorCode":null,"errorMessage":"Length of target array must equal the size of the set.","messagePattern":"Length of target array must equal the size of the set\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdk/src/org.graalvm.collections/src/org/graalvm/collections/UnmodifiableEconomicSet.java","lineNumber":92,"sourceCode":"     * Returns {@code true} if this set contains no elements.\n     *\n     * @since 19.0\n     */\n    boolean isEmpty();\n\n    /**\n     * Stores all the elements in this set into {@code target}. An\n     * {@link UnsupportedOperationException} will be thrown if the length of {@code target} does not\n     * match the size of this set.\n     *\n     * @return an array containing all the elements in this set.\n     * @throws UnsupportedOperationException if the length of {@code target} does not equal the size\n     *             of this set.\n     * @since 19.0\n     */\n    default E[] toArray(E[] target) {\n        if (target.length != size()) {\n            throw new UnsupportedOperationException(\"Length of target array must equal the size of the set.\");\n        }\n\n        int index = 0;\n        for (E element : this) {\n            target[index++] = element;\n        }\n\n        return target;\n    }\n\n    default HashSet<E> toHashSet() {\n        HashSet<E> set = new HashSet<>(size());\n        for (E elem : this) {\n            set.add(elem);\n        }\n        return set;\n    }\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/sdk/src/org.graalvm.collections/src/org/graalvm/collections/UnmodifiableEconomicSet.java#L74-L110","documentation":"UnmodifiableEconomicSet.toArray(E[] target) requires the target array length to exactly equal set.size(); otherwise it throws UnsupportedOperationException. Unlike java.util.Set.toArray(T[]) it does not allocate, grow, pad with null, or return a different array — it is a strict copy into a caller-provided, correctly sized array. (EconomicMapImpl's EconomicSet view shares this contract.)","triggerScenarios":"Calling toArray(target) where target was allocated with new E[0], with a cached/stale size, or where the set changed size between the size() call and toArray(); reusing one target array across differently sized sets.","commonSituations":"Porting java.util.Collection.toArray(T[]) idioms (which tolerate undersized/oversized arrays) to EconomicSet; concurrent modification between sizing and copying; using the classic 'pass empty array as hint' pattern (toArray(new E[0])) which is invalid here.","solutions":["Allocate the target in the same statement as the copy: E[] a = set.toArray(set.newArray(size())) or T[] target = Arrays.copyOf(template, set.size()); then set.toArray(target).","Snapshot the size once and ensure no mutations occur between sizing and copying (synchronize or copy under read lock).","Prefer iterating the set directly or toHashSet()/stream collectors instead of toArray when sizing is hard to guarantee."],"exampleFix":"// before\nE[] arr = set.toArray(new E[0]); // java.util idiom -> throws\n\n// after\nE[] arr = set.toArray(Arrays.copyOf(template, set.size()));","handlingStrategy":"validation","validationCode":"E[] target = Arrays.copyOf(template, set.size()); // size snapshot & alloc together\nE[] result = set.toArray(target);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Allocate the target from the same size() call you copy with; never reuse arrays across sets.","Do not carry over java.util toArray(new T[0]) idioms.","Ensure no mutation can occur between sizing and copying; synchronize if concurrent."],"tags":["graalvm","collections","economic-set","array"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}