Skip to content

Commit d24b75c

Browse files
committed
App: Coverity fixes
1 parent 20408ba commit d24b75c

File tree

10 files changed

+40
-36
lines changed

10 files changed

+40
-36
lines changed

src/App/ComplexGeoData.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ void ComplexGeoData::restoreStream(std::istream& stream, std::size_t count)
586586
// NOLINTNEXTLINE
587587
FC_THROWM(Base::RuntimeError, "Failed to restore element map " << _persistenceName);
588588
}
589+
constexpr std::size_t oneGbOfInts {(1 << 30) / sizeof(int)};
590+
if (sCount > oneGbOfInts) {
591+
// NOLINTNEXTLINE
592+
FC_THROWM(Base::RuntimeError, "Failed to restore element map (>1GB) " << _persistenceName);
593+
}
589594
sids.reserve(static_cast<int>(sCount));
590595
for (std::size_t j = 0; j < sCount; ++j) {
591596
long id = 0;

src/App/ComplexGeoData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ class AppExport ComplexGeoData: public Base::Persistence, public Base::Handled
369369
*/
370370
void traceElement(const MappedName& name, TraceCallback cb) const
371371
{
372-
_elementMap->traceElement(name, Tag, cb);
372+
_elementMap->traceElement(name, Tag, std::move(cb));
373373
}
374374

375375
/** Flush an internal buffering for element mapping */

src/App/Document.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,9 @@ void Document::_removeObject(DocumentObject* pcObject)
38073807
_checkTransaction(pcObject, nullptr, __LINE__);
38083808

38093809
auto pos = d->objectMap.find(pcObject->getNameInDocument());
3810+
if (pos == d->objectMap.end()) {
3811+
FC_ERR("Internal error, could not find " << pcObject->getFullName() << " to remove");
3812+
}
38103813

38113814
if (!d->rollback && d->activeUndoTransaction && pos->second->hasChildElement()) {
38123815
// Preserve link group children global visibility. See comments in
@@ -4272,15 +4275,15 @@ std::vector<App::DocumentObject*> Document::getRootObjectsIgnoreLinks() const
42724275
{
42734276
std::vector<App::DocumentObject*> ret;
42744277

4275-
for (auto objectIt : d->objectArray) {
4278+
for (const auto &objectIt : d->objectArray) {
42764279
auto list = objectIt->getInList();
42774280
bool noParents = list.empty();
42784281

42794282
if (!noParents) {
42804283
// App::Document getRootObjects returns the root objects of the dependency graph.
4281-
// So if an object is referenced by a App::Link, it will not be returned by that
4284+
// So if an object is referenced by an App::Link, it will not be returned by that
42824285
// function. So here, as we want the tree-root level objects, we check if all the
4283-
// parents are links. In which case its still a root object.
4286+
// parents are links. In which case it's still a root object.
42844287
noParents = std::all_of(list.cbegin(), list.cend(), [](App::DocumentObject* obj) {
42854288
return obj->isDerivedFrom<App::Link>();
42864289
});
@@ -4300,21 +4303,19 @@ void DocumentP::findAllPathsAt(const std::vector<Node>& all_nodes,
43004303
Path tmp)
43014304
{
43024305
if (std::find(tmp.begin(), tmp.end(), id) != tmp.end()) {
4303-
Path tmp2(tmp);
4304-
tmp2.push_back(id);
4305-
all_paths.push_back(tmp2);
4306+
tmp.push_back(id);
4307+
all_paths.push_back(std::move(tmp));
43064308
return; // a cycle
43074309
}
43084310

43094311
tmp.push_back(id);
43104312
if (all_nodes[id].empty()) {
4311-
all_paths.push_back(tmp);
4313+
all_paths.push_back(std::move(tmp));
43124314
return;
43134315
}
43144316

43154317
for (size_t i = 0; i < all_nodes[id].size(); i++) {
4316-
Path tmp2(tmp);
4317-
findAllPathsAt(all_nodes, all_nodes[id][i], all_paths, tmp2);
4318+
findAllPathsAt(all_nodes, all_nodes[id][i], all_paths, tmp);
43184319
}
43194320
}
43204321

@@ -4344,18 +4345,18 @@ Document::getPathsByOutList(const App::DocumentObject* from, const App::Document
43444345
size_t index_to = indexMap[to];
43454346
Path tmp;
43464347
std::vector<Path> all_paths;
4347-
DocumentP::findAllPathsAt(all_nodes, index_from, all_paths, tmp);
4348+
DocumentP::findAllPathsAt(all_nodes, index_from, all_paths, std::move(tmp));
43484349

43494350
for (const Path& it : all_paths) {
43504351
Path::const_iterator jt = std::find(it.begin(), it.end(), index_to);
43514352
if (jt != it.end()) {
4352-
std::list<App::DocumentObject*> path;
4353+
array.push_back({});
4354+
auto& path = array.back();
43534355
for (Path::const_iterator kt = it.begin(); kt != jt; ++kt) {
43544356
path.push_back(d->objectArray[*kt]);
43554357
}
43564358

43574359
path.push_back(d->objectArray[*jt]);
4358-
array.push_back(path);
43594360
}
43604361
}
43614362

src/App/DocumentObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ void DocumentObject::Save(Base::Writer& writer) const
11621162

11631163
void DocumentObject::setExpression(const ObjectIdentifier& path, std::shared_ptr<Expression> expr)
11641164
{
1165-
ExpressionEngine.setValue(path, expr);
1165+
ExpressionEngine.setValue(path, std::move(expr));
11661166
}
11671167

11681168
/**

src/App/DocumentObjectPyImp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ PyObject* DocumentObjectPy::setExpression(PyObject* args)
383383
shared_expr->comment = comment;
384384
}
385385

386-
getDocumentObjectPtr()->setExpression(p, shared_expr);
386+
getDocumentObjectPtr()->setExpression(p, std::move(shared_expr));
387387
Py_Return;
388388
}
389389

src/App/ElementMap.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ ElementMapPtr ElementMap::restore(::App::StringHasherRef hasherRef, std::istream
258258

259259
std::vector<ElementMapPtr> childMaps;
260260
count = 0;
261-
if (!(stream >> tmp >> count) || tmp != "MapCount" || count == 0) {
261+
constexpr int practicalMaximum {1 << 30 / sizeof(ElementMapPtr)}; // a 1GB child map vector: almost certainly a bug
262+
if (!(stream >> tmp >> count) || tmp != "MapCount" || count == 0 || count > practicalMaximum) {
262263
FC_THROWM(Base::RuntimeError, msg); // NOLINT
263264
}
264265
childMaps.reserve(count - 1);
@@ -285,6 +286,10 @@ ElementMapPtr ElementMap::restore(::App::StringHasherRef hasherRef,
285286
if (!(stream >> tmp >> index >> id >> typeCount) || tmp != "ElementMap") {
286287
FC_THROWM(Base::RuntimeError, msg); // NOLINT
287288
}
289+
constexpr int maxTypeCount(1000);
290+
if (typeCount < 0 || typeCount > maxTypeCount) {
291+
FC_THROWM(Base::RuntimeError, "Bad type count in element map, ignoring map"); // NOLINT
292+
}
288293

289294
auto& map = _idToElementMap[id];
290295
if (map) {

src/App/Expression.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ FunctionExpression::FunctionExpression(const DocumentObject *_owner, Function _f
17281728
: UnitExpression(_owner)
17291729
, f(_f)
17301730
, fname(std::move(name))
1731-
, args(_args)
1731+
, args(std::move(_args))
17321732
{
17331733
switch (f) {
17341734
case ABS:
@@ -2643,7 +2643,7 @@ Expression *FunctionExpression::simplify() const
26432643
return eval();
26442644
}
26452645
else
2646-
return new FunctionExpression(owner, f, std::string(fname), a);
2646+
return new FunctionExpression(owner, f, std::string(fname), std::move(a));
26472647
}
26482648

26492649
/**
@@ -2811,7 +2811,7 @@ Expression *FunctionExpression::_copy() const
28112811
a.push_back((*i)->copy());
28122812
++i;
28132813
}
2814-
return new FunctionExpression(owner, f, std::string(fname), a);
2814+
return new FunctionExpression(owner, f, std::string(fname), std::move(a));
28152815
}
28162816

28172817
void FunctionExpression::_visit(ExpressionVisitor &v)

src/App/ExpressionTokenizer.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ QString ExpressionTokenizer::perform(const QString& prefix, int pos)
7575
// Pop those trailing tokens depending on the given position, which may be
7676
// in the middle of a token, and we shall include that token.
7777
for (auto it = tokens.begin(); it != tokens.end(); ++it) {
78-
if (std::get<1>(*it) >= pos) {
78+
int tokenType = std::get<0>(*it);
79+
int location = std::get<1>(*it);
80+
int tokenLength = static_cast<int> (std::get<2>(*it).size());
81+
if (location >= pos) {
7982
// Include the immediately followed '.' or '#', because we'll be
8083
// inserting these separators too, in ExpressionCompleteModel::pathFromIndex()
81-
if (it != tokens.begin() && std::get<0>(*it) != '.' && std::get<0>(*it) != '#') {
84+
if (it != tokens.begin() && tokenType != '.' && tokenType != '#') {
8285
it = it - 1;
8386
}
84-
tokens.resize(it - tokens.begin() + 1);
85-
prefixEnd = start + std::get<1>(*it) + (int)std::get<2>(*it).size();
87+
tokens.resize(it - tokens.begin() + 1); // Invalidates it, but we already calculated tokenLength
88+
prefixEnd = start + location + tokenLength;
8689
break;
8790
}
8891
}

src/App/GeoFeature.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,6 @@ bool GeoFeature::getCameraAlignmentDirection(Base::Vector3d& direction, const ch
223223
bool GeoFeature::hasMissingElement(const char* subname)
224224
{
225225
return Data::hasMissingElement(subname);
226-
if (!subname) {
227-
return false;
228-
}
229-
auto dot = strrchr(subname, '.');
230-
if (!dot) {
231-
return subname[0] == '?';
232-
}
233-
return dot[1] == '?';
234226
}
235227

236228
void GeoFeature::updateElementReference()

src/App/GeoFeatureGroupExtension.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,8 @@ bool GeoFeatureGroupExtension::extensionGetSubObject(DocumentObject*& ret,
421421
}
422422
}
423423
if (ret) {
424-
if (dot) {
425-
++dot;
426-
}
427-
if (dot && *dot && !ret->hasExtension(App::LinkBaseExtension::getExtensionClassTypeId())
424+
++dot;
425+
if (*dot && !ret->hasExtension(App::LinkBaseExtension::getExtensionClassTypeId())
428426
&& !ret->hasExtension(App::GeoFeatureGroupExtension::getExtensionClassTypeId())) {
429427
// Consider this
430428
// Body
@@ -451,7 +449,7 @@ bool GeoFeatureGroupExtension::extensionGetSubObject(DocumentObject*& ret,
451449
*mat *=
452450
const_cast<GeoFeatureGroupExtension*>(this)->placement().getValue().toMatrix();
453451
}
454-
ret = ret->getSubObject(dot ? dot : "", pyObj, mat, true, depth + 1);
452+
ret = ret->getSubObject(dot, pyObj, mat, true, depth + 1);
455453
}
456454
}
457455
return true;

0 commit comments

Comments
 (0)