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 ...
随机推荐
- swift 设置string 中汉字中变色等处理代码
我们在做弹窗 或者显示label string的时候经常会用到字体变色 变大 等特殊处理, swift中提供一个函数 NSMutableAttributedString 使用方法简介 var main ...
- java 事件监听
事件监听实现: 三要素: 1.事件源(数据源,要处理的数据) 2.事件 (承载数据,传递信息并被监听) 3.监听器 (负责对数据的业务处理) --该开发用例采用了Spring的事件监听 1. 定义事 ...
- iOS沙盒机制介绍
一.沙盒机制 沙盒的概念:沙盒是每一个iOS应用程序都会自动创建的一个文件系统目录(文件夹),而且沙盒还具有独立.封闭.安全的特点. 沙盒机制 iOS中的沙盒不仅仅是一个文件目录,TA其实更是一种安全 ...
- 流程控制 if while for 已及数字类型 进制转换
一:if 语句 语法一:ifif 条件: code1 code1 code1 语法二:if ...else ... if 条件: code1 code1 code1else: code1 code1 ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- 28. Bad Influence of Western Diet 西式饮食的消极影响
28. Bad Influence of Western Diet 西式饮食的消极影响 ① The spread of Western eating habits around the world i ...
- JPEG Camer 图片上传
/* Linksprite */ #include <SoftwareSerial.h> #include <Ethernet.h> #include <SPI.h> ...
- Md5 util
Md5PasswordEncoder.java http://docs.spring.io/spring-security/site/docs/3.0.x/apidocs/org/springfram ...
- 对于java的命名规范(标识符)
java的命名规范基本都需要使用标识符来命名的: 标识符的介绍: 标识符作用: 给变量起名字的用的. 标识符的组成: 1.Unicode编码:包含大.小写字母,数字,汉字(不建议使用). 2.美元符 ...
- (并查集 建立关系)食物链 -- POJ-- 1182
链接: http://poj.org/problem?id=1182 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...