https://bugs.winehq.org/show_bug.cgi?id=51899
--- Comment #11 from Bruni earns.61@gmail.com --- (In reply to O. Nykyforchyn from comment #10)
This expression tests whether the supplied handle is valid, I see no way to make the code with it simpler.
How about this code? Also, where can one see if your patch was accepted? (before a new wine release)
DC_ATTR *get_dc_attr( HDC hdc ) { DWORD handle_type_bit_mask = 0x1f0000; DWORD type = gdi_handle_type( hdc ); DC_ATTR *dc_attr = get_gdi_client_ptr( hdc, 0 );
if (!type) return dc_attr->disabled ? NULL : dc_attr;
if ((type & handle_type_bit_mask) != NTGDI_OBJ_DC) { SetLastError( ERROR_INVALID_HANDLE ); return NULL; }
if (!dc_attr) { SetLastError( ERROR_INVALID_HANDLE ); return NULL; }
return dc_attr->disabled ? NULL : dc_attr; }
Explanation:
In (type && (type & 0x1f0000) != NTGDI_OBJ_DC),
!= has higher precedence than &&, both have left-to-right associativity
That is, it is grouped as (type && ((type & 0x1f0000) != NTGDI_OBJ_DC)).
If we let
if ((type && (type & 0x1f0000) != NTGDI_OBJ_DC) || !(dc_attr = get_gdi_client_ptr( hdc, 0 )))
be
if ((x && y) || !z))
where `type` stands for x
`(type & 0x1f0000) != NTGDI_OBJ_DC` stands for y
`(dc_attr = get_gdi_client_ptr( hdc, 0 ))` stands for z
then
Both && and || are affected by "Short-circuit evaluation" rule.
So in if ((x && y) || !z) { body } expression,
it's enough for both x and y to be true or for z to be false to execute if's body.
So z can be split into separate if, which can be placed after original if, copying its body.
Which results in if (x && y) { body } if (!z) { body }
In if (x && y) { body } expression,
it's enough for x to be false to not execute if's body. And one can immediately return in our case, where if's body is the last block before `return`.
So one can decompose it as follows
if (!x) return if (y) { body } if (!z) { body }