Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced IF statement in C

Tags:

c

if-statement

I've just reverse engineered a binary with IDA and loaded Hex Ray to check out a specific function. The generate C source code contains the following if statement:

LP_ret_GlobalLock_1 = GlobalLock(hMem);
LP_ret_GlobalLock_2 = LP_ret_GlobalLock_1;
...
if ( !LP_ret_GlobalLock_1 || (v9 = *(_DWORD *)(v6 + 4), *(_DWORD *)v6 = LP_ret_GlobalLock_2, v9 < 0) )

I'm not sure to completely understand the following part:

(v9 = *(_DWORD *)(v6 + 4), *(_DWORD *)v6 = LP_ret_GlobalLock_2, v9 < 0)

v9 is initialised as v6 + 4; but then v6 is modified to be a pointer LP_ret_GlobalLock_2 and finally v9 is check for being less than 0. Is that correct? When calculating v9 what value is used for v6? The LP_ret_GlobalLock_2 or the previous value?

like image 958
Benjamin Avatar asked Feb 20 '26 23:02

Benjamin


1 Answers

I guess you are asking about the comma operator. It evaluates the expression before the comma, then the expression after the comma, and the result of the whole thing is the result of the second expression.

So it first does v9 = *(_DWORD *)(v6 + 4), then *(_DWORD *)v6 = LP_ret_GlobalLock_2, and then v9 < 0. The result is the result of v9 < 0, after the first two expressions have been evaluated.

I understand that you got this via reverse engineering. I would never use the comma operator with side effects inside an if-statement like that when writing code myself; it's too obfuscated.

like image 120
Jesper Avatar answered Feb 23 '26 17:02

Jesper