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 ...
随机推荐
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- azman使用笔记
azman.msc 打开 容易冲突,要用lock 效率问题,可用sql azman
- iCn3D结构查看器的实现方法
iCn3D Structure Viewer:iCn3D结构查看器 演示效果如下: 上面只是一个Basic UI的演示,如果要Advanced UI的话可以去NCBI官网看API
- redis学习-事务命令
multi:开启事务 exec:提交事务 discard:取消事务 1.开启事务之后,每次执行命令之后,都要先进入事务队列中,只有在执行 exec之后才开始执行 2.开启事务之后,每次执行命令之后,都 ...
- 【转】关于编译链接——gcc/g++
添加运行时共享库目录 运行使用共享库的程序需要加载共享库(不同于G++ 编译时指定的链接库),添加共享库的步骤: 修改文件 /etc/ld.so.conf 添加共享库目录 运行 ldconfig 同步 ...
- UVa 1596 Bug Hunt (STL栈)
题意:给定两种操作,一种是定义一个数组,另一种是赋值,让你找出哪一步时出错了,出错只有两种,一种是数组越界,另一种是访问未定义变量. 析:当初看到这个题时,感觉好麻烦啊,然后就放过去了,而现在要重新回 ...
- Java文件复制删除操作合集
import java.io.*; public class FileOperate { public FileOperate() { } /** * 新建目录 * @param folderPath ...
- html5 录制/保存视频/录音
国内这方面的资料真少的可怜,翻出去一搜一大把,推荐一个github的插件库,非常强大,支持各种各样的录制方式. 下载完成之后,引用对应的js立即可以使用. <script src="R ...
- 基于SketchUp和Unity3D的虚拟场景漫游和场景互动
这是上学期的一次课程作业,难度不高但是也一并记录下来,偷懒地拿课程报告改改发上来. 课程要求:使用sketchUp建模,在Unity3D中实现场景漫游和场景互动. 知识点:建模.官方第一人称控制器.网 ...
- 通过oracle闪回查看表中值的变更履历信息
http://www.oracle.com/technetwork/cn/articles/week1-10gdba-093837-zhs.html 得到电影而不是图片:闪回版本查询 不需要设置,立即 ...