From cfb4423f799d5a633a84742ee97b8789b3a54fbb Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 11 Dec 2021 23:48:59 +0100 Subject: [PATCH] Fix warning about possible null pointer deref --- model/entrymodel.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/model/entrymodel.cpp b/model/entrymodel.cpp index a9f5254..7d6546e 100644 --- a/model/entrymodel.cpp +++ b/model/entrymodel.cpp @@ -451,40 +451,44 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co } #endif // check validation of specified arguments: source and destination parent entries need to be node entries - if (!sourceParent.isValid() || !destinationParent.isValid() || sourceRow < 0 || count <= 0 || entry(sourceParent)->type() != EntryType::Node // - || entry(destinationParent)->type() != EntryType::Node) { + if (sourceRow < 0 || count <= 0) { + return false; + } + const auto *const srcParentEntry = entry(sourceParent); + const auto *const destParentEntry = entry(sourceParent); + if (!srcParentEntry || !destParentEntry || srcParentEntry->type() != EntryType::Node || destParentEntry->type() != EntryType::Node) { return false; } // determine the source parent entry and dest parent entry as node entries - auto *const srcParentEntry = static_cast(sourceParent.internalPointer()); - auto *const destParentEntry = static_cast(destinationParent.internalPointer()); + auto *const srcParentNodeEntry = static_cast(sourceParent.internalPointer()); + auto *const destParentNodeEntry = static_cast(destinationParent.internalPointer()); #if CPP_UTILITIES_DEBUG_BUILD cout << "destinationChild: " << destinationChild << endl; #endif // source rows must be within the valid range - if (static_cast(sourceRow + count) > srcParentEntry->children().size() + if (static_cast(sourceRow + count) > srcParentNodeEntry->children().size() // if source and destination parent are the same the destination child mustn't be in the source range - || !(srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) { + || !(srcParentNodeEntry != destParentNodeEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) { return false; } // do not move a row to one of its own children! -> check before for (int index = 0; index < count; ++index) { - Entry *const toMove = srcParentEntry->children()[static_cast(sourceRow + index)]; + Entry *const toMove = srcParentNodeEntry->children()[static_cast(sourceRow + index)]; if (toMove->type() != EntryType::Node) { continue; } - if (toMove == destParentEntry || destParentEntry->isIndirectChildOf(static_cast(toMove))) { + if (toMove == destParentNodeEntry || destParentNodeEntry->isIndirectChildOf(static_cast(toMove))) { return false; } } // actually perform the move operation beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild); for (int index = 0; index < count; ++index) { - Entry *toMove = srcParentEntry->children()[static_cast(sourceRow + index)]; - if (srcParentEntry == destParentEntry && sourceRow < destinationChild) { - toMove->setParent(destParentEntry, destinationChild + index - 1); + Entry *toMove = srcParentNodeEntry->children()[static_cast(sourceRow + index)]; + if (srcParentNodeEntry == destParentNodeEntry && sourceRow < destinationChild) { + toMove->setParent(destParentNodeEntry, destinationChild + index - 1); } else { - toMove->setParent(destParentEntry, destinationChild + index); + toMove->setParent(destParentNodeEntry, destinationChild + index); } } endMoveRows();