“undefined behavior:
behavior for which this International Standard imposes no requirements.”

example of Undefined Behavior

Use of an uninitialized variable

Misaligned pointers

Access to an object past end of lifetime

The Compiler and Undefined Behavior

为了提高效率,编译器有做了一些假设,如下是几个例子。

编译器不同优化会引起不同的结果

优化1,先去掉多余的 NULL 检查,再去掉多余的语句

优化2,先去掉多余的语句,再去掉多余的 NULL 检查

这样子,仅仅是不同的优化顺序,最终得到的结果是不同的。

此外,还有不同的优化等级、不同的设备、不同的编译器版本等。

Undefined Behavior 的特征

  1. 不可预测
  2. 可能影响整个程序
  3. 会导致潜伏的 bug
  4. 是很多安全问题的原因

如何解决

工具篇

  • Compiler
  • Static Analyzer
  • Address Sanitizer
  • Thread Sanitizer
  • Undefined Behavior Sanitizer

tips

  • Modernize your project (Editor → Validate Settings)

  • Run the Static Analyzer
    把开关打开。

  • Use the Runtime Sanitizers
    不同的工具可以解决不同的问题。

参考

Understanding Undefined Behavior的更多相关文章

  1. UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

    Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...

  2. 【转载】C语言中的undefined behavior/unspecified behavior - 序

    嗷嗷的话: 这都是一些细枝末节的东西,我想不做编译器的话,大部分都很难碰到.研究学习这些只是出于对C语言一种偏执狂. 写出来是为了找到和我一样的偏执狂. 在随后的的文章中,首先我写一写191种unde ...

  3. 情景剧:C/C++中的未定义行为(undefined behavior)

    写在前面 本文尝试以情景剧的方式,轻松.直观地解释C/C++中未定义行为(undefined behavior)的概念.设计动机.优缺点等内容1,希望读者能够通过阅读本文,对undefined beh ...

  4. C++ Undefined Behavior 详细列表

    Undefined Behavior,即未定义的行为,指程序不可预测的执行效果,一般由错误的代码实现引起.出于效率.兼容性等多方面原因,语言标准不便于定义错误程序的明确行为,而是将其统称为" ...

  5. About Undefined Behavior[译文]

    原文:blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 人们偶尔会问为什么LLVM的汇编代码有时会在优化器打开时产生SIGT ...

  6. C语言中的undefined behavior

    参考: http://www.cnblogs.com/aoaoblogs/archive/2010/08/31/1813982.html

  7. IAR运行程序警告:undefined behavior: the order of volatile accesses is undefined in this statement

    运算符两边都是volatile变量的警告,将IAR设置如下即可:

  8. Understanding C++ Modules In C++20 (1)

    Compiling evironment: linux (ubuntu 16.04)+ gcc-10.2. The Post will clarify and discuss what modules ...

  9. WARNING: Calls to any function that may require a gradient calculation inside a conditional block may return undefined results

    GLES2.0: Some device will give a warning on compling shaders(yet the compling will succeed), and the ...

随机推荐

  1. 数据库链接 mybatis spring data jpa 两种方式

    jdbc mybatis                     spring data jpa dao service webservice jaxrs     jaxws  springmvc w ...

  2. js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意

    js  和 css 中 不能使用 jsp  页面中一些 标签 和 java 代码等,应注意 如 ${ }  <%%>  等

  3. jQuery load和unload函数使用

    实例 当用户点击链接离开本页时,弹出一个消息框: $(window).unload(function(){ alert("Goodbye!"); }); 亲自试一试 定义和用法 当 ...

  4. 【JS】点击页面判断是否安装app并打开,否则跳转下载的方法

    应用场景 App产品在运营推广上有一个需求,就是要求可以让用户在访问我们的推广网页时,就可以判断出这个用户手机上是否安装了我们的App,如果安装了则可以直接在网页上打开,否则就引导用户前往下载.从而形 ...

  5. vue webkit-box-orient: vertical打包线上不显示

    emmm……觉得不科学啊,写了几个vue的网站,限制超出行数省略号.结果发现放到线上,全都瓦特了.反复检查本地跑起来没错,代码没少,偏偏在线上的时候就是缺了vue -webkit-box-orient ...

  6. 解决sea_born和matplotlib画图中文显示的问题

    #以下解决mtpl中文显示问题 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] #以下解决seaborn中文编码报错问 ...

  7. 13.A={1,2,3,5}和为10的问题

    题目:集合A={1,2,3,5},从中任取几个数相加等于10,并打印各得哪几个数?补充参照:http://www.cnblogs.com/tinaluo/p/5294341.html上午弄明白了幂集的 ...

  8. How To Change the Supplier Bank Account Masking in UI (Doc ID 877074.1)

      Give Feedback...           How To Change the Supplier Bank Account Masking in UI (Doc ID 877074.1) ...

  9. Android-Git命令行操作

    Git命令行操作,在Mac上使用的话,Mac会自带了Git,直接在终端或者iTerm都可以执行Git命令操作:    Git命令行操作,在Windows系统电脑上使用的话,需要安装Git,安装好Git ...

  10. Maven发布和管理项目

    1 什么是Maven? 如果没有Maven,你可能不得不经历下面的过程: 1 如果使用了spring,去spring的官网下载jar包:如果使用hibernate,去hibernate的官网下载Jar ...