Need to avoid buffer underflow before adding '\0'

Added fix to make sure we have enough space to add '\0' character at end.