NO

Author Topic: Bug in "if statement" if ( ulong1 - ulong2 <= 0  (Read 3181 times)

kia

  • Guest
Bug in "if statement" if ( ulong1 - ulong2 <= 0
« on: September 06, 2006, 10:12:52 AM »
Hi,
Version 4.50

Bug in "if statement" when operands type is "unsigned long"  
and result is negative, if type is "signed long" or result >= 0 all right.

    if ( ulong1 - ulong2 <= 0 ) {}

The assembler code:
1. "signed long" ok
    if( ulong1 - ulong2 <= 0 )
 8B 45 FC         mov         eax,dword ptr [ulong1]
 2B 45 F8         sub         eax,dword ptr [ulong2]
 83 F8 00         cmp         eax,0
 7F 14              jg         address

2. "unsigned long" error
    if( ulong1 - ulong2 <= 0 )
 8B 45 FC         mov         eax,dword ptr [ulong1]
 2B 45 F8         sub         eax,dword ptr [ulong2]
 83 F8 00         cmp         eax,0
 75 14              jne         address
but "sub eax,dword ptr [ulong2]" not set zf flag
if result is negative.

Please test.
The example generated error:
CCFLAGS= -Tx86-coff -Ot -Gd -Ze -Zx

 
Code: [Select]

#include <stdio.h>

int main()
{
signed long   niTst1 = 0;
signed long   niTst2 = 10;
unsigned long   nuiTst1 = 0;
unsigned long   nuiTst2 = 10;



printf("\tTest PellesC\n");

if ( niTst1 - niTst2 <= 0 )
{
printf( "\tAll is correct.\t%i <= 0 YES!\n", niTst1 - niTst2 );
}

if ( nuiTst1 - nuiTst2 <= 0 )
{
printf( "\tAll is correct.\t%i <= 0 YES!\n", nuiTst1 - nuiTst2 );
}

printf( "\tError!\t%i <= 0 NO?!\n", nuiTst1 - nuiTst2 );
getchar();

return 0;
}

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Bug in "if statement" if ( ulong1 - ulong2 <= 0
« Reply #1 on: September 07, 2006, 10:56:58 AM »
Huh?! Exactly how did you came to the conclusion that an unsigned result can be negative?! It works just fine...
/Pelle

kia

  • Guest
Bug in "if statement" if ( ulong1 - ulong2 <= 0
« Reply #2 on: September 07, 2006, 02:51:10 PM »
Quote from: "Pelle"
Huh?! Exactly how did you came to the conclusion that an unsigned result can be negative?! It works just fine...


Oops!  :oops: #-o
You are right, sorry.

This is not my code, it was "size_t" type in source, but not
obviously "unsigned long", it was confused me.

I must be more attentive before write to forum, sorry :oops:

Thanks for your work.