The memory assigned to the local variable 'ptr' is freed
Run clang's static analyzer scan-build before to find the problem. Run scan-build after to verify the problem is fixed.
Just take out the if statement and it's fine.
Good catch. Since ptr is always either NULL or malloced memory, you can just pass it in to free(3) without the if statement. (free(3) will do nothing if the pointer is NULL.)
I will update and commit with the requested changes.
Abandon, since this has already been addressed.