Understanding Undefined Behavior
“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 的特征
- 不可预测
- 可能影响整个程序
- 会导致潜伏的 bug
- 是很多安全问题的原因
如何解决
工具篇
- 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的更多相关文章
- 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 ...
- 【转载】C语言中的undefined behavior/unspecified behavior - 序
嗷嗷的话: 这都是一些细枝末节的东西,我想不做编译器的话,大部分都很难碰到.研究学习这些只是出于对C语言一种偏执狂. 写出来是为了找到和我一样的偏执狂. 在随后的的文章中,首先我写一写191种unde ...
- 情景剧:C/C++中的未定义行为(undefined behavior)
写在前面 本文尝试以情景剧的方式,轻松.直观地解释C/C++中未定义行为(undefined behavior)的概念.设计动机.优缺点等内容1,希望读者能够通过阅读本文,对undefined beh ...
- C++ Undefined Behavior 详细列表
Undefined Behavior,即未定义的行为,指程序不可预测的执行效果,一般由错误的代码实现引起.出于效率.兼容性等多方面原因,语言标准不便于定义错误程序的明确行为,而是将其统称为" ...
- About Undefined Behavior[译文]
原文:blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 人们偶尔会问为什么LLVM的汇编代码有时会在优化器打开时产生SIGT ...
- C语言中的undefined behavior
参考: http://www.cnblogs.com/aoaoblogs/archive/2010/08/31/1813982.html
- IAR运行程序警告:undefined behavior: the order of volatile accesses is undefined in this statement
运算符两边都是volatile变量的警告,将IAR设置如下即可:
- Understanding C++ Modules In C++20 (1)
Compiling evironment: linux (ubuntu 16.04)+ gcc-10.2. The Post will clarify and discuss what modules ...
- 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 ...
随机推荐
- tty linux 打开和设置范例
http://bbs.csdn.net/topics/340184140 /************************************************************** ...
- 2018.08.27 lucky(模拟)
描述 Ly 喜欢幸运数字,众所周知,幸运数字就是数字位上只有 4 和 7 的数字. 但是本题的幸运序列和幸运数字完全没关系,就是一个非常非常普通的序列. 哈哈,是 不是感觉被耍了,没错,你就是被耍了. ...
- Java 增强 for 循环
Java 增强 for 循环 Java5 引入了一种主要用于数组的增强型 for 循环. Java 增强 for 循环语法格式如下: for(声明语句 : 表达式) { //代码句子 } 声明语句:声 ...
- LDA汇总
1.Blei的LDA代码(C):http://www.cs.princeton.edu/~blei/lda-c/index.html2.D.Bei的主页:http://www.cs.princeton ...
- struts2 file
JavaBean 中: private File[] pic; private String[] picContentType; private String [] picFileName; sett ...
- OpenGL + MFC
OpenGL超级宝典(中文版) 2001年 本书是一本完整而详尽的关于OpenGL的参考书,全书分为四部分:第一部分“OpenGL导言”介绍3D图形学的基本原理,读者将在此学会构造使用OpenGL的程 ...
- 团队合作项目—(GG队)
团队展示 一.队名:GG 二.队员信息 队员 学号 叶尚文(队长) 3116008802 蔡晓晴 3216008808 杜婷萱 3216008809 龙剑初 3116004647 于泽浩 311600 ...
- 读《深入理解Windows Phone 8.1 UI控件编程》1.4.3 框架的应用示例:自定义弹出窗口有感
前些天买了园子里林政老师的两本 WP8.1 的书籍.毕竟想要学得深入的话,还是得弄本书跟着前辈走的. 今天读到 1.4.3 节——框架的应用示例:自定义弹出窗口这一小节.总的来说,就是弄一个像 Mes ...
- navicat远程连接oracle
本机没有oracle,这个软件太大了. 想要远程连接oracle,本地不安装oracle的话是不行的,我们安装一个oracle instance client,然后配置navicat就ok了. 下载i ...
- Eclipse 4.2 failed to start after TEE is installed
--------------- VM Arguments--------------- jvm_args: -Dosgi.requiredJavaVersion=1.6 -Dhelp.lucene ...