mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-11 22:54:12 +02:00
a16964c055
Deleting a cable and pressing Ctrl+Z twice leaves the cable out of the project while AddCableCommand still stands and still thinks it has to clean that cable up. The next edit of any kind makes the stack throw the undone commands away: ~RemoveCableCommand frees the cable, and ~AddCableCommand frees the same memory again. A plain build hides it, AddressSanitizer reports it as a heap-use-after-free in addcablecommand.cpp (blocking review comment by ispyisail). AddCableCommand now holds the cable as a QPointer, which is what RemoveCableCommand already did: whoever of the two runs first frees it, the other one sees a null pointer and has nothing left to do, in either order of destruction. The destructor also stopped dereferencing project() without checking it, which it did on that very line. Covered by tst_cableundointegration, a probe linked against the application's objects the way the other integration probes are. It draws a cable, deletes it, undoes twice and pushes a further edit -- the sequence which used to crash. Verified in both directions: with the raw pointer the probe dies with SIGSEGV inside ~AddCableCommand called from QUndoStack::push, with the QPointer it prints its PASS line.
96 lines
3.0 KiB
C++
96 lines
3.0 KiB
C++
/*
|
|
Copyright 2006-2026 The QElectroTech Team
|
|
This file is part of QElectroTech.
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#ifndef ADDCABLECOMMAND_H
|
|
#define ADDCABLECOMMAND_H
|
|
|
|
#include "cable.h"
|
|
|
|
#include "../autoNum/numerotationcontext.h"
|
|
|
|
#include <QList>
|
|
#include <QPointer>
|
|
#include <QUndoCommand>
|
|
|
|
class CablePart;
|
|
class Diagram;
|
|
class QETProject;
|
|
|
|
/**
|
|
@brief Puts a drawn cable line on the folio, or takes it back off.
|
|
|
|
Both halves are one single step, because a cable line without its
|
|
cable is of no use to anybody: undoing has to take the line away and
|
|
leave nothing behind, and redoing has to put both back exactly as
|
|
they were.
|
|
*/
|
|
class AddCableCommand : public QUndoCommand
|
|
{
|
|
public:
|
|
AddCableCommand(Cable *cable,
|
|
CablePart *part,
|
|
Diagram *diagram,
|
|
bool own_cable,
|
|
const QList<CableCoreTaken> &taken = QList<CableCoreTaken>());
|
|
~AddCableCommand() override;
|
|
|
|
void undo() override;
|
|
void redo() override;
|
|
|
|
private:
|
|
QETProject *project() const;
|
|
|
|
private:
|
|
/**
|
|
The cable the line belongs to.
|
|
|
|
A pointer, not a raw one: RemoveCableCommand deletes the
|
|
cable when it goes, and both of them are torn down together
|
|
when the stack takes a new command. Whoever of the two runs
|
|
first frees the cable, so the other one must not follow it
|
|
into the freed memory -- QPointer goes null instead, and
|
|
this command then simply has nothing left to clean up.
|
|
*/
|
|
QPointer<Cable> m_cable;
|
|
///The line itself, owned by the folio once it is drawn
|
|
QPointer<CablePart> m_part;
|
|
QPointer<Diagram> m_diagram;
|
|
///Which section of the cable this line is
|
|
CablePartData m_part_data;
|
|
///The cores the line was giving to the cable, put back by redo
|
|
QList<CableCore> m_cores;
|
|
///The cores this line took away from other cables: redo takes
|
|
///them again, undo gives them back
|
|
QList<CableCoreTaken> m_taken;
|
|
///True when this command brought the cable into existence, so it
|
|
///is the one which has to take it away again
|
|
bool m_own_cable = false;
|
|
/**
|
|
When this cable took a number out of the project's numbering
|
|
rule, the rule as it was before it did and as it was after:
|
|
an empty key means the project numbers its cables by no rule
|
|
and nothing here applies. Handing the number out is part of
|
|
bringing the cable into being, so undo has to hand it back --
|
|
otherwise undoing a cable and drawing it once more would
|
|
leave a gap in the numbering for good.
|
|
*/
|
|
QString m_num_key;
|
|
NumerotationContext m_num_before;
|
|
};
|
|
|
|
#endif // ADDCABLECOMMAND_H
|