Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the null-terminator placed arbitrarily in a string terminate it?

The null character or null-terminator (\0) is to be used to terminate a contiguous sequence of characters. I find that in C, I can add the character into a string at a random position and the string will be cut off from that point. For example:

char * s = "Hello\0World";

will result in s being equal to the string "Hello". In JavaScript, however, this is not the case:

var s = "Hello\0World";

The above won't work as expected. s will be equal to the string "HelloWorld".

Why doesn't this work?

like image 623
David G Avatar asked Sep 26 '12 14:09

David G


People also ask

Why do strings have to be null-terminated?

Character encodings Null-terminated strings require that the encoding does not use a zero byte (0x00) anywhere; therefore it is not possible to store every possible ASCII or UTF-8 string. However, it is common to store the subset of ASCII or UTF-8 – every character except NUL – in null-terminated strings.

What happens if you don't null terminate a string?

Many library functions accept a string or wide string argument with the constraint that the string they receive is properly null-terminated. Passing a character sequence or wide character sequence that is not null-terminated to such a function can result in accessing memory that is outside the bounds of the object.

What is a null terminator in a string?

The null character indicates the end of the string. Such strings are called null-terminated strings. The null terminator of a multibyte string consists of one byte whose value is 0. The null terminator of a wide-character string consists of one gl_wchar_t character whose value is 0.

Does string add null terminator?

c_str() a null terminator will be included in the return from this method. It's also worth saying that you can include a null character in a string just like any other character. and not 5 1 as you might expect if null characters had a special meaning for strings. If you call temp.


3 Answers

JavaScript does not use NULL terminated strings, while C does.

Javascript strings are stored by keeping track of the characters and the length separately instead of trying to assume that a NULL marks the end of the string.

like image 152
loganfsmyth Avatar answered Oct 08 '22 19:10

loganfsmyth


The C string still points to an address in memory where "Hello\0World" is stored, only that most string handling functions considers 0 end of string. For some functions you must pass a string length argument, but most simply read until they find the null byte. In memory the string is actually "Hello\0World\0".

A JavaScript engine cannot determine string length by looking for a null byte, since you in such a case wouldn't ever be able to have a nullbyte inside a string. There's probably something about that in the specs. The engine must instead storing the length of the string separately, and then read that many characters from memory whenever you access the string.

And how to properly parse and store the size of buffers is something scripting languages usually try to hide from the user. That's half the purpose of scripting, to not require the programmer to worry about adding 0 to created character buffers and or storing string length separately so that string handling functions don't print a bunch of random characters outside your buffer looking for a nullbyte...

So exactly how does a JavaScript string behave? I don't know, it's probably up to the engine to describe its properties in depth. As long as you interface with the object like the specification says, it can be implemented in whatever manner, using structs for buffer and length, using a translation character for 0, using a linked list of characters, etc...

like image 20
sapht Avatar answered Oct 08 '22 20:10

sapht


In Javascript a NULL byte in a String is simply a NULL byte in a string. If you want truncate the string

var s = "Hello\0World".split("\0").shift();

but in this case I think it not need to disturb the null byte :)

like image 43
Luca Rainone Avatar answered Oct 08 '22 19:10

Luca Rainone