Fix potential segmentation fault in error case