Strcmp returns different result under -fsanitize=address

主宰稳场 提交于 2019-12-23 18:46:38

问题


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

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