I am trying to guard my apps not to crush when my pointers are out bounds
here is what i did
bool MyClass::isValid(void)
{
bool bOutofBounds = false;
t_TimeRecordHeader* ptrTimeHeader = locateTimeRecordHeader(); // will give me a pointer to my time record which contian my checksum
uint16 checksum = ptrTimeHeader->timCheckSum;
uint16 calChecksum = 0;
uint16 sum = 0;
uint8* ptrData = (uint8*) &ptrTimeHeader->sysTimeHigh;
for (int i = 0; i < (timeBlkSize() - 2) ; i++, ptrData++)
{
try
{
sum = sum + (uint16)*ptrData;
}
catch (...)
{
bOutofBounds = true; // hoping i could catch any exeption
break;
}
}
calChecksum = sum;
sum = sum + checksum;
if (bOutofBounds) // in the event of an exeption where ptrData is really far (outofbounds) this will never get executed this puzzled me?
{
m_ErrorString = tr("Detected checksum pointer of Bounds");
return false;
}
if (sum)
{
m_ErrorString = tr("Detected Checksum Error in Frame's Time Record\n Recorded: %1\n Calculated %2")
.arg(checksum).arg(calChecksum);
return false;
}
return true;
}
bool MyClass::isValid(void)
{
bool bOutofBounds = false;
t_TimeRecordHeader* ptrTimeHeader = locateTimeRecordHeader(); // will give me a pointer to my time record which contian my checksum
uint16 checksum = ptrTimeHeader->timCheckSum;
uint16 calChecksum = 0;
uint16 sum = 0;
uint8* ptrData = (uint8*) &ptrTimeHeader->sysTimeHigh;
for (int i = 0; i < (timeBlkSize() - 2) ; i++, ptrData++)
{
try
{
sum = sum + (uint16)*ptrData;
}
catch (...)
{
bOutofBounds = true; // hoping i could catch any exeption
break;
}
}
calChecksum = sum;
sum = sum + checksum;
if (bOutofBounds) // in the event of an exeption where ptrData is really far (outofbounds) this will never get executed this puzzled me?
{
m_ErrorString = tr("Detected checksum pointer of Bounds");
return false;
}
if (sum)
{
m_ErrorString = tr("Detected Checksum Error in Frame's Time Record\n Recorded: %1\n Calculated %2")
.arg(checksum).arg(calChecksum);
return false;
}
return true;
}
To copy to clipboard, switch view to plain text mode
is this right or wrong , please enlightened me
baray98
Bookmarks