fix bad free
do not return negative value on error

just log it
use sxi_rand_pseudo_bytes instead of rand
set f to null just in case