What bothers me is the repeated code inside `check_and_adjust`. I'd almost rather always return an error message, possibly `NULL`, or always provide a buffer. A buffer on the stack would avoid the requirement for freeing memory.