[#2314] add checks for string length before copying into a buffer