When building with gcc and -Wnull-dereference, -O3 and -flto we get a
lot of warnings about potential null dereferences. Exiting instead of
returning NULL solves all of it.
If we cannot alloc memory, there is not much to do - exiting is the
easiest option.
The if clause is true if cache is NULL. man 3 fclose says:
The behaviour of fclose() is undefined if the stream parameter is an
illegal pointer, or is a descriptor already passed to a previous invo‐
cation of fclose().
Check if cache is NULL before calling fclose().