3 ms·
If that's the case, there should be a #define for that length. If not, they should be using strlen("_shutdown") or sizeof("_shutdown")+1 There's really no exc
by xxpor 3y ago
If that's the case, there should be a #define for that length.
If not, they should be using strlen("_shutdown") or sizeof("_shutdown")+1
There's really no excuse for a magic number there.
- NikkiA 3y agoyou'd want strlen("_shutdown")+1, since checking without the null-terminator means that you match with strings like '_shutdown123'. It's one of the reasons why I detest the strcmp warning, as strcmp is perfectly safe given a literal string as a comparison, the comparison will drop out as not matching whenever the first NUL happens, and a literal string acts the same as specifying 'n'. Blind usage of strncmp leads to perhaps even more bugs than blind usage of strcmp.
- smcl 3y agoThat would introduce a bug. See the following: #include <stdio.h> int main() { printf("%lu\n", strlen("_shutdown")); printf("%lu\n", sizeof("_shutdown")); return 0; } This prints the numbers 9 and 10 (on gcc I have). So if you used `sizeof(...)+1` when calculating the length argument for strcmp you'd be comparing the 9-byte string, the null terminator and one character of random nonsense.