buffer: prevent string write offset overflow