Strcmp在-fsanitize = address下返回不同的结果 [英] Strcmp returns different result under -fsanitize=address

查看:145
本文介绍了Strcmp在-fsanitize = address下返回不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gcc(SUSE Linux)7.2.1 20171020编译以下C程序strcmp.c:

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

这对我来说是一个惊喜,我原本以为-11。确实,当我以以下方式编译程序时,我得到了:

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

为什么给出选项 -fsanitize = address 结果?

推荐答案

Asan为 strcmp 提供了包装器以检测内存溢出。 其版本仅返回-1、0或+1(仍符合标准)。

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

这篇关于Strcmp在-fsanitize = address下返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆