Kostrahb
Kostrahb

Reputation: 743

What is wrong with adding null character to non null-terminated string?

Why I shouldn't add a null character to the end of a non null-terminated string like in this answer? I mean if I have a non null-terminated string and add null character to the end of the string, I now have a null-terminated string which should be good, right? Is there any security problem I don't see?

Here's the code in case the answer gets deleted:

char letters[SIZE + 1];  // Leave room for the null-terminator.

// ...
// Populate letters[].
// ...

letters[SIZE] = '\0';  // Null-terminate the array.

Upvotes: 1

Views: 1376

Answers (5)

Allan Deamon
Allan Deamon

Reputation: 497

You shouldn't use it, to avoid errors (or security holes) due mixing C/Pascal strings.

  • C style string: An array of char, terminated by NULL ('\0')
  • Pascal style string: a kind of structure, with a int with the size of the string, and an array with the string itself.

The Pascal style don't use in-band control, so it can use any char inside it, like NULL. C strings can't, as they use it as signaling control.

The problem is when you mix them, or assume one style when it's another. Or even try to convert between them.

Converting a C string to pascal would do no harm. But if you have a legit Pascal string with more then one NULL character, converting it to C style will cause problem, as it can't represent it.

A good example of this is the X.509 Null Char Exploit, where you could register a ssl certificate to:

www.mysimplesite.com\0www.bigbank.com

The X.509 certificate uses Pascal string, so this is valid. But when checking, the CA could use or assume C code or string style that just sees the first www.mysimplesite.com and signs the certificate. And some brosers parses this certificate as valid also for www.bigbank.com.

So, you CAN use it, but you SHOULD'NT, as it's risky to cause some bug or even a security breach.

More details and info: https://www.blackhat.com/presentations/bh-usa-09/MARLINSPIKE/BHUSA09-Marlinspike-DefeatSSL-SLIDES.pdf https://sites.google.com/site/cse825maninthemiddle/odds-and-ends/x-509-null-char-exploit

Upvotes: 0

Steve Summit
Steve Summit

Reputation: 48033

In general, there are two ways of keeping track of an array of some variable number of things:

  1. Use a terminator. Of course, this is the C approach to representing strings: an array of characters of some unknown size, with the actual string length given by a null terminator.
  2. Use an explicit count stored somewhere else. (As it happens, this is how Pascal traditionally represents strings.)

If you have an array containing a known but not null-terminated sequence of characters, and if you want to turn it into a proper null-terminated string, and if you know that the underlying array is allocated big enough to contain the null terminator, then yes, explicitly setting array[N] to '\0' is not only acceptable, it is the way to do it.

Bottom line: it's a fine technique (if the constraints are met). I don't know why that earlier answer was criticized and downvoted.

Upvotes: 0

chux
chux

Reputation: 154218

To be clear: a string in C always has one and only one null character - it is the last character of the string. A string is an array of characters. If an array of characters does not have a null character, it is not a string.

A string is a contiguous sequence of characters terminated by and including the first null character. C11dr 7.1.1 1

There is nothing wrong with adding a null character to an array of characters as OP coded.

This is a fine way to form a a string if:

  1. All the preceding characters are defined.

  2. String functions are not call until after a null character is written.

Upvotes: 1

Arton Dorneles
Arton Dorneles

Reputation: 1709

There is nothing technically wrong in terminating the string with \0 this way. However, the approaches you can use to populate the array before adding \0 are prone to error. Take a look in some situations:

  1. Suppose you decide to populate letters char by char. What happens if you forget to add some letters? What if you add more letters than the expected size?

  2. What if there are thousands of letters to populate the array?

  3. What if you need to populate letters with Unicode characters that (often) require more than one byte per symbol?

Of course you can address these situations very carefully but they still will be prone to error when maintaining the code.

Upvotes: 1

Andre Ahmed
Andre Ahmed

Reputation: 1889

to know the end of the string you must have a null terminated string, otherwise there is no way to know the end of the string

Upvotes: 2

Related Questions