Fix bugs found by ccc-analyzer
More bug fixes
Test case for #1392
Fix #1392