{"record":{"id":"79463cc0acd79dd1","repo":"stanfordnlp/CoreNLP","slug":"unclosed-bracket-in-encoded-array-encoded","errorCode":null,"errorMessage":"Unclosed bracket in encoded array: \" + encoded","messagePattern":"Unclosed bracket in encoded array: \" \\+ encoded","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/StringUtils.java","lineNumber":2566,"sourceCode":"   * Decode an array encoded as a String. This entails a comma separated value enclosed in brackets\n   * or parentheses.\n   *\n   * @param encoded The String encoding an array\n   * @return A String array corresponding to the encoded array\n   */\n  public static String[] decodeArray(String encoded) {\n    if (encoded.isEmpty()) return EMPTY_STRING_ARRAY;\n    char[] chars = encoded.trim().toCharArray();\n\n    //--Parse the String\n    // (state)\n    char quoteCloseChar = (char) 0;\n    List<String> terms = new ArrayList<>();\n    StringBuilder current = new StringBuilder();\n    //(start/stop overhead)\n    int start = 0; int end = chars.length;\n    if(chars[0] == '('){ start += 1; end -= 1; if(chars[end] != ')') throw new IllegalArgumentException(\"Unclosed paren in encoded array: \" + encoded); }\n    if(chars[0] == '['){ start += 1; end -= 1; if(chars[end] != ']') throw new IllegalArgumentException(\"Unclosed bracket in encoded array: \" + encoded); }\n    if(chars[0] == '{'){ start += 1; end -= 1; if(chars[end] != '}') throw new IllegalArgumentException(\"Unclosed bracke in encoded array: \" + encoded); }\n    // (finite state automaton)\n    for (int i=start; i<end; i++) {\n      if (chars[i] == '\\r') {\n        // Ignore funny windows carriage return\n        continue;\n      } else if (quoteCloseChar != 0) {\n        //(case: in quotes)\n        if(chars[i] == quoteCloseChar){\n          quoteCloseChar = (char) 0;\n        }else{\n          current.append(chars[i]);\n        }\n      } else if(chars[i] == '\\\\'){\n        //(case: escaped character)\n        if(i == chars.length - 1) throw new IllegalArgumentException(\"Last character of encoded array is escape character: \" + encoded);\n        current.append(chars[i+1]);\n        i += 1;","sourceCodeStart":2548,"sourceCodeEnd":2584,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/StringUtils.java#L2548-L2584","documentation":"StringUtils' encoded-array decoder (stringToArray-style parsing) expects an array encoded as \"(a,b,c)\" or \"[a,b]\". When the string starts with '[' the decoder strips one character from each end and requires the final character to be ']'. If it is not, the encoded array is malformed and this IllegalArgumentException is thrown so the caller fails fast instead of silently mis-parsing.","triggerScenarios":"Calling the public StringUtils decode method with a string whose first character is '[' but whose last character is not ']' (e.g. \"[a,b\", \"[a,b)\", \"[]x\").","commonSituations":"Hand-built or truncated encoded strings pasted into config files; string truncation by logging/copy-paste; off-by-one string slicing when programmatically building the encoded form; stray characters appended after the closing bracket.","solutions":["Inspect the input string and add the missing ']' as the last character","Ensure the program that generates the encoded string appends the matching close bracket","Trim stray whitespace/trailing characters before passing the string to the decoder","Wrap the call in try-catch on IllegalArgumentException and report the malformed input to the user"],"exampleFix":"// before\nString[] terms = StringUtils.decode(\"[foo,bar\");\n// after\nString[] terms = StringUtils.decode(\"[foo,bar]\");","handlingStrategy":"validation","validationCode":"if (s != null && s.startsWith(\"[\") && !s.endsWith(\"]\")) throw new IllegalArgumentException(\"Encoded array must end with ']': \" + s);","typeGuard":"static boolean isBracketBalanced(String s) { return s != null && s.length() >= 2 && ((s.charAt(0)=='[' && s.charAt(s.length()-1)==']') || (s.charAt(0)=='(' && s.charAt(s.length()-1)==')') || (s.charAt(0)=='{' && s.charAt(s.length()-1)=='}')); }","tryCatchPattern":null,"preventionTips":["Always emit matching open/close delimiters when building encoded strings","Trim and validate input before decoding","Never hand-edit encoded strings without checking the final character"],"tags":["java","string-parsing","malformed-input","illegal-argument"],"backgroundTag":"invalid-argument-format","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}