Do not patch the 0 terminator into the input string, instead safely copy the data to a separate buffer and add the terminator over there.