{"record":{"id":"95705c10b98d3c3b","repo":"apache/hadoop","slug":"unterminated-entity-ref-starting-with","errorCode":null,"errorMessage":"unterminated entity ref starting with {}","messagePattern":"unterminated entity ref starting with (.+?)","errorType":"exception","errorClass":"UnmanglingError","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/XMLUtils.java","lineNumber":231,"sourceCode":"        escapedCp = \"\";\n        slashPosition = -1;\n      } else if (ch == '\\\\') {\n        slashPosition = 0;\n      } else {\n        boolean startingEntityRef = false;\n        if (decodeEntityRefs) {\n          startingEntityRef = (ch == '&');\n        }\n        if (startingEntityRef) {\n          entityRef = new StringBuilder();\n          entityRef.append(\"&\");\n        } else {\n          bld.append(ch);\n        }\n      }\n    }\n    if (entityRef != null) {\n      throw new UnmanglingError(\"unterminated entity ref starting with \" +\n          entityRef.toString());\n    } else if (slashPosition != -1) {\n      throw new UnmanglingError(\"unterminated code point escape: string \" +\n          \"broke off in the middle\");\n    }\n    return bld.toString();\n  }\n  \n  /**\n   * Add a SAX tag with a string inside.\n   *\n   * @param contentHandler     the SAX content handler\n   * @param tag                the element tag to use  \n   * @param val                the string to put inside the tag\n   */\n  public static void addSaxString(ContentHandler contentHandler,\n      String tag, String val) throws SAXException {\n    contentHandler.startElement(\"\", \"\", tag, new AttributesImpl());","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/XMLUtils.java#L213-L249","documentation":"With decodeEntityRefs=true, a '&' starts an entity reference that must be closed by ';' before the string ends. If the scan loop finishes while entityRef is still open, unmangleXmlString throws UnmanglingError(\"unterminated entity ref starting with \" + the partial text accumulated so far).","triggerScenarios":"The input ends inside an entity — e.g. \"AT&amp\" (missing ';') or a bare trailing '&' — while decodeEntityRefs is true. Every character after the '&' is appended to the pending ref, so the message can include trailing junk such as \"&amp B\".","commonSituations":"Truncated values read from fsimage XML; column-limited or tail-cut copies of XML content; user input containing a bare ampersand passed to the decoder.","solutions":["Terminate the entity: append the missing ';' (\"&amp\" -> \"&amp;\")","If the '&' is literal text rather than an entity, escape it as &amp; first or call with decodeEntityRefs=false","Validate string completeness (balanced quotes, matching tags, expected length) before unmangling"],"exampleFix":"// before\nXMLUtils.unmangleXmlString(\"A &amp B\", true); // '&' never closed by ';' -> throws\n\n// after\nXMLUtils.unmangleXmlString(\"A &amp; B\", true); // ok","handlingStrategy":"validation","validationCode":"static boolean entityClosed(String s) {\n  return s.lastIndexOf('&') <= s.lastIndexOf(';');\n}","typeGuard":null,"tryCatchPattern":"try {\n  XMLUtils.unmangleXmlString(s, true);\n} catch (XMLUtils.UnmanglingError e) {\n  // string ended mid-entity; fix truncation at the source and retry\n}","preventionTips":["Check that the last '&' is closed by a later ';' before decoding","Avoid truncating or column-limiting mangled XML values","Escape bare ampersands as &amp; at the producer side"],"tags":["xml","parsing","entity-references","hdfs","fsimage"],"backgroundTag":"xml-entity-decoding-error","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}