Whole file

martinblech/xmltodict

The author described this change as fix: validate XML comments. It counts as a record because the checks below fail on the code as it stood at b4a5f2a3f and pass on 3d4d2d3a4, with nothing else changed between the two runs.

Fix saved2025-09-16
Sharing licenceMIT · LICENSE
Change size+17 1

What the code was meant to do, written into the code itself as a save note

fix: validate XML comments

The change

402402 raise ValueError(f"Invalid {kind} name: whitespace not allowed")
403403
404404
405+def _validate_comment(value):
406+ if isinstance(value, bytes):
407+ try:
408+ value = value.decode("utf-8")
409+ except UnicodeDecodeError as exc:
410+ raise ValueError("Comment text must be valid UTF-8") from exc
411+ if not isinstance(value, str):
412+ raise ValueError("Comment text must be a string")
413+ if "--" in value:
414+ raise ValueError("Comment text cannot contain '--'")
415+ if value.endswith("-"):
416+ raise ValueError("Comment text cannot end with '-'")
417+ return value
418+
419+
405420 def _process_namespace(name, namespaces, ns_sep=':', attr_prefix='@'):
406421 if not isinstance(name, str):
407422 return name
440455 if comment_text is None:
441456 continue
442457 comment_text = _convert_value_to_string(comment_text)
443- if comment_text == "":
458+ if not comment_text:
444459 continue
445460 if pretty:
446461 content_handler.ignorableWhitespace(depth * indent)
520535
521536 class _XMLGenerator(XMLGenerator):
522537 def comment(self, text):
538+ text = _validate_comment(text)
523539 self._write(f"<!--{escape(text)}-->")
524540
525541

The check that tells the two apart

failpass·tests/test_dicttoxml.py::test_unparse_rejects_comment_ending_with_hyphen
failpass·tests/test_dicttoxml.py::test_unparse_rejects_comment_with_double_hyphen

Check file tests/test_dicttoxml.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before itb4a5f2a3f04aff68384486e957632c8438396fd6
Broken version dated2025-09-16
Modulexmltodict
Units changed_XMLGenerator, _emit
Fingerprintc87f2ba243b5d4ec
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in martinblech/xmltodict