CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to prevent buffer overflows?

    I have declared some local variables in various places inside a function.
    I have checked the boundary checks option in the C++ option section.
    But sometimes some local variables will get overwritten by previous variables,
    so it becomes garbage. I don't know why, recently, this happens more often in the VS2010 compiler
    I can't recall what options I have changed to the project properties.
    How to prevent this problem from happening again?
    Code:
    // one set of bones per meshcontainer
    DWORD nBones = meshcontainer->dwNumBones;
    
    for (int iBone = 0; iBone < nBones; iBone++)
    Like this code snippet, iBone is overflowed by nBones, and it becomes an extremely large value...
    Thanks
    Jack
    Last edited by lucky6969b; November 13th, 2015 at 11:26 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to prevent buffer overflows?

    How to prevent this problem from happening again?
    Write correct code! Basically you have a bug(s) in the code. c++ doesn't do bounds testing (except for some methods with containers) or memory checks etc. The compiler just assumes you know what you're doing and lets you do it and generates the code!

    I don't know why, recently, this happens more often in the VS2010 compiler
    Because you have changed some code! The symptoms may show more with one compiler than another because of the difference in how code is generated - but the underlying code problem is still present!

    Having code/variables being overwritten unintentionally is a symptom of the code writing to areas of memory it which it shouldn't. Often if code is changed (even with some output statements for debugging) then the 'problem' can move. Common suspects include
    exceeding the bounds of a stack based array (especially that the last element is 1 less than the number of elements)
    for c-style strings not allocating an extra byte for the null terminator
    not allocating sufficient memory for a dynamic array
    freeing allocated memory and then still using the pointer
    having a pointer variable that isn't initialised correctly
    etc etc etc

    In large code bases these sort of errors can be difficult to track down. Have you considered using a static code analyser?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to prevent buffer overflows?

    Lucky, as a side note, kind of wondering why you use VS 2010 and don't use VS 2015?

    Care to post your reasons? If they are performance or usage related, I'd bet MS would like to here about them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured