Free variable on error exit

Signed-off-by: Michal Čihař <michal@cihar.com>
Check null return value

Signed-off-by: Michal Čihař <michal@cihar.com>