App: Fix output string to XML

Not all unicode characters are allowed as XML output. When writing disallowed characters the SAX parser throws an exception
when loading a project file that results into a broken document and thus to a possible loss of data.

This PR replaces all disallowed characters with an underscore and prints a warning.

This fixes https://github.com/FreeCAD/FreeCAD/issues/22123
Note: It does not fix an already corrupted project file.
This commit is contained in:
wmayer
2025-08-15 20:45:11 +02:00
committed by Max Wilfinger
parent 9d806694ff
commit 696c18b6da
4 changed files with 54 additions and 0 deletions

View File

@@ -1509,6 +1509,14 @@ void PropertyString::setPyObject(PyObject* value)
void PropertyString::Save(Base::Writer& writer) const
{
auto verifyXMLString = [this](std::string& input) {
const std::string output = this->validateXMLString(input);
if (output != input) {
Base::Console().warning("XML output: Validate invalid string:\n'%s'\n'%s'\n",
input, output);
}
return output;
};
std::string val;
auto obj = freecad_cast<DocumentObject*>(getContainer());
writer.Stream() << writer.ind() << "<String ";
@@ -1520,11 +1528,13 @@ void PropertyString::Save(Base::Writer& writer) const
else if (_cValue == obj->getNameInDocument()) {
writer.Stream() << "restore=\"0\" ";
val = encodeAttribute(obj->getExportName());
val = verifyXMLString(val);
exported = true;
}
}
if (!exported) {
val = encodeAttribute(_cValue);
val = verifyXMLString(val);
}
writer.Stream() << "value=\"" << val << "\"/>" << std::endl;
}