2020-01-13 Splint C语言内存调试

0. 快速回忆

Linux 安装 splint

yum install -y splint

// 检测文件 *.c
splint *.c 

常会出现的问题

  1. 空指针报错
  2. 类型转换报错
  3. 释放有其他指针引用的空间
  4. 最后一个指针指引丢失,但空间没有释放
  5. 数组越界,访问超出申请的 buffer 大小范围

1. splint 介绍

针对C语言的开源程序静态分析工具 -- splint

2. splint消息

检测文件*.c

splint *.c

测试文件 splint_msg.c

//splint_msg.c
int func_splint_msg1(void)
{
    int a;
    return 0;
}
int func_splint_msg2(void)
{
    int* a = (int*)malloc(sizeof(int));
    a = NULL;
    return 0;
}
Splint 3.1.2 --- 11 Oct 2015

// 给出告警所在函数名,在函数的第一个警告消息报告前打印;
splint_msg.c: (in function func_splint_msg1)        
// 消息的正文,文件名、行号、列号显示在的警告的正文前;
splint_msg.c:4:6: Variable a declared but not used
// 有关该可疑错误的详细信息,包含一些怎样去掉这个消息的信息;
  A variable is declared but never used. Use /*@unused@*/ in front of
  declaration to suppress message. (Use -varuse to inhibit warning)
// 第二条
splint_msg.c: (in function func_splint_msg2)
splint_msg.c:10:2: Fresh storage a (type int *) not released before assignment:
                      a = NULL
  A memory leak has been detected. Storage allocated locally is not released
  before the last reference to it is lost. (Use -mustfreefresh to inhibit
  warning)
  // 给出格外的位置信息,这里消息给出了是在哪里申请了这个可能泄露的内存。
   splint_msg.c:9:38: Fresh storage a created

2. 检查控制

splint提供了三种方式可进行检查的控制,分别是.splintrc配置文件、flags标志和格式化注释。

1. flags

splint 支持几百个标志用来控制检查和消息报告

使用时标志前加’+‘或’-’,'+'标志开启这个标志,'-'表示关闭此标志

splint -showcol a.c   //在检测a.c时,告警消息中列数不被打印
splint -varuse  a.c   //在检测a.c时,告警消息中未使用变量告警不被打印

2. .splintrc配置文件

在使用源码安装splint之后,.splintrc 文件将被安装在主目录下

.splintrc 文件中对一些标志作了默认的设定,命令行中指定的 flags 标志会覆盖 .splintrc 文件中的标志。

3. 格式化注释

格式化注释提供一个类型、变量或函数的格外的信息,可以控制标志设置,增加检查效果,所有格式化注释都以/@开始,@/结束,比如在函数参数前加/@null@/,表示该参数可能是NULL,做检测时,splint会加强对该参数的值的检测。

3. 检测分析内容

1. 引用空指针(Null Dereferences)

在Unix操作系统中,解引用空指针将导致我们在程序运行时产生段错误(Segmentation fault)

// null.c
int func_null(void)
{
    int *a = NULL;
    return *a;
}
Splint 3.1.2 --- 11 Oct 2015

null.c: (in function func_null)
null.c:3:11: Unrecognized identifier: null
  Identifier used in code has not been declared. (Use -unrecog to inhibit
  warning)
null.c:4:9: Return value type int * does not match declared type int: a
  Types are incompatible. (Use -type to inhibit warning)

Finished checking --- 2 code warnings

2. 类型(Types)

我们在编程中经常用到强制类型转换,将有符号值转换为无符号值、大范围类型值赋值给小范围类型

void splint_types(void)
{
    short a = 0;
    long b = 32768;
    a = b;
    return;
}

 

int main() {
    return 1;
}
Splint 3.1.2 --- 11 Oct 2015

types.c: (in function splint_types)
types.c:5:2: Assignment of long int to short int: a = b
  To ignore type qualifiers in type comparisons use +ignorequals.

Finished checking --- 1 code warning

3. 内存管理(Memory Management)

C语言程序中,将近半数的bug归功于内存管理问题,关乎内存的bug难以发现并且会给程序带来致命的破坏。

由内存释放所产生的问题,我们可以将其分为两种

1. 当尚有其他指针引用的时候,释放一块空间

void memory(void)
{
    int *a = (int *)malloc(sizeof(int));
    int *b = a;
    free(a);
    *b = 0;
    return;
}

int main() {
    return 1;
}

在上面这个例子中,指针a与b指向同一块内存,但在内存释放之后仍对b指向的内容进行赋值操作

Splint 3.1.2 --- 11 Oct 2015

memory.c: (in function memory)
memory.c:6:3: Variable b used after being released
  Memory is used after it has been released (either by passing as an only param
  or assigning to an only global). (Use -usereleased to inhibit warning)
   memory.c:5:7: Storage b released
memory.c:6:3: Dereference of possibly null pointer b: *b
  A possibly null pointer is dereferenced.  Value is either the result of a
  function which may return null (in which case, code should check it is not
  null), or a global, parameter or structure field declared with the null
  qualifier. (Use -nullderef to inhibit warning)
   memory.c:4:11: Storage b may become null

Finished checking --- 2 code warnings

第一个指出我们使用了b指针,而它所指向的内存已被释放

第二个是对解引用空指针的告警

2. 当最后一个指针引用丢失的时候,其指向的空间尚未释放

void memory2(void)
{
    int *a = (int *)malloc(sizeof(int));
    a = NULL;
    return;

}

int main() {
    return 1;
}

这个例子中内存尚未释放,就将指向它的唯一指针赋值为NULL

Splint 3.1.2 --- 11 Oct 2015

memory2.c: (in function memory2)
memory2.c:4:2: Fresh storage a (type int *) not released before assignment:
                  a = NULL
  A memory leak has been detected. Storage allocated locally is not released
  before the last reference to it is lost. (Use -mustfreefresh to inhibit
  warning)
   memory2.c:3:38: Fresh storage a created

Finished checking --- 1 code warning

splint抛出一个告警:类型为 int* 的 a 在进行 a = NULL 赋值前没有释放新分配的空间。

4. 缓存边界(Buffer Sizes)

splint 会对数组边界、字符串边界作检测,使用时需要加上 +bounds 的标志

void bound1(void)
{
    int a[10];
    a[10] = 0;
    return ;
}

int main() {
    return 1;
}

1. 数组越界

# splint +bounds size1.c

Splint 3.1.2 --- 11 Oct 2015

size1.c: (in function bound1)
size1.c:4:2: Likely out-of-bounds store: a[10]
    Unable to resolve constraint:
    requires 9 >= 10
     needed to satisfy precondition:
    requires maxSet(a @ size1.c:4:2) >= 10
  A memory write may write to an address beyond the allocated buffer. (Use
  -likelyboundswrite to inhibit warning)

Finished checking --- 1 code warning

2. 可能出现越界错误

void bounds2(char *str)
{
    char *tmp = getenv("HOME");
    if(tmp != NULL) 
    {
        strcpy(str, tmp);
    }
    return;
}

int main() {
    return 1;
}

告警消息提示我们:在使用strcpy(str, tmp)进行字符串复制时,可能出现越界错误

因为str的大小可能不足以容纳环境变量“HOME”对应的字符串。绿色字体的内容指示了如何消除告警消息。

# splint +bounds size2.c

Splint 3.1.2 --- 11 Oct 2015

size2.c: (in function bounds2)
size2.c:6:3: Possible out-of-bounds store: strcpy(str, tmp)
    Unable to resolve constraint:
    requires maxSet(str @ size2.c:6:10) >= maxRead(getenv("HOME") @
    size2.c:3:14)
     needed to satisfy precondition:
    requires maxSet(str @ size2.c:6:10) >= maxRead(tmp @ size2.c:6:15)
     derived from strcpy precondition: requires maxSet(<parameter 1>) >=
    maxRead(<parameter 2>)
  A memory write may write to an address beyond the allocated buffer. (Use
  -boundswrite to inhibit warning)

Finished checking --- 1 code warning

Splint介绍

代码静态分析工具——splint的学习与使用

splint 手册

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,907评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,546评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,705评论 0 238
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,624评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,940评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,371评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,672评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,396评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,069评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,350评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,876评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,243评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,847评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,004评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,755评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,378评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,266评论 2 259

推荐阅读更多精彩内容

  • 多线程、特别是NSOperation 和 GCD 的内部原理。运行时机制的原理和运用场景。SDWebImage的原...
    LZM轮回阅读 1,977评论 0 12
  • 最全的iOS面试题及答案 iOS面试小贴士 ———————————————回答好下面的足够了-----------...
    zweic阅读 2,566评论 0 73
  • ———————————————回答好下面的足够了---------------------------------...
    恒爱DE问候阅读 1,672评论 0 4
  • 史上最全的iOS面试题及答案 iOS面试小贴士———————————————回答好下面的足够了----------...
    Style_伟阅读 2,317评论 0 35
  • 今天放学回家,我们隔壁家阿姨就给我们送来一袋桑葚。它的颜色是深紫的。桑葚是从树上长的。它没熟的时候是绿色的...
    5f5214a208fb阅读 250评论 0 0