Issues comparing unsigned long's in RAD studio 2009

1

My office is using RAD Studio 2009 for some legacy support on products, however we recently came across the following behaviour and would like to know if anyone has seen this, and knows the cause.

When comparing a pair of unsigned long's we end up getting results where only the least significant 2 bytes are used for the comparison.

A simplified example:

unsigned long Val1 = 0xDC00;
unsigned long Val2 = 0x5A550;

if(Val1 > Val2)
{
//Something horribly wrong
printf("things are bad- Val1:%x > Val2:%x", Val1, Val2);
}

In our code the above scenario is resulting in "something horribly wrong"

I have managed to isolate the issue by changing Val1 to 0xA551 and it still fails, yet 0xA549 will pass. This says that the greater than operator is only checking the lowest order 2 bytes of the unsigned long. However it always prints the correct values in the printf.

The code is running on a Windows XP 32 bit machine, and the sample is a simplified example of the issue, but it captures the behaviour we are seeing.

Responses (8)
  • Accepted Answer

    Tuesday, April 25 2017, 10:28 PM - #Permalink
    0

    I eventually found the issue, it was a bad cast that was hidden in a separate section of code not at all related to where the fault was happening... or so I thought.

     

    Thanks Vytra and Roger for your help.

    The reply is currently minimized Show
  • Accepted Answer

    Friday, April 21 2017, 04:23 AM - #Permalink
    1

    Hi,

    - very slow, but good enough RAD studio tool for this.

    Thanks.

     

    The reply is currently minimized Show
  • Accepted Answer

    Thursday, April 20 2017, 08:05 PM - #Permalink
    1

    Thanks Vytra,

    I have built a simple project using the same configuration as the project that is exhibiting this issue, and the problem is not present. I've started adding in levels of complexity to try and replicate the issue as seen in my other project to try and identify what is causing the problem, but no luck so far. 

    I'll keep an eye out for the potential causes you have mentioned, but this is quite a large project, so I suspect I will need to use some form of profiling tool to find the problem if it is memory or addressing.

     

    Regards

    The reply is currently minimized Show
  • Accepted Answer

    Thursday, April 20 2017, 05:55 AM - #Permalink
    1

    Hi,

    there somewhere can be a lot of case:

    1. You return from the function the dynamic local variable address.

    2. You are using the address pointer to the different variable types.

    3. You have exceeded the boundaries of the some object in the long cycles.

    4. You executed the wrong address multiplicity flare structures and unions.

    5. You use not initialized memory pointers.

    6. The memory array indexing exceeded threshold.

    all of this can corrupt any data, and sometimes even a some code.

     

    Recommendation: Try completely empty project, as demonstrated Roger.

     

    The reply is currently minimized Show
  • Accepted Answer

    Thursday, April 20 2017, 03:00 AM - #Permalink
    1

    Hi Vytra,

    We aren't using 3rd party code, could you explain what you mean by addressing methods? The values are addressed the exact same way in outputting to a log as they are in the comparison, yet we get different results.

    Regards

     

    The reply is currently minimized Show
  • Accepted Answer

    Thursday, April 20 2017, 01:50 AM - #Permalink
    1

    Hi,

    - usually this type of error obtained from addressing methods errors in your code or third-party code.

    Thanks.

     

    The reply is currently minimized Show
  • Accepted Answer

    Wednesday, April 19 2017, 07:00 PM - #Permalink
    1

    Hi Roger,

     

    Update 3 and 4 are installed.

    This specific issue has only been noticed in one specific location in our application, in a library that is included in the overall project, and the variables come from higher in the call stack, and one of them specifically is an element of an array of structs, which are in a master struct.

     

    Regards

    The reply is currently minimized Show
  • Accepted Answer

    Wednesday, April 19 2017, 05:07 AM - #Permalink
    1

    Hi DDTT,

    I've just used C++ Builder 2009, update 3 to create a new VCL forms project, put a single TLabel on the form and (only) entered code into the form constructor as follows:

     

    __fastcall TForm1::TForm1(TComponent* Owner)

       : TForm(Owner)

    {

    unsigned long Val1 = 0xDC00;

    unsigned long Val2 = 0x5A550;

     

    if(Val1 > Val2)

    {

        //Something horribly wrong

        Label1->Caption = "Wrong";

    }

    else {

       Label1->Caption = "Ok";

    }

    }

     

    And this compiles and runs giving "ok"

    First question: have you got C++ / Delphi 2009 update 3?  (look in "help/about").

    Regards, Roger

    The reply is currently minimized Show
Your Reply

Please login to post a reply