Fix some edge cases in buffer.c