Is strlen on a string with uninitialized values undefined behavior?

情到浓时终转凉″ 提交于 2019-11-28 03:54:34

问题


strlen returns the number of characters that precede the terminating null character. An implementation of strlen might look like this:

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

This particular implementation dereferences s, where s may contain indeterminate values. It's equivalent to this:

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

So for example if one were to do this (which causes strlen to give an incorrect output):

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

Is it undefined behavior?


回答1:


Calling the standard function strlen causes undefined behaviour. DR 451 clarifies this:

library functions will exhibit undefined behavior when used on indeterminate values

For a more in-depth discussion see this thread.




回答2:


The behavior of the variant that you are showing is well defined under these circumstances.

  • The bytes of the uninitialized array have all indeterminate values, with exception of the 10th element that you set to 0.
  • Accessing an indeterminate value would only be UB if the address of the underlying object would be never taken or if the value is a trap for the corresponding type.
  • Since this is an array and access to array elements is through pointer arithmetic, the first case is not relevant, here.
  • Any char value can be accessed without UB, the clauses about trap representations in the standard explicitly exclude all character types from that.
  • Thus the values that you are dealing with are simply "unspecified".
  • Reading unspecified values may according to some members of the C standards committee give different results each time, what some call a "whobly" state or so. This property is not relevant, here, since your function reads any such value at most once.
  • So your access to the array elements gives you any arbitrary but valid char value.
  • You are sure that your for loop stops at latest at position 9, so you will not overrun your array.

So no "bad" things beyond the visible may happen if you use your specific version of the function. But having a function call that produces unspecified results is certainly nothing you want to see in real code. Something like this here leads to very subtle bugs, and you should avoid it by all means.




回答3:


No, it's not undefined behavior. Your strlen function will stop before the end of the buffer. If your strlen function referenced buffer[10], then, yes that is undefined.

It certainly will be unexpected behavior, since most of buffer contains random data. "Undefined" is special word for people writing language standards. It means that anything could happen, including memory faults or exiting the program. By unexpected, I mean that it sure not what the programmer wanted to happen. On some runs, the result of strlen could be 3 or it could be 10.




回答4:


Yes, it's undefined behaviour. From the draft C11 standard, §J.2 "Undefined behavior":

The behavior is undefined in the following circumstances:

...

The value of an object with automatic storage duration is used while it is indeterminate.



来源:https://stackoverflow.com/questions/25799336/is-strlen-on-a-string-with-uninitialized-values-undefined-behavior

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!