Make sure that buffer is null terminated

That fixes issues like CID 1087769

Coverity: If the buffer is treated as a null terminated string in later operations, a buffer overflow or over-read may occur.