- 论坛徽章:
- 0
|
我的这段代码里面压根没用到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 编辑 ] |
|