From 946e0127b8f0ecb10c13c1649cf82c5ad711aee3 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Mon, 3 Dec 2007 23:53:56 +0000 Subject: [PATCH] went through and checked methods that take or return pointers or indexes and added safety precautions against bad parameters svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=744630 --- layouts/boxlayout.cpp | 18 +++++++++++++++--- layouts/flowlayout.cpp | 25 +++++++++++++++++++++---- layouts/nodelayout.cpp | 23 +++++++++++++++++------ 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/layouts/boxlayout.cpp b/layouts/boxlayout.cpp index ba030a7e6..327e78dc7 100644 --- a/layouts/boxlayout.cpp +++ b/layouts/boxlayout.cpp @@ -313,6 +313,10 @@ void BoxLayout::insertItem(int index, LayoutItem *item) void BoxLayout::addItem(LayoutItem *item) { + if (!item) { + return; + } + insertItem(-1, item); } @@ -325,8 +329,9 @@ void BoxLayout::removeItem(LayoutItem *item) item->unsetManagingLayout(this); d->children.removeAll(item); - if ( animator() ) - animator()->setCurrentState(item,LayoutAnimator::RemovedState); + if (animator()) { + animator()->setCurrentState(item, LayoutAnimator::RemovedState); + } updateGeometry(); } @@ -338,13 +343,20 @@ int BoxLayout::indexOf(LayoutItem *l) const LayoutItem *BoxLayout::itemAt(int i) const { + if (i >= d->children.count()) { + return 0; + } + return d->children[i]; } LayoutItem *BoxLayout::takeAt(int i) { - return d->children.takeAt(i); + if (i >= d->children.count()) { + return 0; + } + return d->children.takeAt(i); updateGeometry(); } diff --git a/layouts/flowlayout.cpp b/layouts/flowlayout.cpp index 30eda5d28..5758934d6 100644 --- a/layouts/flowlayout.cpp +++ b/layouts/flowlayout.cpp @@ -57,7 +57,7 @@ int FlowLayout::count() const void FlowLayout::addItem(LayoutItem* item) { - if (d->items.contains(item)) { + if (!item || d->items.contains(item)) { return; } @@ -71,6 +71,10 @@ void FlowLayout::addItem(LayoutItem* item) } void FlowLayout::removeItem(LayoutItem* item) { + if (!item) { + return; + } + item->unsetManagingLayout(this); d->items.removeAll(item); @@ -80,12 +84,21 @@ void FlowLayout::removeItem(LayoutItem* item) } int FlowLayout::indexOf(LayoutItem* item) const { + if (!item) { + return -1; + } + return d->items.indexOf(item); } LayoutItem* FlowLayout::itemAt(int i) const { + if (i >= d->items.count()) { + return 0; + } + return d->items[i]; } + QSizeF FlowLayout::sizeHint() const { // TODO A proper algorithm here @@ -98,8 +111,13 @@ QSizeF FlowLayout::sizeHint() const // testing return QSizeF(500,500); } + LayoutItem* FlowLayout::takeAt(int i) { + if (i >= d->items.count()) { + return 0; + } + return d->items.takeAt(i); } @@ -123,7 +141,7 @@ void FlowLayout::relayout() qreal totalWidth = 0; qreal totalHeight = 0; - foreach( LayoutItem *item , d->items ) { + foreach(LayoutItem *item , d->items) { totalWidth += item->sizeHint().width(); totalHeight += item->sizeHint().height(); } @@ -146,8 +164,7 @@ void FlowLayout::relayout() qreal rowHeight = 0; // lay the items out in left-to-right , top-to-bottom order - foreach( LayoutItem *item , d->items ) { - + foreach(LayoutItem *item , d->items) { const QSizeF& itemSize = item->sizeHint(); int columnSpan = (int)ceil(itemSize.width() / averageWidth); diff --git a/layouts/nodelayout.cpp b/layouts/nodelayout.cpp index 21356ee79..541e8ebc2 100644 --- a/layouts/nodelayout.cpp +++ b/layouts/nodelayout.cpp @@ -174,12 +174,12 @@ QSizeF NodeLayout::sizeHint() const return d->sizeHint; } -void NodeLayout::addItem (LayoutItem * item) +void NodeLayout::addItem(LayoutItem * item) { - NodeLayout::addItem (item, NodeCoordinate()); + NodeLayout::addItem(item, NodeCoordinate()); } -void NodeLayout::addItem (LayoutItem * item, NodeCoordinate topLeft, NodeCoordinate bottomRight) +void NodeLayout::addItem(LayoutItem * item, NodeCoordinate topLeft, NodeCoordinate bottomRight) { if (!item) { return; @@ -190,7 +190,7 @@ void NodeLayout::addItem (LayoutItem * item, NodeCoordinate topLeft, NodeCoordin d->calculateSizeHint(item); } -void NodeLayout::addItem (LayoutItem * item, NodeCoordinate node, qreal xr, qreal yr) +void NodeLayout::addItem(LayoutItem * item, NodeCoordinate node, qreal xr, qreal yr) { if (!item) { return; @@ -202,7 +202,7 @@ void NodeLayout::addItem (LayoutItem * item, NodeCoordinate node, qreal xr, qrea d->calculateSizeHint(item); } -void NodeLayout::removeItem (LayoutItem * item) +void NodeLayout::removeItem(LayoutItem * item) { if (!item) { return; @@ -220,17 +220,28 @@ int NodeLayout::count() const int NodeLayout::indexOf(LayoutItem * item) const { - if (!item) return -1; + if (!item) { + return -1; + } + return d->items.keys().indexOf(item); } LayoutItem * NodeLayout::itemAt(int i) const { + if (i >= d->items.count()) { + return 0; + } + return d->items.keys()[i]; } LayoutItem * NodeLayout::takeAt(int i) { + if (i >= d->items.count()) { + return 0; + } + LayoutItem * item = itemAt(i); removeItem(item); return item;