Update the safefile() function.

This is a backport of the 3.0 security patch.