memcmp()检查程序:内存访问错误 [英] memcmp() checker: memory access errors

查看:109
本文介绍了memcmp()检查程序:内存访问错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *

我删除了与重现问题无关的内容。

我的代码出了什么问题?提前感谢任何提示!

1166425362

* /

#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< assert.h>


int probe_img(FILE * fp)

{

unsigned char * data;

unsigned char KDF_header [4] = {0x01,0x03 ,0x13,0x5E};

int probe_len = 256;

int type = -1;


data = malloc(probe_len );

断言(NULL!=数据);

断言(1 == fread(data,probe_len,1,fp));

if(!memcmp(data,KDF_header,4))

{

type = 3;

}


返回类型;

}


int main(无效)

{

FILE * fp = fopen(" sample.kdf"," r");

assert(NULL!= fp);

printf( " type =%d \ n",probe_img(fp));

返回0;

}


/ *

$ echo $ CHECKERO PTS

-p -v -t -o = checker.out

$!chec

checker gcc -Wall bug.c
$ rm checker.out

$ ls -l sample.kdf

-rwxr-xr-x 1 chen mitarb 3075 Feb 10 1997 sample.kdf

$ ./a.out sample.kdf

type = 3

$ cat checker.out


Checker 0.9.9.1(i686-pc-linux-gnu)版权所有(C)1998 Tristan Gingold。

这个程序是用`checkergcc''或`checkerg ++''编译的。

Checker是一个内存访问检测器。

Checker的发布是希望它有用,

但没有任何担保;甚至没有暗示的保证

适销性或特定用途的适用性。有关更多详细信息,请参阅GNU

通用公共许可证。

有关更多信息,请将CHECKEROPTS设置为`--help''

/*
Hi, I have removed things irrelevant to reproducing the problem.
What''s wrong with my code? Thanks in advance for any hint!
1166425362
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int probe_img(FILE *fp)
{
unsigned char *data;
unsigned char KDF_header[4]={0x01, 0x03, 0x13, 0x5E};
int probe_len = 256;
int type=-1;

data = malloc( probe_len );
assert ( NULL != data );
assert ( 1 == fread(data, probe_len, 1, fp) );

if ( !memcmp(data, KDF_header, 4) )
{
type = 3;
}

return type;
}

int main(void)
{
FILE *fp = fopen("sample.kdf", "r");
assert ( NULL != fp);
printf("type=%d\n", probe_img(fp) );
return 0;
}

/*
$ echo $CHECKEROPTS
-p -v -t -o=checker.out
$ !chec
checker gcc -Wall bug.c
$ rm checker.out
$ ls -l sample.kdf
-rwxr-xr-x 1 chen mitarb 3075 Feb 10 1997 sample.kdf
$ ./a.out sample.kdf
type=3
$ cat checker.out

Checker 0.9.9.1 (i686-pc-linux-gnu) Copyright (C) 1998 Tristan Gingold.
This program has been compiled with `checkergcc'' or `checkerg++''.
Checker is a memory access detector.
Checker is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
For more information, set CHECKEROPTS to `--help''


>来自Checker(pid:03038):`./a.out''正在运行(Sun Dec 17 22:47:01
>From Checker (pid:03038): `./a.out'' is running (Sun Dec 17 22:47:01



2006)

2006)


>来自Checker(pid:03038):( mes)用户调试消息。
>From Checker (pid:03038): (mes) debugging message for the user.



开始结束权利名称路径

08048000 0805b58c T-rx .text ./a.out

0805c000 0805fdbc -D rw- .data

b7fa9000 b7fb27fc T-rx .text /lib/libgcc_s.so.1

b7fb37fc b7fb4000 -D rw- .data / lib / libgcc_s .so.1

08048000 0805b58c T-rx .text

0805c000 0805fdbc -D rw- .data

b7e60000 b7f875a4 T-rx .text /lib/tls/libc.so.6

b7f8815c b7f9189c -D rw- .data /lib/tls/libc.so.6

b7fb7000 b7fcb808 T-rx。 text /lib/ld-linux.so.2

b7fccca0 b7fce000 -D rw- .data /lib/ld-linux.so.2

begin end rights name path
08048000 0805b58c T- r-x .text ./a.out
0805c000 0805fdbc -D rw- .data
b7fa9000 b7fb27fc T- r-x .text /lib/libgcc_s.so.1
b7fb37fc b7fb4000 -D rw- .data /lib/libgcc_s.so.1
08048000 0805b58c T- r-x .text
0805c000 0805fdbc -D rw- .data
b7e60000 b7f875a4 T- r-x .text /lib/tls/libc.so.6
b7f8815c b7f9189c -D rw- .data /lib/tls/libc.so.6
b7fb7000 b7fcb808 T- r-x .text /lib/ld-linux.so.2
b7fccca0 b7fce000 -D rw- .data /lib/ld-linux.so.2


>来自Checker(pid:03038):( mes)用户调试消息。
>From Checker (pid:03038): (mes) debugging message for the user.



内存图:

名称|基地|结束|尺寸| RWX |输入

--------------- + ------------ + ------------ + ------------- + ----- + -----

NULL zone | 0x00000000 | 0x08047fff | 128 Mb | --- |无效

文本段| 0x08048000 | 0x0805b58b | 77 kb | r-x |文字

..text | 0x08048000 | 0x0805b58b | 77 kb | r-x | ROnly

(无)| 0x0805b58c | 0x0805bfff | 2 kb | --- | -

数据段| 0x0805c000 | 0x0805fdbb | 15 kb | rw- | RW

..data | 0x0805c000 | 0x0805fdbb | 15 kb | rw- | RW

(无)| 0x0805fdbc | 0xb7e5ffff | 2814 Mb | --- | -

..text | 0xb7e60000 | 0xb7f875a3 | 1181 kb | r-x | ROnly

(无)| 0xb7f875a4 | 0xb7f87fff | 2 kb | --- | -

..数据| 0xb7f88000 | 0xb7f9189b | 38 kb | rw- | RW

(无)| 0xb7f9189c | 0xb7fa8fff | 93 kb | --- | -

..text | 0xb7fa9000 | 0xb7fb27fb | 37 kb | r-x | ROnly

(无)| 0xb7fb27fc | 0xb7fb2fff | 2 kb | --- | -

..数据| 0xb7fb3000 | 0xb7fb3fff | 4 kb | rw- | RW

(无)| 0xb7fb4000 | 0xb7fb6fff | 12 kb | --- | -

..text | 0xb7fb7000 | 0xb7fcb807 | 82 kb | r-x | ROnly

(无)| 0xb7fcb808 | 0xb7fcbfff | 2040 b | --- | -

..数据| 0xb7fcc000 | 0xb7fcdfff | 8 kb | rw- | RW

(无)| 0xb7fce000 | 0xbffcd7b3 | 127 Mb | --- | -

stack | 0xbffcd7b4 | 0xbfffffff | 202 kb | rwx |正常

#malloc(352)= 0x8060134;

#malloc(256)= 0x80602d4;

Memory map:
Name | Base | End | Size | RWX | Type
---------------+------------+------------+-------------+-----+-----
NULL zone | 0x00000000 | 0x08047fff | 128 Mb | --- | Void
text segment | 0x08048000 | 0x0805b58b | 77 kb | r-x | Text
..text | 0x08048000 | 0x0805b58b | 77 kb | r-x | ROnly
(nothing) | 0x0805b58c | 0x0805bfff | 2 kb | --- | -
data segment | 0x0805c000 | 0x0805fdbb | 15 kb | rw- | RW
..data | 0x0805c000 | 0x0805fdbb | 15 kb | rw- | RW
(nothing) | 0x0805fdbc | 0xb7e5ffff | 2814 Mb | --- | -
..text | 0xb7e60000 | 0xb7f875a3 | 1181 kb | r-x | ROnly
(nothing) | 0xb7f875a4 | 0xb7f87fff | 2 kb | --- | -
..data | 0xb7f88000 | 0xb7f9189b | 38 kb | rw- | RW
(nothing) | 0xb7f9189c | 0xb7fa8fff | 93 kb | --- | -
..text | 0xb7fa9000 | 0xb7fb27fb | 37 kb | r-x | ROnly
(nothing) | 0xb7fb27fc | 0xb7fb2fff | 2 kb | --- | -
..data | 0xb7fb3000 | 0xb7fb3fff | 4 kb | rw- | RW
(nothing) | 0xb7fb4000 | 0xb7fb6fff | 12 kb | --- | -
..text | 0xb7fb7000 | 0xb7fcb807 | 82 kb | r-x | ROnly
(nothing) | 0xb7fcb808 | 0xb7fcbfff | 2040 b | --- | -
..data | 0xb7fcc000 | 0xb7fcdfff | 8 kb | rw- | RW
(nothing) | 0xb7fce000 | 0xbffcd7b3 | 127 Mb | --- | -
stack | 0xbffcd7b4 | 0xbfffffff | 202 kb | rwx | Normal
# malloc (352) = 0x8060134;
# malloc (256) = 0x80602d4;


>来自Checker(pid:03038):( rus)读取
中未初始化的字节
>From Checker (pid:03038): (rus) read uninitialized byte(s) in the



stack。

在地址0xbffce798读取1个字节时,在堆栈内。

已知堆栈限制= 0xbffce680

stack.
When Reading 1 byte(s) at address 0xbffce798, inside the stack.
known stack limit= 0xbffce680


>来自Checker(pid:03038):( mes)用户调试消息。
>From Checker (pid:03038): (mes) debugging message for the user.



08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07

0xbffce798:WWWW RW RW RW RW RW RW RW RW RW RW RW RW

0xbffce7a8:RW RW RW RW WWWW RW RW RW RW RW RW RW RW

堆栈帧为:

pc =在checker_text_stubs_begin中的0x08055bb2

stubs / begin-stubs.c:7

pc = 0x08048d73在bug.c中的probe_img:25

pc =主要在bug.c的0x08048fdb:37

pc = 0x08055d53在checker_text_stubs_begin中

stubs / begin-stubs.c:7

pc =在stubs / end-stubs.c的checker_text_stubs_end中的0xb7e74ea8:7

pc = 0x080487e8 in * unknown * at * unknown *:0

08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07
0xbffce798: W W W W RW RW RW RW RW RW RW RW RW RW RW RW
0xbffce7a8: RW RW RW RW W W W W RW RW RW RW RW RW RW RW
Stack frames are:
pc=0x08055bb2 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0x08048d73 in probe_img at bug.c:25
pc=0x08048fdb in main at bug.c:37
pc=0x08055d53 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0xb7e74ea8 in checker_text_stubs_end at stubs/end-stubs.c:7
pc=0x080487e8 in *unknown* at *unknown*:0


>来自Checker(pid:03038):( rus)读取
>From Checker (pid:03038): (rus) read uninitialized byte(s) in the



堆栈中未初始化的字节。

阅读时1堆栈内地址为0xbffce799的字节。

已知堆栈限制= 0xbffce680

stack.
When Reading 1 byte(s) at address 0xbffce799, inside the stack.
known stack limit= 0xbffce680


>来自Checker(pid:03038) : (mes)为用户调试消息。
>From Checker (pid:03038): (mes) debugging message for the user.



08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07

0xbffce798:WWWW RW RW RW RW RW RW RW RW RW RW RW RW

0xbffce7a8:RW RW RW RW WWWW RW RW RW RW RW RW RW RW

堆栈帧为:

pc =在checker_text_stubs_begin中的0x08055bb2

stubs / begin-stubs.c:7

pc = 0x08048d73在bug.c中的probe_img:25

pc =主要在bug.c的0x08048fdb:37

pc = 0x08055d53在checker_text_stubs_begin中

stubs / begin-stubs.c:7

pc =在stubs / end-stubs.c的checker_text_stubs_end中的0xb7e74ea8:7

pc = 0x080487e8 in * unknown * at * unknown *:0

08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07
0xbffce798: W W W W RW RW RW RW RW RW RW RW RW RW RW RW
0xbffce7a8: RW RW RW RW W W W W RW RW RW RW RW RW RW RW
Stack frames are:
pc=0x08055bb2 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0x08048d73 in probe_img at bug.c:25
pc=0x08048fdb in main at bug.c:37
pc=0x08055d53 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0xb7e74ea8 in checker_text_stubs_end at stubs/end-stubs.c:7
pc=0x080487e8 in *unknown* at *unknown*:0


>来自Checker(pid:03038):( rus)读取
>From Checker (pid:03038): (rus) read uninitialized byte(s) in the



堆栈中未初始化的字节。

阅读时1堆栈内地址为0xbffce79a的字节。

已知堆栈限制= 0xbffce680

stack.
When Reading 1 byte(s) at address 0xbffce79a, inside the stack.
known stack limit= 0xbffce680


>来自Checker(pid:03038) : (mes)为用户调试消息。
>From Checker (pid:03038): (mes) debugging message for the user.



08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07

0xbffce798:WWWW RW RW RW RW RW RW RW RW RW RW RW RW

0xbffce7a8:RW RW RW RW WWWW RW RW RW RW RW RW RW RW

堆栈帧为:

pc =在checker_text_stubs_begin中的0x08055bb2

stubs / begin-stubs.c:7

pc = 0x08048d73在bug.c中的probe_img:25

pc =主要在bug.c的0x08048fdb:37

pc = 0x08055d53在checker_text_stubs_begin中

stubs / begin-stubs.c:7

pc =在stubs / end-stubs.c的checker_text_stubs_end中的0xb7e74ea8:7

pc = 0x080487e8 in * unknown * at * unknown *:0

08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07
0xbffce798: W W W W RW RW RW RW RW RW RW RW RW RW RW RW
0xbffce7a8: RW RW RW RW W W W W RW RW RW RW RW RW RW RW
Stack frames are:
pc=0x08055bb2 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0x08048d73 in probe_img at bug.c:25
pc=0x08048fdb in main at bug.c:37
pc=0x08055d53 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0xb7e74ea8 in checker_text_stubs_end at stubs/end-stubs.c:7
pc=0x080487e8 in *unknown* at *unknown*:0


>来自Checker(pid:03038):( rus)读取
>From Checker (pid:03038): (rus) read uninitialized byte(s) in the



堆栈中未初始化的字节。

阅读时1堆栈内地址为0xbffce79b的字节。

已知堆栈限制= 0xbffce680

stack.
When Reading 1 byte(s) at address 0xbffce79b, inside the stack.
known stack limit= 0xbffce680


>来自Checker(pid:03038) : (mes)为用户调试消息。
>From Checker (pid:03038): (mes) debugging message for the user.



08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07

0xbffce798:WWWW RW RW RW RW RW RW RW RW RW RW RW RW

0xbffce7a8:RW RW RW RW WWWW RW RW RW RW RW RW RW RW

堆栈帧为:

pc =在checker_text_stubs_begin中的0x08055bb2

stubs / begin-stubs.c:7

pc = 0x08048d73在bug.c中的probe_img:25

pc =主要在bug.c的0x08048fdb:37

pc = 0x08055d53在checker_text_stubs_begin中

stubs / begin-stubs.c:7

pc =在stubs / end-stubs.c的checker_text_stubs_end中的0xb7e74ea8:7

pc = 0x080487e8 in * unknown * at * unknown *:0

08 09 0a 0b 0c 0d 0e 0f 00 01 02 03 04 05 06 07
0xbffce798: W W W W RW RW RW RW RW RW RW RW RW RW RW RW
0xbffce7a8: RW RW RW RW W W W W RW RW RW RW RW RW RW RW
Stack frames are:
pc=0x08055bb2 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0x08048d73 in probe_img at bug.c:25
pc=0x08048fdb in main at bug.c:37
pc=0x08055d53 in checker_text_stubs_begin at
stubs/begin-stubs.c:7
pc=0xb7e74ea8 in checker_text_stubs_end at stubs/end-stubs.c:7
pc=0x080487e8 in *unknown* at *unknown*:0


>来自Checker(pid:03038):( prf)个人资料结果。
>From Checker (pid:03038): (prf) profile results.



堆栈被处理(访问)130次。

数据段被处理(访问)0次。

文本段被处理(访问)4次。

NULL区域被处理(访问)0次。

.text被处理(访问)0时间。

.data被处理(访问)0次。

.text被处理(访问)0次。

。数据被处理(访问)0次。

.x被处理(访问)0次。

.data被处理(访问)0次。

.text被处理(访问)0次。

.data被处理(访问)0次。

堆(sbrk)被处理(访问)10次。

程序访问内存144次。

堆栈限制调整了47次。

4个内存访问错误报告。

4内存访问错误。

2 malloc / mmalloc调用。

0免费/ mfree电话。

0 realloc / mrealloc电话。

用于垃圾检测的总时间:0秒(s)0次使用。


* /

The stack was handled (accessed) 130 times.
The data segment was handled (accessed) 0 time.
The text segment was handled (accessed) 4 times.
The NULL zone was handled (accessed) 0 time.
The .text was handled (accessed) 0 time.
The .data was handled (accessed) 0 time.
The .text was handled (accessed) 0 time.
The .data was handled (accessed) 0 time.
The .text was handled (accessed) 0 time.
The .data was handled (accessed) 0 time.
The .text was handled (accessed) 0 time.
The .data was handled (accessed) 0 time.
The heap (sbrk) was handled (accessed) 10 times.
The program accessed memory 144 times.
The stack limit was adjusted 47 times.
4 memory access errors reported.
4 memory access errors.
2 malloc/mmalloc calls.
0 free/mfree calls.
0 realloc/mrealloc calls.
Total time used for garbage detecting: 0 sec(s) 0 usecs.

*/

推荐答案

echo


CHECKEROPTS

-p -v -t -o = checker.out
CHECKEROPTS
-p -v -t -o=checker.out


!chec

checker gcc -Wall bug.c
!chec
checker gcc -Wall bug.c


这篇关于memcmp()检查程序:内存访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆