Fix warning about possible null pointer deref

This commit is contained in:
Martchus 2021-12-11 23:48:59 +01:00
parent 2428c3e656
commit cfb4423f79
1 changed files with 16 additions and 12 deletions

View File

@ -451,40 +451,44 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
} }
#endif #endif
// check validation of specified arguments: source and destination parent entries need to be node entries // 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 // if (sourceRow < 0 || count <= 0) {
|| entry(destinationParent)->type() != EntryType::Node) { 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; return false;
} }
// determine the source parent entry and dest parent entry as node entries // determine the source parent entry and dest parent entry as node entries
auto *const srcParentEntry = static_cast<NodeEntry *>(sourceParent.internalPointer()); auto *const srcParentNodeEntry = static_cast<NodeEntry *>(sourceParent.internalPointer());
auto *const destParentEntry = static_cast<NodeEntry *>(destinationParent.internalPointer()); auto *const destParentNodeEntry = static_cast<NodeEntry *>(destinationParent.internalPointer());
#if CPP_UTILITIES_DEBUG_BUILD #if CPP_UTILITIES_DEBUG_BUILD
cout << "destinationChild: " << destinationChild << endl; cout << "destinationChild: " << destinationChild << endl;
#endif #endif
// source rows must be within the valid range // source rows must be within the valid range
if (static_cast<size_t>(sourceRow + count) > srcParentEntry->children().size() if (static_cast<size_t>(sourceRow + count) > srcParentNodeEntry->children().size()
// if source and destination parent are the same the destination child mustn't be in the source range // 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; return false;
} }
// do not move a row to one of its own children! -> check before // do not move a row to one of its own children! -> check before
for (int index = 0; index < count; ++index) { for (int index = 0; index < count; ++index) {
Entry *const toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)]; Entry *const toMove = srcParentNodeEntry->children()[static_cast<size_t>(sourceRow + index)];
if (toMove->type() != EntryType::Node) { if (toMove->type() != EntryType::Node) {
continue; continue;
} }
if (toMove == destParentEntry || destParentEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) { if (toMove == destParentNodeEntry || destParentNodeEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) {
return false; return false;
} }
} }
// actually perform the move operation // actually perform the move operation
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild); beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
for (int index = 0; index < count; ++index) { for (int index = 0; index < count; ++index) {
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)]; Entry *toMove = srcParentNodeEntry->children()[static_cast<size_t>(sourceRow + index)];
if (srcParentEntry == destParentEntry && sourceRow < destinationChild) { if (srcParentNodeEntry == destParentNodeEntry && sourceRow < destinationChild) {
toMove->setParent(destParentEntry, destinationChild + index - 1); toMove->setParent(destParentNodeEntry, destinationChild + index - 1);
} else { } else {
toMove->setParent(destParentEntry, destinationChild + index); toMove->setParent(destParentNodeEntry, destinationChild + index);
} }
} }
endMoveRows(); endMoveRows();