The variable block is initialized by the return value of Tip() which may be NULL.
The while loop condition takes this into account and checks for nullness before dereferencing.
If Tip() returns null, the while loop is never executed and the null pointer is dereferenced right after ( block->nHeight)
With this fix, there is a single check for nullness of block.
A JSONRPCError is thrown in this case.
After that, block is only assigned non-null values.
The code block->nHeight is never executed if block is NULL.
The while loop condition is also simplified.