问题
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?
回答1:
Asan provides a wrapper for strcmp
to detect memory overflows. Their version returns only -1, 0 or +1 (which is still standards-compliant).
来源:https://stackoverflow.com/questions/49213586/strcmp-returns-different-result-under-fsanitize-address