jeudi 11 septembre 2014

Is strlen on a string with unitialized values undefined behavior?


Vote count:

0




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?



asked 1 min ago







Is strlen on a string with unitialized values undefined behavior?

Aucun commentaire:

Enregistrer un commentaire