免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2147 | 回复: 2

strcpy是不是会引起二次释放内存的啊? [复制链接]

论坛徽章:
0
发表于 2008-12-11 10:35 |显示全部楼层
我的这段代码里面压根没用到malloc系列的东西,但运行的时候就不知怎么搞得出了这样的错误:
double free or corruption (fasttop): 0x098a5cc8 ***
最后的 PERROR_VB("Leaving...");是我插进去查看程序崩溃之前是否离开这个函数的,是行缓冲,所以会立即打印。但运行起来之后是没有看到他打印这行东西的,所以肯定在这个函数里挂掉了的。
附:
#define PERROR_VB(format, args...) do{fprintf(stderr,format,##args);}while(0)/*verbose print*/
static int str_sum(char* str)
{
    int sum = 0;
    char* ptr = str;

    while (*ptr != 0)
    {
        sum += *ptr;
        ptr += 1;
    }

    return sum;
}

运行结果:
Content get: dbname netflow.
Content get: dbuser ifms.
Content get: dbpassword zhangyanran;.
Content get: dbport 3306.
Content get: dbhost sun.org.
*** glibc detected *** ./auditor.run: double free or corruption (fasttop): 0x098a5cc8 ***
======= Backtrace: =========
/lib/libc.so.6[0xb7d76a00]
/lib/libc.so.6(cfree+0x89)[0xb7d786f9]
……
……

我的源代码是:
static int read_conf_file(char* path)
{
#define __BUFLEN 100
    FILE* file = NULL;
    char buf[__BUFLEN];
    char item[__BUFLEN], item_value[__BUFLEN];
    int line = 1;

    if ((file = fopen(path, "r")) == NULL)
    {
        PFLR("Fail to open configuration file");
        return -1;
    }

    while (!feof(file))
    {
        fscanf(file, "%*[/s/t\n]s");/*skip spaces and tabs at the start of this line*/
        if ((buf[0] = fgetc(file)) == EOF)
            break;/*reaching the end of the file*/

        if (buf[0] == '#')/*this line is just a comment*/
        {
            fscanf(file, "%*[^\n]s");/*skip the comment*/
            fgetc(file);/*skip the newline*/
            line++;
            continue;
        }
        else
        {
            bzero(buf + 1, __BUFLEN - 1);
            fscanf(file, "%99[^\n#]s", buf + 1);/*scan the content*/
            fscanf(file, "%*[^\n]s");/*skip the left comment if there is any*/
            fgetc(file);/*skip the newline */
        }
        /*do the configuring work*/
        PERROR_VB("Content get: %s.\n",buf);
        bzero(item, __BUFLEN);
        bzero(item_value, __BUFLEN);
        sscanf(buf, "%s%s", item, item_value);
        switch (str_sum(item))
        {
        case SUM_DBNAME:
            bzero(shm->rt_cmn.db_dbname, NAME_MAX);
            strncpy(shm->rt_cmn.db_dbname, item_value,NAME_MAX);
            break;
        case SUM_DBPSWD:
            bzero(shm->rt_cmn.db_passwd, NAME_MAX);
            strncpy(shm->rt_cmn.db_passwd, item_value,NAME_MAX);
            break;
        case SUM_DBUSER:
            bzero(shm->rt_cmn.db_username, NAME_MAX);
            strncpy(shm->rt_cmn.db_username, item_value,NAME_MAX);
            break;
        case SUM_DBHOST:
            bzero(shm->rt_cmn.db_host, NAME_MAX);
            strncpy(shm->rt_cmn.db_host, item_value,NAME_MAX);
            break;
        case SUM_DBPORT:
            shm->rt_cmn.db_port = atoi(item_value);
            break;
        default:
            PERROR_VB("Unknown item in configuration file in line %d.\n",line);
            return -1;
        }
    }
    fclose(file);
    PERROR_VB("Leaving...");

    return 0;
#undef __BUFLEN
}


[ 本帖最后由 longtem 于 2008-12-11 11:05 编辑 ]

论坛徽章:
0
发表于 2008-12-11 10:57 |显示全部楼层
原帖由 longtem 于 2008-12-11 10:35 发表
我的这段代码里面压根没用到malloc系列的东西,但运行的时候就不知怎么搞得出了这样的错误:
double free or corruption (fasttop): 0x098a5cc8 ***
最后的 PERROR_VB("Leaving...");是我插进去查看程序崩溃之 ...
...
是我插进去查看程序崩溃之前是否离开这个函数的,是行缓冲,所以会立即打印...




1、不要轻易怀疑库函数
2、缓冲的目的就是不要立即输出,因此你的论点不成立。  fflush函数不是拿来看的,你要手动使用
3、没有在你这段代码中发现free,在其他地方找原因吧

论坛徽章:
0
发表于 2008-12-11 11:06 |显示全部楼层
好的,谢谢你~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP