I am trying to guard my apps not to crush when my pointers are out bounds
here is what i did

Qt Code:
  1. bool MyClass::isValid(void)
  2. {
  3. bool bOutofBounds = false;
  4. t_TimeRecordHeader* ptrTimeHeader = locateTimeRecordHeader(); // will give me a pointer to my time record which contian my checksum
  5. uint16 checksum = ptrTimeHeader->timCheckSum;
  6. uint16 calChecksum = 0;
  7. uint16 sum = 0;
  8. uint8* ptrData = (uint8*) &ptrTimeHeader->sysTimeHigh;
  9. for (int i = 0; i < (timeBlkSize() - 2) ; i++, ptrData++)
  10. {
  11. try
  12. {
  13. sum = sum + (uint16)*ptrData;
  14. }
  15. catch (...)
  16. {
  17. bOutofBounds = true; // hoping i could catch any exeption
  18. break;
  19. }
  20. }
  21. calChecksum = sum;
  22. sum = sum + checksum;
  23. if (bOutofBounds) // in the event of an exeption where ptrData is really far (outofbounds) this will never get executed this puzzled me?
  24. {
  25. m_ErrorString = tr("Detected checksum pointer of Bounds");
  26. return false;
  27. }
  28. if (sum)
  29. {
  30. m_ErrorString = tr("Detected Checksum Error in Frame's Time Record\n Recorded: %1\n Calculated %2")
  31. .arg(checksum).arg(calChecksum);
  32. return false;
  33. }
  34. return true;
  35. }
To copy to clipboard, switch view to plain text mode 

is this right or wrong , please enlightened me

baray98