Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Weird bug: increase with conditional value makes value 0

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25987
Location: The netherlands

PostPosted: Sat Jul 18, 2009 6:16 pm    Post subject: Weird bug: increase with conditional value makes value 0 Reply with quote

Code:

bx_phy_address pAddr = vm->msr_bitmap_addr + (msr >> 3) + (op == VMX_VMEXIT_RDMSR) ? 0 : 2048;



vm->msr_bitmap_addr contains a valid value (not even near 0xffffffff)
(op == VMX_VMEXIT_RDMSR) will evaluate possitive

after execution, pAddr == 0

the following code:
Code:

bx_phy_address pAddr = vm->msr_bitmap_addr + (msr >> 3);
printf("1:pAddr =%x\n",pAddr );
pAddr=pAddr+(op == VMX_VMEXIT_RDMSR) ? 0 : 2048;
printf("2:pAddr =%x\n",pAddr );

shows :
1:pAddr=1f86b02f
2:pAddr=0

buf if I do this:
Code:

bx_phy_address pAddr = vm->msr_bitmap_addr + (msr >> 3) + ((op == VMX_VMEXIT_RDMSR) ? 0 : 2048);

It'll work fine

So, can anyone explain WHY this is happening?

GCC 4.4.0

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sat Jul 18, 2009 6:20 pm    Post subject: Reply with quote

is it checking the conditional value after it adds instead of before? in the second code its checking the value of just the second phrase(on my phone so idk the phrase). it seems like an order of operations issue? edit: im pretty sure its an operation order issue. its doing the conditional after the add and not doing the conditional then the add
_________________


Last edited by HomerSexual on Sat Jul 18, 2009 6:23 pm; edited 1 time in total
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jul 18, 2009 6:23 pm    Post subject: Reply with quote

it should be the same so i am assuming it is a problem with the compiler. what i would do is debug it in ollydbg and see what code is different when using each of those
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sat Jul 18, 2009 6:24 pm    Post subject: Reply with quote

well assuming the compiler is correct its order of ops. the code is NOT the same because the parenthesis make it so the condition happens before the addition
_________________
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25987
Location: The netherlands

PostPosted: Sat Jul 18, 2009 6:26 pm    Post subject: Reply with quote

yes, I think somehow the compiler is seeing it like this:

bx_phy_address pAddr = (vm->msr_bitmap_addr + (msr >> 3) + (op == VMX_VMEXIT_RDMSR)) ? 0 : 2048;

instead of
bx_phy_address pAddr = vm->msr_bitmap_addr + (msr >> 3) + ((op == VMX_VMEXIT_RDMSR) ? 0 : 2048);

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jul 18, 2009 6:42 pm    Post subject: Reply with quote

lol.. i was reading it like that too haha. main reason is that i didn't know you can do conditional assignment without binding the result to a variable.. ie. the last bit :

((op == VMX_VMEXIT_RDMSR) ? 0 : 2048)

the result is not bound, are you sure you can do that ?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25987
Location: The netherlands

PostPosted: Sat Jul 18, 2009 6:44 pm    Post subject: Reply with quote

yes, ((op == VMX_VMEXIT_RDMSR) ? 0 : 2048) works fine
if (op == VMX_VMEXIT_RDMSR) then that whole block gets replaced by a 0, else it becomes 2048

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Jul 18, 2009 6:54 pm    Post subject: Reply with quote

This happens on the MS compiler as well (and every compiler I would imagine)

Code:
DWORD test = 0x00102030;

test = test + (0xF3 == 0xFF) ? 0 : 1;
printf("%x", test);


test will become 0 here.

instead of this, which is what would make sense to me for it to boil down to... but mathematical precedence disagrees apparently.
Code:
test = test + ((0xF3 == 0xFF) ? 0 : 1);


http://en.wikipedia.org/wiki/Order_of_operations#Mathematical_precedence


Last edited by hcavolsdsadgadsg on Sat Jul 18, 2009 8:40 pm; edited 1 time in total
Back to top
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Sat Jul 18, 2009 7:47 pm    Post subject: Reply with quote

Slovach that is because your logic just failed you.. Wink

F3 will Never = FF.. so there for always 0..and I think something along this line should work..
Code:

op+= (op == VMX_VMEXIT_RDMSR ) ? 0 : 2048;

will tell the compiler what to do properly.. I think..

The reason I suspect to be behind it is that it compiler treats the whole thing after the '=' symbol as a statement and when its not equal to the condition 0's it out.. the solution above should prevent that Wink

regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Jul 18, 2009 8:32 pm    Post subject: Reply with quote

BanMe wrote:
Slovach that is because your logic just failed you.. Wink

F3 will Never = FF.. so there for always 0..and I think something along this line should work..

regards BanMe


I know and it was intentional as I was just using it as an example.

Code:
test = test + ((0xF3 == 0xFF) ? 0 : 1);
will have test get the 1 added to it as it will evaluate to false.

Code:
test += (0xF3 == 0xFF) ? 0 : 1;

this also will add the 1 as expected because of the +='s precedence.
Back to top
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Mon Jul 20, 2009 5:41 pm    Post subject: Reply with quote

this is true slovach Smile
but doing it that way prevents the z3r0ing out of the 'test' var..
so I win.. Smile woohooo..yay..wait.. idc..

regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites