Hacker News new | ask | show | jobs
by shawxe 2067 days ago
> Sure, and if you do strlen(s) without checking if there is actually a NUL first then:

>> finding the end of a string may place you into invalid memory if there are missing trailing bytes.

You're really losing me here. Either you have a NUL terminated string or you already know the length of the string or you have no way of determining the length of the string that exists in memory that it is safe for you to access and what you have is garbage.

Just because data is "valid UTF-8" doesn't make it safe to read. If the end of your string isn't marked and you don't know how long the string is already, it's over.

1 comments

Or you reserved the string ahead of time or it was reused and not zeroed properly or there is more than 1 null in the data and copying must be done with care.

Again, it's doable, or you can validate the string once (including more than "it's valid encoding") and make a ton of safe assumptions later instead of each thing that interacts with the string. UTF or not.