App: Improve Document::writeObjects

Do not directly pass getNameInDocument() to the ostream as this will
set the badbit in case it returns nullptr. As a result no further
output is written to the stream and leads to loss of data.

Instead the returned C string is stored in a local variable and in case
it's nullptr an empty string is passed to the ostream.

At the end of the function writeObjects() it's checked whether the
failbit or badbit is set. If yes an error message is printed and the
failbit or badbit are cleared.

This is supposed to fix the issue 18044.

In case this fix is not sufficient it can be also check for every
object inside writeObjectData()
This commit is contained in:
wmayer
2025-03-12 19:01:19 +01:00
committed by Ladislav Michl
parent 2c3d9e2bb7
commit c97f6e5b3f

View File

@@ -1259,10 +1259,11 @@ void Document::writeObjectDeps(const std::vector<DocumentObject*>& objs,
DocumentObject::OutListNoXLinked);
// clang-format on
auto objName = o->getNameInDocument();
writer.Stream() << writer.ind()
<< "<" << fcElementObjectDeps
<< " " << fcAttrDepObjName << "=\""
<< o->getNameInDocument() << "\" "
<< (objName ? objName : "") << "\" "
<< fcAttrDepCount << "=\""
<< outList.size();
if (outList.empty()) {
@@ -1276,11 +1277,11 @@ void Document::writeObjectDeps(const std::vector<DocumentObject*>& objs,
writer.Stream() << "\">\n";
writer.incInd();
for (auto dep : outList) {
auto name = dep ? dep->getNameInDocument() : "";
auto depName = dep ? dep->getNameInDocument() : "";
writer.Stream() << writer.ind()
<< "<" << fcElementObjectDep
<< " " << fcAttrDepObjName << "=\""
<< (name ? name : "") << "\"/>\n";
<< (depName ? depName : "") << "\"/>\n";
}
writer.decInd();
writer.Stream() << writer.ind() << "</" << fcElementObjectDeps << ">\n";
@@ -1368,6 +1369,14 @@ void Document::writeObjects(const std::vector<DocumentObject*>& objs,
writeObjectData(objs, writer);
writer.decInd(); // indentation for 'Objects count'
// check for errors
if (writer.hasFailed()) {
std::cerr << "Output stream is in error state. As a result the "
"Document.xml file may be incomplete.\n";
// reset the error flags to try to safe the data files
writer.clear();
}
}
struct DepInfo