下例说明了如何准备程序、使用 Discover 对其进行校验、运行程序,以及针对检测到的内存访问错误生成一个报告。此示例使用了一个访问未初始化数据的简单程序。
% cat test_UMR.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
// UMR: accessing uninitialized data
int *p = (int*) malloc(sizeof(int));
printf("*p = %d\n", *p);
free(p);
}
% cc -g -02 test_UMR.c
% a.out
*p = 131464
% discover -w - a.out
% a.out
ERROR (UMR): accessing uninitialized data from address 0x50010 (4 bytes) at:
main() + 0x54 <test_UMR.c:7>
4: {
5: // UMR: accessing uninitialized data
6: int *p = (int*) malloc(sizeof(int));
7:=> printf(**p = %d\n", *p);
8: free(p);
9: }
_start() + 0x108
block at 0x50010 (4 bytes long) was allocated at:
malloc() + 0x220
main() + 0x1c <test_UMR.c:6>
3: int main()
4: {
5: // UMR: accessing uninitialized data
6:=> int *p = (int*) malloc(sizeof(int));
7: printf(**p = %d\n", *p);
8: free(p);
9: }
_start() + 0x108
*p = 327704
**************** Discover Memory Report *****************
No allocated memory left on program exit.
DISCOVER SUMMARY:
unique errors : 1 (1 total, 0 filtered)
unique warnings : 0 (0 total, 0 filtered)
FILTERS: type=ML:func=_rt_boot:depth=*; type=*:func__f90_esfw:depth=*;
type=ML:func=__f90_ssfw:depth=*;
Discover 输出显示了在何处使用了未初始化内存、将其分配至了何处,以及结果摘要。