The problem with strcmp
is that sometimes, if by mistake, arguments that are passed are not valid C-strings (meaning that p1 or p2 is not terminated with a null character i.e. not NULL-terminated String), then, strcmp continues comparing until it reaches non-accessible memory and crashes or sometimes results to an unexpected behaviour.
Using strncmp
you can limit the search, so that it doesn't reach non-accessible memory.
But, from that, it should not be concluded that strcmp
is insecure to use. Both the functions work well in the way they are intended to work. Programmer should read man
page for that function before using it and must be sincere enough while passing parameters to such library functions.
You can also read THIS which contains an almost similar question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…