correct buffer length calculation