How to understand buffer overflow scenario?

Brian Grasso 41 Reputation points
2021-02-16T06:03:03.527+00:00

Hi,
I am writing an application that handles very large numbers. When I run an overflow scenario I find that the returned values are sequential, and the returned value decreases as the overflow is increased... Can someone please explain this phenomenon to me? 68551-image.png The value from the line with two question marks plus one question mark = אבא (last valid returned value). As I raise אבא (in the lines that follow) to the values of the "containers" the overflow occurs (data type auto). My interest in the scenario is how the computer is processing the values, how the computer keeps them in order according to their values (even though they exceed the limitations of the data type), and why are the values related to the last "good" value where אבא is used. I can use a different base value (also Hebrew variables) and the same scenario plays out. When using multiple base variables each overflow relates back to the last "good" output associated with each variable. Trying to understand... Thanks!

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,109 questions
{count} votes

Accepted answer
  1. Viorel 117.3K Reputation points
    2021-02-16T10:11:05.02+00:00

    What programming language are you using, and can you show some code?

    In case of this C++ code:

    int x = 11250204;  
    int GS = 618;  
      
    auto r = x ^ GS; // result: 11249782  
    

    the result is correct, since ‘^’ means “Bitwise exclusive OR operator”: https://learn.microsoft.com/en-us/cpp/cpp/bitwise-exclusive-or-operator-hat. This is not an arithmetic nor buffer overflow.

    Maybe you wanted to use the pow function?


1 additional answer

Sort by: Most helpful
  1. David Lowndes 4,716 Reputation points
    2021-02-16T09:02:56.933+00:00

    I am writing an application that handles very large numbers. When I run an overflow scenario I find that the returned values are sequential, and the returned value decreases as the overflow is increased... Can someone please explain this phenomenon to me?

    It's just a consequence of exceeding the maximum value of whatever data type you're using. Most computers use 2's compliment numbers for integer values. You need to understand that and choose an appropriate data type for whatever values you're handling.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.