{"record":{"id":"f7d72aa03f311a90","repo":"apache/hadoop","slug":"error-parsing-unmangling-escape-code","errorCode":null,"errorMessage":"error parsing unmangling escape code","messagePattern":"error parsing unmangling escape code","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":211,"sourceCode":"          } else if (e.equals(\"&gt;\")) {\n            bld.append(\">\");\n          } else {\n            throw new UnmanglingError(\"Unknown entity ref \" + e);\n          }\n          entityRef = null;\n        }\n      } else  if ((slashPosition >= 0) && (slashPosition < NUM_SLASH_POSITIONS)) {\n        escapedCp += ch;\n        ++slashPosition;\n      } else if (slashPosition == NUM_SLASH_POSITIONS) {\n        if (ch != ';') {\n          throw new UnmanglingError(\"unterminated code point escape: \" +\n              \"expected semicolon at end.\");\n        }\n        try {\n          bld.appendCodePoint(Integer.parseInt(escapedCp, 16));\n        } catch (NumberFormatException e) {\n          throw new UnmanglingError(\"error parsing unmangling escape code\", e);\n        }\n        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    }","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/XMLUtils.java#L193-L229","documentation":"After collecting the 4 characters following a backslash, unmangleXmlString parses them as a base-16 code point via Integer.parseInt(escapedCp, 16). A NumberFormatException (non-hex characters present) is wrapped in UnmanglingError(\"error parsing unmangling escape code\") with the original exception chained as the cause.","triggerScenarios":"The 4 characters after a backslash are not all hex digits — e.g. \"\\\\zzff;\", or a literal backslash embedded in the input followed by 4 characters and a semicolon (mangleXmlString escapes real backslashes as \\005c;, so this only happens with strings that did not come from that method).","commonSituations":"Unmangling Windows-style paths or regex text containing raw backslashes; hand-edited escape sequences with typos; encoding mismatches that alter characters inside an escape.","solutions":["Escape literal backslashes before unmangling: s.replace(\"\\\\\", \"\\\\005c;\")","Only unmangle strings that were produced by XMLUtils.mangleXmlString in the first place","Catch UnmanglingError and inspect getCause() instanceof NumberFormatException to pinpoint the bad escape"],"exampleFix":"// before\nString raw = \"C:\\\\Users\\\\\"; // literal backslashes, not mangled\nXMLUtils.unmangleXmlString(raw, false); // can throw\n\n// after\nString escaped = raw.replace(\"\\\\\", \"\\\\005c;\");\nXMLUtils.unmangleXmlString(escaped, false);","handlingStrategy":"validation","validationCode":"static String escapeRawBackslashes(String s) {\n  return s.replace(\"\\\\\", \"\\\\005c;\"); // makes any literal backslash round-trip\n}","typeGuard":null,"tryCatchPattern":"try {\n  XMLUtils.unmangleXmlString(s, false);\n} catch (XMLUtils.UnmanglingError e) {\n  if (e.getCause() instanceof NumberFormatException) {\n    // non-hex digits after a backslash: input contained raw backslashes\n  }\n}","preventionTips":["Escape literal backslashes yourself before unmangling","Do not unmangle Windows paths, regex text, or any string with raw '\\\\'","Remember mangleXmlString escapes backslash as \\\\005c; — inputs must come from it"],"tags":["xml","parsing","escape-sequences","hdfs","fsimage"],"backgroundTag":"escape-sequence-parse-error","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}