C和C++安全编码读书笔记1
(1)type safety
Another characteristic of C that is worth mentioning is the lack of type safety. Type safety consists of two attributes: preservation and progress [Pfenning 04]. Preservation dictates that if a variable x has type t and x evaluates to a value v, then v also has type t. Progress tells us that evaluation of an expression does not get stuck in any unexpected way: either we have a value (and are done), or there is a way to proceed. In general, type safety implies that any operation on a particular type results in another value of that type. C was derived from two typeless languages and still shows many characteristics of a typeless or weakly typed language. For example, it is possible to use an explicit cast in C to convert from a pointer to one type to a pointer to a different type. If the resulting pointer is dereferenced, the results are undefined. Operations can legally act on signed and unsigned integers of differing lengths using implicit conversions and producing unrepresentable results. This lack of type safety leads to a wide range of security flaws and vulnerabilities.
由于C语言存在(隐式/显式)类型转换,所以容易产生许多安全问题。
(2)Unbounded String Copies
防止cin越界的方法:
1. #include <iostream>
2. int main(void) {
3. char buf[12];
4. cin.width(12);
5. cin >> buf;
6. cout << "echo: " << buf << endl;
7. }
The extraction operation can be limited to a specified number of characters (thereby avoiding the possibility of out-of-bounds write) if the field width inherited member (ios_base::width) is set to a value greater than 0. In this case, the extraction ends one character before the count of characters extracted reaches the value of field width leaving space for the ending null character. After a call to this extraction operation the value of the field width is reset to 0.
需要注意这个width是一次性的,每次调用 >> 后都会归0。
代码进行PCLint检查时常常提示strcpy不安全,有机会要整改一下。
(3)Off-by-One Errors
经典程序
1. int main(int argc, char* argv[]) {
2. char source[10];
3. strcpy(source, "0123456789");
4. char *dest = (char *)malloc(strlen(source));
5. for (int i=1; i <= 11; i++) {
6. dest[i] = source[i];
7. }
8. dest[i] = '\0';//据书上说VC++ 6.0能编译通过
9. printf("dest = %s", dest);
10. }
The source character array (declared on line 2) is 10 bytes long, but strcpy() (line 3) copies 11 bytes, including a one-byte terminating null character.
The malloc() function (line 4) allocates memory on the heap of the length of the source string. However, the value returned by strlen() does not account for the null byte.
The index value i in the for loop (line 5) starts at 1, but the first position in a C array is indexed by 0.
The ending condition for the loop (line 5) is i <= 11. This means the loop will iterate one more time than the programmer likely intended.
The assignment on line 8 also causes an out-of-bounds write.
(4)Null-Termination Errors
依赖于编译器如何分配空间,了解各种编译器貌似一个大坑,还是别跳了,老老实实注意不要忘了'\0’
C和C++安全编码读书笔记1的更多相关文章
- 汉字与区位码互转(天天使用Delphi的String存储的是内码,Windows记事本存储的文件也是内码),几个常见汉字的各种编码,utf8与unicode的编码在线查询,附有读书笔记 good
汉=BABA(内码)=-A0A0=2626(区位码)字=D7D6(内码)=-A0A0=5554(区位码) 各种编码查询表:http://bm.kdd.cc/ 汉(记住它,以后碰到内存里的数值,就会有敏 ...
- 《JavaScript高级程序设计》读书笔记--前言
起因 web编程过程使用javascript时感觉很吃力,效率很低.根本原因在于对javascript整个知识体系不熟,看来需要找些书脑补一下,同时欢迎众网友监督. 大神推荐书籍 看了博客大神们推荐的 ...
- <读书笔记> 代码整洁之道
概述 1.本文档的内容主要来源于书籍<代码整洁之道>作者Robert C.Martin,属于读书笔记. 2.软件质量,不仅依赖于架构和项目管理,而且与代码质量紧密相关,本书提出一 ...
- <读书笔记>软件调试之道 :问题的核心-修复后的反思
声明:本文档的内容主要来源于书籍<软件调试修炼之道>作者Paul Butcher,属于读书笔记.欢迎转载! ---------------------------------------- ...
- <读书笔记>软件调试之道 :问题的核心-诊断
声明:本文档的内容主要来源于书籍<软件调试修炼之道>作者Paul Butcher,属于读书笔记. 不要急于动手! 尽管可以利用各种工具和技术以及软件自身查找缺陷,但是你最重要的财富是你的智 ...
- <读书笔记>软件调试之道 :问题的核心-重现问题
声明:本文档的内容主要来源于书籍<软件调试修炼之道>作者Paul Butcher,属于读书笔记. 重现第一,提问第二 问题重现是实证过程的最强大武器,如果不能重现问题,你也无法证明修复了它 ...
- WPF,Silverlight与XAML读书笔记第四十八 - Silverlight网络与通讯
说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 这一部分我们重点讨论下Silverlight ...
- WPF,Silverlight与XAML读书笔记第四十五 - 外观效果之模板
说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 模板允许用任何东西完全替换一个元素的可视树, ...
- 《你必须知道的.NET》读书笔记三:体验OO之美
此篇已收录至<你必须知道的.Net>读书笔记目录贴,点击访问该目录可以获取更多内容. 一.依赖也是哲学 (1)本质诠释:“不要调用我们,我们会调用你” (2)依赖和耦合: ①无依赖,无耦合 ...
随机推荐
- 动态子类化CComboBox以得到子控件EDIT及LISTBOX
动态子类化CComboBox以得到子控件EDIT及LISTBOX Joise.LI写于2004-4-6 ComboBox是比较常用的一个控件,有三种样式:CBS_SIMPLE(简单),CBS_DROP ...
- 优步uber司机怎么注册不了?注册优步司机问题要点
第一,可能是你的车型不符全要求,看是不是5年内的车型,同时要求车价8W以上:第二,你的驾驶年限不够,要求驾驶证年限1年以上的,如果不够的怎么办,告诉你个方法,PS啊!优步可查不了车管所的系统,所以这类 ...
- 【每周一译】愚蠢的指标:Java中使用最多的关键字
此翻译纯属个人爱好,由于水平所限,翻译质量可能较低.网络上可能存在其它翻译版本,原文地址:http://blog.jooq.org/2013/08/26/silly-metrics-the-most- ...
- 【LeetCode】Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- ASP.NET Web编程
runat="server"直接回交服务器,处理数据,又以数据加密后的hidden属性的input控件插入回去,实现表单的状态保存 ruant="server" ...
- 将Oracle数据库导出为txt格式
将Oracle数据库导出为txt格式: 方法1: 对于Windows系统,可以采用以下方式: 选择控制面板-->管理工具-->数据源(ODBC),添加一个新的数据源(系统或用户DSN均可) ...
- ExtJs目录说明
Ext开发包目录结构说明builds目录为ExtJS压缩后的代码docs目录为ExtJS的文档examples目录中是官方的演示示例locale是多国语言的资源文件, 其中ext - lang - z ...
- java线程的使用(Runnable)
在实际项目开发过程中,线程是经常要用到的,特别是为了不影响项目的运行效果. 以下就以实际项目中的简单例子来介绍: public class SystemRedisInfoController exte ...
- La=LaULb (单链表)
#include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...
- BZOJ 3156: 防御准备( dp + 斜率优化 )
dp(i)表示处理完[i,n]且i是放守卫塔的最小费用. dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N) 然后斜率优化 ------------ ...