Avoid segfaults in replace-buffer-contents with large buffers