From c97f6e5b3f29d9a4f698686ea63067596cf29236 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 12 Mar 2025 19:01:19 +0100 Subject: [PATCH] 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() --- src/App/Document.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/App/Document.cpp b/src/App/Document.cpp index 2d2bfa940e..fe186f3c41 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -1259,10 +1259,11 @@ void Document::writeObjectDeps(const std::vector& 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& 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() << "\n"; @@ -1368,6 +1369,14 @@ void Document::writeObjects(const std::vector& 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