Fix CID 1134594

And add a check to prevent NULL dereference