首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

one strange bug解决思路

2012-03-07 
one strange bugvoidmy_test(){FILE*filefopen( /root/todo , r )if(fileNULL){returnNULL}char*

one strange bug
void   my_test()
{      
        FILE   *   file   =   fopen( "/root/todo ", "r ");
        if(file   ==   NULL)
        {
                return   NULL;
        }
        char   *   t;
        while((t   =   my_readline(file,256))!=   NULL)
        {
                free(t);
                t   =   NULL;
        }
        fclose(file);
        file   =   NULL;
             
        char*   fileName   =   calloc(100,1);
        strcpy(fileName, "/root/back/20070411192812592.jpg ");
        char*   split_name   =   strrchr(fileName, '/ ');
        char*   word   =   strdup(split_name);
     
     
        int   size   =   strlen(word);              
        unsigned   char*   result   =   calloc(2*size,1);
        int   i   =   0;      
        int   j   =   0;
     
        while(i <size)
        {
                *(result+j)   =   0x00;
                *(result+j+1)   =   *(word+i);
                i++;
                j   =   j   +   2;
        }
        *(result+j)   =   0x00;
        *(result+j+1)   =   0x00;


        uint8_t   *buf;
        int   file_size   =   43069;

     
        printf( "1\n ");
        buf   =   malloc(file_size);
}

这段代码会在执行到最后一句话   buf   =   malloc(file_size)的时候报错,错误代码是。


***   glibc   detected   ***   ./btserver:   malloc():   memory   corruption:   0x081200c0   ***
=======   Backtrace:   =========
/lib/tls/i686/cmov/libc.so.6[0xb7ec2ef3]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x7e)[0xb7ec460e]
./btserver[0x804c54d]
./btserver[0x804c325]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7e70ebc]
./btserver[0x8049d91]
=======   Memory   map:   ========
08048000-0807a000   r-xp   00000000   16:01   311637           /root/project/btserver/trunk/Debug/btserver
0807a000-0807d000   rw-p   00031000   16:01   311637           /root/project/btserver/trunk/Debug/btserver
0807d000-08141000   rw-p   0807d000   00:00   0                     [heap]


b7d00000-b7d21000   rw-p   b7d00000   00:00   0
b7d21000-b7e00000   ---p   b7d21000   00:00   0
b7e59000-b7e5b000   rw-p   b7e59000   00:00   0
b7e5b000-b7f96000   r-xp   00000000   16:01   4293890         /lib/tls/i686/cmov/libc-2.5.so
b7f96000-b7f97000   r--p   0013b000   16:01   4293890         /lib/tls/i686/cmov/libc-2.5.so
b7f97000-b7f99000   rw-p   0013c000   16:01   4293890         /lib/tls/i686/cmov/libc-2.5.so
b7f99000-b7f9c000   rw-p   b7f99000   00:00   0
b7f9c000-b7faf000   r-xp   00000000   16:01   4293916         /lib/tls/i686/cmov/libpthread-2.5.so
b7faf000-b7fb1000   rw-p   00013000   16:01   4293916         /lib/tls/i686/cmov/libpthread-2.5.so
b7fb1000-b7fb3000   rw-p   b7fb1000   00:00   0
b7fb5000-b7fc0000   r-xp   00000000   16:01   4259904         /lib/libgcc_s.so.1
b7fc0000-b7fc1000   rw-p   0000a000   16:01   4259904         /lib/libgcc_s.so.1
b7fc1000-b7fc4000   rw-p   b7fc1000   00:00   0
b7fc4000-b7fdd000   r-xp   00000000   16:01   4259861         /lib/ld-2.5.so
b7fdd000-b7fdf000   rw-p   00019000   16:01   4259861         /lib/ld-2.5.so
bfe2f000-bfe45000   rw-p   bfe2f000   00:00   0                     [stack]
ffffe000-fffff000   r-xp   00000000   00:00   0                     [vdso]
Aborted   (core   dumped)


而且我完全不知道为什么这段代码会报这个错误.
any   one   can   help   me?


[解决办法]
Follow your test result, We can make sure your pre-codes impact the memory allocated.
I think it may cause memory broken by my experience.
Have a test as following suggestion, move the definition of varible buf on the top ,
and you 'd better call free() to free the appropriate memory include the pointer returned by strdup() and memory you call calloc() to allocate.

I think it has nothing to do with the platform, try again! good luck
[解决办法]
It 's funny to see this conversation, to see how desperate we people want to be to learn english. It is always not a good idea to talk with your own people when you want to learn a foreign language.

Any way, to kulasama(酷拉), have you realized that you have a serious buffer overflow in your code?
When you write
"*(result+j) = 0x00;
*(result+j+1) = 0x00; "
you acturally access the memory across the alloced boundary, no matter it may or may not crab the memory system, it is not a good thing you what.
Fix it, and try your luck again.

热点排行