Ensure the string isn't null terminated before len, if this happens it's likely an error calculating the length argument. Since the length is passed to memcpy it could result in reading outside `str` bounds. There was one case where the string was duplicated then grew by 1 byte afterwards. Replace this with an allocation since it's an uncommon operation, not especially useful to support.