user21
user21

Reputation: 153

Strcmp returns different result under -fsanitize=address

I am using gcc (SUSE Linux) 7.2.1 20171020 to compile the following C program strcmp.c:

#include <stdio.h>
#include <string.h>

int main () {
   char str1[] = "e";
   char str2[] = "pi";
   int ret;

   ret = strcmp(str1, str2);

   printf("val: %i\n", ret);

   return(0);
}

I compile this with:

gcc -Wall -Wextra -fsanitize=address  strcmp.c

And when I run it I get:

./a.out
val: -1

This is a surprise to me, I would have expected a result of -11. And indeed I get that when I compile the program in the following way:

gcc -Wall -Wextra  strcmp.c

Why is giving the option -fsanitize=address changing the result?

Upvotes: 3

Views: 562

Answers (1)

yugr
yugr

Reputation: 21955

Asan provides a wrapper for strcmp to detect memory overflows. Their version returns only -1, 0 or +1 (which is still standards-compliant).

Upvotes: 4

Related Questions