Fix #CR-1642
Add sanity checks over memory allocation
Changes to match coding style.