Improved string buffer handling

Handle NULL input string.
Remove extra buffer allocation.