FIX: unchecked mallocs and Python operations

Rewrote _tmp_string_t handling in buffer.c to use the fact that
realloc(NULL, n) is equivalent to malloc(n). There are still some
unchecked cases in this file.

Also changed some arguments to const and removed casts on malloc
return values; malloc returns void*, which is converted to any
other pointer automatically. Casting the return value of malloc
is more likely to hide bugs than to fix anything.
MAINT: remove unused goto label and useless casts
FIX: some more casts in ufunc_object

Introduced a helper function to get rid of repetitive code.
FIX don't explicitly cast void* when final type is known
Merge pull request #3655 from larsmans/c-fixes

FIX: unchecked mallocs and various other things