Fix a segfault with weird magic hashes and stuff

RT #50412

Have to check that there is a value before calling SvGETMAGIC
Try to make the code a bit more robust against NULLs