Skip to content

Commit dc3ebb4

Browse files
committed
check for null pointer dereference
1 parent cc69ef5 commit dc3ebb4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/commands.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
namespace Cmd {
3232

33-
GUI::MainScene * Cmd::Undo::s_scene = nullptr;
33+
GUI::MainScene * Undo::s_scene = nullptr;
3434

3535
Undo::Undo( QUndoCommand *parent, State *st)
3636
: QUndoCommand(parent), current_state_(st)
@@ -44,8 +44,12 @@ void Undo::undo()
4444
//qDebug() << Q_FUNC_INFO;
4545

4646
*current_state_ = *prev_state_; // set
47-
scene()->removeAllItems();
48-
scene()->addGraphicItems( prev_state_.get() );
47+
auto *scn = scene();
48+
if (scn==nullptr) {
49+
return;
50+
}
51+
scn->removeAllItems();
52+
scn->addGraphicItems( prev_state_.get() );
4953
}
5054

5155

@@ -54,8 +58,12 @@ void Undo::redo()
5458
//qDebug() << Q_FUNC_INFO;
5559

5660
*current_state_ = *next_state_; // copy content
57-
scene()->removeAllItems();
58-
scene()->addGraphicItems( next_state_.get() );
61+
auto *scn = scene();
62+
if (scn==nullptr) {
63+
return;
64+
}
65+
scn->removeAllItems();
66+
scn->addGraphicItems( next_state_.get() );
5967
}
6068

6169

@@ -78,8 +86,13 @@ DeleteSelection::DeleteSelection( State *st,
7886
QUndoCommand *parent) : Undo(parent, st)
7987
{
8088
// qDebug() << Q_FUNC_INFO ;
81-
setText( QStringLiteral("delete selected item%1")
82-
.arg( scene()->selectedItems().size()==1 ? "" : "s") );
89+
auto *scn = scene();
90+
if (scn==nullptr) {
91+
return;
92+
}
93+
94+
const QString suffix = scn->selectedItems().size()==1 ? QStringLiteral("") : QStringLiteral("s");
95+
setText( QStringLiteral("delete selected item%1").arg(suffix) );
8396

8497
prev_state_ = std::move(p);
8598
next_state_ = std::move(n);

0 commit comments

Comments
 (0)