Interrupted by Nulls – The Daily WTF

Arrays in Cu are a unique collection of errors. The biggest is the idea of ​​zero termination. Null termination is not without its advantages: since you use a single byte to mark the end of a string, you can have strings of arbitrary length. No need to keep track of size and worry if your size variable is big enough to hold the end of the wire. No complicated data structures. Just “read until you find 0 bytes and you know you’re done.”

Of course, it is the root of many evils. Malicious inputs that do not have a null terminator, for example, are common exploits. It is so dangerous that everyone str* functions have strn* versions, which allow you to pass sizes to make sure you don’t overrun buffers.

Dmitry he sends us a simple example of someone who doesn’t fully understand it.

strcpy( buffer, string);
strcat( buffer, "\0");

The first line here copies the content string in buffer. It uses the null terminator to know when the copy can be stopped. Then we use strcatwhich scans the string for a null terminator and inserts a new string at the end – the new string is in this case zero terminator.

The programmer responsible for this protects against an array that does not have a null terminator by using functions that absolutely require to be null terminated.

C strings are difficult at best, but much more difficult when you don’t understand them.

[Advertisement]

Continuously monitor your servers for configuration changes and report if configuration drift occurs. Get started with Otter today!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *