Add a null check for 'file' before dereferencing it and also rework the error handling to be a bit more sane. This issue was found by Smatch. Signed-off-by: Andrew Goodbody <andrew.goodbody@linaro.org>