Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[libc] Fix strcmp()/strncmp() to return proper values
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>
  • Loading branch information
ajyoung-oracle authored and mcb30 committed Jan 15, 2019
1 parent e226fec commit 3946aa9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/core/string.c
Expand Up @@ -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 )
Expand All @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/tests/string_test.c
Expand Up @@ -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 );
Expand Down

0 comments on commit 3946aa9

Please sign in to comment.