Fix Coverity Scan warnings

Warnings were:
* uninitialized varaibles
* out-of-bounds access
* dereference after null check