Resolve multiple resource leaks, out-of-bound access, memory allocation problems

This pull request resolves several resource leaks, an out-of-bound access, several memory allocation problems (correct usage of realloc) as well as some problems that might occur caused by uninitialized variables. For a more detailed description please refer to the single commit messages. Since I am new to the code base of seafile I would like you to double-check 3e28a6f and 0b88b96 if they fix the issue in the intented way.