local.c: avoid a NULL dereference on error.

A re-commit of 150f4c79e9f7143a57ec876a8cd98f3c2c172269
minus the return statements which were a major part of
upstream commit 6bb7900e512f2391a957801723d326c3ed3450a7