Skip to content

Commit

Permalink
Ensure defensive check of UBP previous data to fix crash - fixes #1662 (
Browse files Browse the repository at this point in the history
  • Loading branch information
wgauvin committed May 28, 2022
1 parent 3c6f767 commit 37860f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
27 changes: 21 additions & 6 deletions drivers/auxiliary/pegasus_upb.cpp
Expand Up @@ -1341,13 +1341,28 @@ bool PegasusUPB::sendFirmware()
//////////////////////////////////////////////////////////////////////
bool PegasusUPB::sensorUpdated(const std::vector<std::string> &result, uint8_t start, uint8_t end)
{
for (uint8_t index = start; index <= end; index++)
if (result[index] != lastSensorData[index])
if (lastSensorData.empty())
return true;

for (uint8_t index = start; index <= end; index++){
if (index >= lastSensorData.size() or result[index] != lastSensorData[index])
return true;
}

return false;
}

//////////////////////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////////////////////
bool PegasusUPB::stepperUpdated(const std::vector<std::string> &result, u_int8_t index)
{
if (lastStepperData.empty())
return true;

return index >= lastStepperData.size() or result[index] != lastSensorData[index];
}

//////////////////////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1641,13 +1656,13 @@ bool PegasusUPB::getStepperData()
IDSetNumber(&FocusAbsPosNP, nullptr);
IDSetNumber(&FocusRelPosNP, nullptr);
}
else if (result[0] != lastStepperData[0])
else if (stepperUpdated(result, 0))
IDSetNumber(&FocusAbsPosNP, nullptr);

FocusReverseS[INDI_ENABLED].s = (std::stoi(result[2]) == 1) ? ISS_ON : ISS_OFF;
FocusReverseS[INDI_DISABLED].s = (std::stoi(result[2]) == 1) ? ISS_OFF : ISS_ON;

if (result[2] != lastStepperData[2])
if (stepperUpdated(result, 1))
IDSetSwitch(&FocusReverseSP, nullptr);

uint16_t backlash = std::stoi(result[3]);
Expand All @@ -1656,7 +1671,7 @@ bool PegasusUPB::getStepperData()
FocusBacklashN[0].value = backlash;
FocusBacklashS[INDI_ENABLED].s = ISS_OFF;
FocusBacklashS[INDI_DISABLED].s = ISS_ON;
if (result[3] != lastStepperData[3])
if (stepperUpdated(result, 3))
{
IDSetSwitch(&FocusBacklashSP, nullptr);
IDSetNumber(&FocuserSettingsNP, nullptr);
Expand All @@ -1667,7 +1682,7 @@ bool PegasusUPB::getStepperData()
FocusBacklashS[INDI_ENABLED].s = ISS_ON;
FocusBacklashS[INDI_DISABLED].s = ISS_OFF;
FocusBacklashN[0].value = backlash;
if (result[3] != lastStepperData[3])
if (stepperUpdated(result, 3))
{
IDSetSwitch(&FocusBacklashSP, nullptr);
IDSetNumber(&FocuserSettingsNP, nullptr);
Expand Down
11 changes: 11 additions & 0 deletions drivers/auxiliary/pegasus_upb.h
Expand Up @@ -129,9 +129,20 @@ class PegasusUPB : public INDI::DefaultDevice, public INDI::FocuserInterface, pu

/**
* @return Return true if sensor data different from last data
*
* If the previous sensor data is empty then this will always
* return true.
*/
bool sensorUpdated(const std::vector<std::string> &result, uint8_t start, uint8_t end);

/**
* @return Return true if stepper data different from last data.
*
* If the previous stepper data is empty then this will always
* return true.
*/
bool stepperUpdated(const std::vector<std::string> &result, u_int8_t index);

int PortFD { -1 };
bool setupComplete { false };

Expand Down

0 comments on commit 37860f0

Please sign in to comment.