There might be some communication issues here.
The point I think vlad is trying to make is that using strlen() on the value you pass to strncmp is
likely a bug because it does not do what you expect.
|
strncmp( "foo", "foobar", strlen("foo") ); // <- probably wrong
|
This is bad because strlen gives you the length of "foo", which is 3. Therefore strncmp will only compare 3 characters. Since the first 3 characters of each string are identical... strncmp will return 0 indicating they match, even though they clearly do not.
The 3rd parameter should not be the length of the string, but should be the
size of the buffer. The whole point of the param is to prevent buffer overflow... so giving it a strlen value completely defeats the point.
EDIT:
@ciphermagi
isValid = strncmp(str1, str2, MAX(strlen(str1), strlen(str2));
This is bad code. The whole point of strncmp is to be "safer" by stopping from going out of bounds of a given buffer.
In that regard strlen() is "unsafe" because it iterates over the entire string without regard to where the buffer actually ends.
So in effect... doing strlen on
BOTH strings is
even less safe and slower than just doing a regular, 'deprecated' strcmp.
Putting the calls in a MAX macro is
even worse still because it will ensure strlen is called multiple times on the same string, resulting in the string being unnecessarily parsed twice.
Do yourself a favor and just use strcmp.