When will strcmp not return -1, 0 or 1?

百般思念 提交于 2019-11-28 14:08:26

In c99 standard

7.21.4.2 The strcmp function

" The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2. "

It means standard doesn't guarantee about the -1 ,0or 1 it may vary according to operating systems.

The value you are getting is the difference of w and h which is 15.

In your case hello and world so 'h'-'w' = -15 < 0 and that's why strcmp retuns -15

Is there something in the C standard that defines what the return values are besides less than, greater than, or equal to zero?

No. The tightest constraint is that it should be zero, less than zero or more than zero, as specified in the documentation of this particular function.

If not, what does the standard implementation do?

There's no such thing as "the standard implementation". Even if there was, it would probably just

return zero, less than zero or more than zero;

:-)

Is the return value consistent across the Linux, Windows and the BSDs?

I can confirm that it's consistent across Linux and OS X as of 10.7.4 (specifically, it's -1, 0 or +1). I have no idea about Windows, but I bet Microsoft guys use -2 and +3 just to break code :P

Also, let me also point out that you have completely misunderstood what the code does.

I found this code (taken from this question) that relies on the values of strcmp being something other than -1, 0 and 1 (it uses the return value in qsort). To me, this is terrible style and depends on undocumented features.

No, it actually doesn't. The C standard library is designed with consistency and ease of use in mind. That is, what qsort() requires is that its comparator function returns a negative or a positive number or zero - exactly what strcmp() is guaranteed to do. So this is not "terrible style", it's perfectly standards-conformant code which does not depend upon undocumented features.

•Is there something in the C standard that defines what the return values are besides less than, greater than, or equal to zero? If not, what does the standard implementation do?

No, as you mentioned yourself the man says less than, equal to, or greater than zero and that's the standard as well.

•Is the return value consistent across the Linux, Windows and the BSDs?

No.

On linux (openSuse 12.1, kernel 3.1) with gcc I get -15/15 depending on if test1 or test2 is first.
On Windows 7 (VS 2010) I get -1/1.

Based on the loose defination of strcmp() both are fine.


...that relies on the values of strcmp being something other than -1, 0 and 1 (it uses the return value in qsort).

An interesting side not for you... if you take a look at the man for qsort() the example there is pretty much the same as the bell code you posted using strcmp(), reason being the compairator function that qsort() requires is actually a great fit for the return from strcmp():

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

In reality, the return value of strcmp is likely to be the difference between the values of the bytes at the first position that differed, simply because returning this difference is a lot more efficient than doing an additional conditional branch to convert it to -1 or 1. Unfortunately, some broken software has been known to assume the result fits in 8 bits, leading to serious vulnerabilities. In short, you should never use anything but the sign of the result.

For details on the issues, read the article I linked above:

https://communities.coverity.com/blogs/security/2012/07/19/more-defects-like-the-mysql-memcmp-vulnerability

In this page:

The strcmp() function compares the string pointed to by s1 to the string pointed to by s2. The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared.

Here is an implementation of strcmp in FreeBSD.

#include <string.h>

/*
 * Compare strings.
 */
int
strcmp(s1, s2)
    register const char *s1, *s2;
{
    while (*s1 == *s2++)
        if (*s1++ == 0)
            return (0);
    return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}

From the manual page:

RETURN VALUE The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

It only specifies that it is greater or less than 0, doesn't say anything about specific values, those are implementation specific i suppose.

CONFORMING TO SVr4, 4.3BSD, C89, C99. This says in which standards it is included. The function must exist and behave as specified, but the specification doesn't say anything about the actual returned values, so you can't rely on them.

There's nothing in the C standard that talks about the value returned by strcmp() (that is, other than the sign of that value):

7.21.4.2 The strcmp function

Synopsis

#include <string.h>
int strcmp(const char *s1, const char *s2);

Description

The strcmp function compares the string pointed to by s1 to the string pointed to by s2.

Returns

The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

It is therefore pretty clear that using anything other than the sign of the returned value is a poor practice.

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