{"record":{"id":"0f397f364e0a02a2","repo":"alibaba/nacos","slug":"list-size-must-be-a-multiple-of-2","errorCode":null,"errorMessage":"list size must be a multiple of 2","messagePattern":"list size must be a multiple of 2","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/com/alibaba/nacos/common/http/param/Header.java","lineNumber":113,"sourceCode":"        List<String> list = new ArrayList<>(header.size() * 2);\n        Iterator<Map.Entry<String, String>> iterator = iterator();\n        while (iterator.hasNext()) {\n            Map.Entry<String, String> entry = iterator.next();\n            list.add(entry.getKey());\n            list.add(entry.getValue());\n        }\n        return list;\n    }\n    \n    /**\n     * Add all KV list to header. The odd index is key and the even index is value.\n     *\n     * @param list KV list\n     * @return header\n     */\n    public Header addAll(List<String> list) {\n        if ((list.size() & 1) != 0) {\n            throw new IllegalArgumentException(\"list size must be a multiple of 2\");\n        }\n        for (int i = 0; i < list.size();) {\n            String key = list.get(i++);\n            if (StringUtils.isNotEmpty(key)) {\n                header.put(key, list.get(i++));\n            }\n        }\n        return this;\n    }\n    \n    /**\n     * Add all parameters to header.\n     *\n     * @param params parameters\n     */\n    public void addAll(Map<String, String> params) {\n        if (MapUtil.isNotEmpty(params)) {\n            for (Map.Entry<String, String> entry : params.entrySet()) {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java#L95-L131","documentation":"Thrown by Header.addAll(List<String>) when the supplied KV list has an odd number of entries. The method treats the list as flat key/value pairs (odd index = key, even index = value), so an odd size leaves a dangling key with no value and the contract is violated. It is an IllegalArgumentException raised before any entry is put into the header map.","triggerScenarios":"Calling Header.newInstance().addAll(list) or header.addAll(Arrays.asList(\"k1\",\"v1\",\"k2\")) where list.size() is odd; building a Header from a flattened collection produced by splitting or streaming without ensuring pairs.","commonSituations":"Manually flattening a Map into an alternating key/value List and dropping the last value; passing a varargs/collection whose last element was filtered out; off-by-one when zipping headers from an HTTP response into a flat list.","solutions":["Ensure the list length is even before calling addAll: assert (list.size() & 1) == 0, or guard with an if.","Build the list from an explicit Map.entrySet() loop that always writes both key and value, so a pair can never be split.","Prefer header.addAll(Map<String,String>) (the map overload in the same class) over the flat List form to make pairing structural.","If the list is user supplied, log list.size() and the offending tail before rejecting so the caller sees the unpaired key."],"exampleFix":"// before\nList<String> flat = new ArrayList<>();\nheaders.forEach((k,v) -> { flat.add(k); /* v conditionally skipped */ });\nheader.addAll(flat); // throws if any value omitted\n\n// after\nMap<String,String> safe = new LinkedHashMap<>();\nheaders.forEach((k,v) -> { if (k != null && v != null) safe.put(k, v); });\nheader.addAll(safe);","handlingStrategy":"validation","validationCode":"if (list == null || (list.size() & 1) != 0) {\n    throw new IllegalArgumentException(\"header KV list must have even size, got \" + (list == null ? 0 : list.size()));\n}\nheader.addAll(list);","typeGuard":"boolean isEvenKvList(List<String> list) {\n    return list != null && (list.size() & 1) == 0;\n}","tryCatchPattern":null,"preventionTips":["Prefer the Map<String,String> overload of addAll over the flat List form.","When flattening a Map, always write both key and value in the same iteration.","Add a unit test asserting addAll rejects odd-sized lists and accepts even ones."],"tags":["http-client","validation","header","argument"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}