Is strlen a string with uninitialized undefined behavior values?

strlen returns the number of characters preceding the terminating null character. The strlen implementation might look like this:

 size_t strlen(const char * str) { const char *s; for (s = str; *s; ++s) {} return(s - str); } 

These are the specific dereferences of the implementation of s , where s may contain undefined values. This is equivalent to this:

 int a; int* p = &a; *p; 

So, for example, if this were done (which leads to the fact that strlen gives the wrong output):

 char buffer[10]; buffer[9] = '\0'; strlen(buffer); 

Is this behavior undefined?

+6
source share
4 answers

A call to the standard strlen function calls undefined behavior. DR 451 clarifies this:

library functions will exhibit undefined behavior when used in undefined values

For a more detailed discussion, see this topic .

+2
source

The behavior of the option you are showing is well defined in these circumstances.

  • The byte of an uninitialized array has all the undefined values, with the exception of the 10th element set to 0 .
  • Access to an undefined value will only be UB if the address of the underlying object is never accepted or if the value is a trap for the corresponding type.
  • Since this is an array and access to the elements of the array is through pointer arithmetic, the first case does not matter here.
  • Any char value can be accessed without UB; proposals for trap representations in the standard explicitly exclude all character types from it.
  • So the meanings you are dealing with are simply "unspecified."
  • Reading undefined values ​​may, according to some members of C standardization committee, produce different results each time, which some call "still." This property does not matter, since your function reads any such value no more than once.
  • Thus, your access to the elements of the array gives you an arbitrary but valid char value.
  • You are sure that your for loop will be stopped last at position 9 , so you won’t overflow your array.

Thus, no β€œbad” things beyond the visible can happen if you use your particular version of the function. But calling a function that produces unspecified results, of course, you do not want to see in real code. Something like this here leads to very subtle errors, and you should avoid it by all means.

+2
source

No, this is not undefined behavior. The strlen function will be stopped until the end of the buffer. If your strlen function refers to a buffer [10], then yes, it is undefined.

Of course, this will be an unexpected behavior, since most of the buffer contains random data. "Undefined" is a special word for people who write language standards. This means that anything can happen, including memory errors or exit from the program. By surprise, I mean that he is not sure what the programmer wanted. On some runs, the strlen result may be 3 or may be 10.

+1
source

Yes, this behavior is undefined. From the draft C11 standard, Β§J.2 "Undefined behavior":

Undefined behavior in the following cases:

...

The value of an object with automatic storage duration is used when it is undefined.

0
source

Source: https://habr.com/ru/post/975164/


All Articles