Fix some issues from SCA

Includes smaller issues like:
- reduce scope of local variables
- same expression on both sides of '||'
- remove unused local variable
- don't return bool if return value is int
- return result of function calls
- don't check unsigned for less than zero
- use static_cast instead of C-Style cast
- fix dereferencing after deallocation

And these two issues (please check them carefully)
- clarify expression with parentheses
- fix malloc memory leak