]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[libc] Fix strcmp()/strncmp() to return proper values
authorAaron Young <Aaron.Young@oracle.com>
Wed, 9 Jan 2019 19:35:39 +0000 (11:35 -0800)
committerMichael Brown <mcb30@ipxe.org>
Tue, 15 Jan 2019 14:12:27 +0000 (14:12 +0000)
Fix strcmp() and strncmp() to return proper standard positive/negative
values for unequal strings.  Current implementation is backwards
(i.e. the functions are returning negative when should be positive and
vice-versa).

Currently all consumers of these functions only check the return value
for ==0 or !=0 and so we can safely change the implementation without
breaking things.

Signed-off-by: Aaron Young <Aaron.Young@oracle.com>
Modified-by: Michael Brown <mcb30@ipxe.org>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/string.c
src/tests/string_test.c

index 5a185e6356e799dc5447aa8e113f375518c58f52..5bd9dae8b41279f4935757a7e18e0210ca314c5f 100644 (file)
@@ -173,7 +173,7 @@ int strncmp ( const char *first, const char *second, size_t max ) {
        int diff;
 
        for ( ; max-- ; first_bytes++, second_bytes++ ) {
-               diff = ( *second_bytes - *first_bytes );
+               diff = ( *first_bytes - *second_bytes );
                if ( diff )
                        return diff;
                if ( ! *first_bytes )
@@ -195,8 +195,8 @@ int strcasecmp ( const char *first, const char *second ) {
        int diff;
 
        for ( ; ; first_bytes++, second_bytes++ ) {
-               diff = ( toupper ( *second_bytes ) -
-                        toupper ( *first_bytes ) );
+               diff = ( toupper ( *first_bytes ) -
+                        toupper ( *second_bytes ) );
                if ( diff )
                        return diff;
                if ( ! *first_bytes )
index 4693b5f65309c2d715584dcfd24751d31d983f23..a66501da303c12a420f04e84e82e72ca75ed8dd1 100644 (file)
@@ -88,6 +88,7 @@ static void string_test_exec ( void ) {
        ok ( strcmp ( "Hello", "hello" ) != 0 );
        ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
        ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
+       ok ( strcmp ( "abc", "def" ) < 0 );
 
        /* Test strncmp() */
        ok ( strncmp ( "", "", 0 ) == 0 );