Fix some issues from SCA - v2

Update version of pull request #428. I've removed two of the patches, since they need to get reworked.

---
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
- use static_cast instead of C-Style cast
- fix dereferencing after deallocation

And these two issues (please check them carefully)
- fix malloc memory leak