undo: fix RemoveEvent if we can't find the event

We check for the event, but then access it anyway even if it
doesn't exist. Should not happen, but let's be safe.

Found by Coverity.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-08-14 09:08:44 +02:00 committed by Michael Keller
parent 5c6ca2c1ce
commit e305546046

View file

@ -115,19 +115,21 @@ void RenameEvent::undoit()
redoit(); redoit();
} }
RemoveEvent::RemoveEvent(struct dive *d, int dcNr, int idx) : EventBase(d, dcNr), RemoveEvent::RemoveEvent(struct dive *d, int dcNr, int idxIn) : EventBase(d, dcNr),
idx(idx), cylinder(-1) idx(idxIn), cylinder(-1)
{ {
struct divecomputer *dc = d->get_dc(dcNr); struct divecomputer *dc = d->get_dc(dcNr);
event *ev = get_event(dc, idx); event *ev = get_event(dc, idx);
if (ev && (ev->type == SAMPLE_EVENT_GASCHANGE2 || ev->type == SAMPLE_EVENT_GASCHANGE)) if (!ev)
idx = -1;
if (ev->type == SAMPLE_EVENT_GASCHANGE2 || ev->type == SAMPLE_EVENT_GASCHANGE)
cylinder = ev->gas.index; cylinder = ev->gas.index;
setText(Command::Base::tr("Remove %1 event").arg(ev->name.c_str())); setText(Command::Base::tr("Remove %1 event").arg(ev->name.c_str()));
} }
bool RemoveEvent::workToBeDone() bool RemoveEvent::workToBeDone()
{ {
return true; return idx >= 0;
} }
void RemoveEvent::redoit() void RemoveEvent::redoit()