Results 1 to 5 of 5

Thread: taking pointer out of bounds nicely

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default taking pointer out of bounds nicely

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: taking pointer out of bounds nicely

    I've been reading your post over and over and I think I still don't understand it. What exactly are you trying to do? If you read a chunk of memory which is not allocated by your application, it will either not crash at all (and you'll receive garbage) or it will crash without throwing any exceptions because the operating system will kill it because of a page fault. So I'm not sure what you are trying to do, but most likely it won't work.

    You can use some memory patrolling tools (like Valgrind for Linux) to inspect whether you are reading/writing past the chunk boundaries so that you can correct your code.

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: taking pointer out of bounds nicely

    here is my story

    my app is trying to read a datafile. this file is divided into frames and each frames contiains its length and checksum of the frame.

    so i read a frame into a memory and check its validity before anything else.

    i noticed that if i have an invalid frame checking the checksum will sometimes crash. i found that this is due to the fact that the length is invalid so i ended up reading memory out of my area.

    what i want to know is that HOW CAN I PROTECT MY APPS FROM THIS SCENARIO?

    i thought i could use try and catch but if it won't give any exception as you said, then there must some other way...

    please enlightened me
    baray98

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: taking pointer out of bounds nicely

    Can't you note the size of the frame and not read more? You might for example fill all the bytes from where you copy the frame to with zeroes (provided that zero can't be seen inside the frame, use some other symbol otherwise) and then check the input (after writing the frame) for the character and stop reading if you spot it?

    Basically if you know the frame length and you see that the frame is shorter, you can ignore it instantly without checking the checksum.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: taking pointer out of bounds nicely

    Quote Originally Posted by baray98 View Post
    i found that this is due to the fact that the length is invalid so i ended up reading memory out of my area.
    How did you define sysTimeHigh field and timeBlkSize()?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.