add branch in strncpy to avoid reading from possible invalid memory