我有一个问题,一个奇怪的问题,我得到一个段错误,这是 GDB 的输出:
Core was generated by `./vfirewall-monitor'.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000402a5c in init () at kernel.c:57
57 }
(gdb) l
52 if (read_rules(conn) == NULL) {
53 return 1;
54 }
55
56 return 0;
57 }
58
59 int get_all_system_info() {
60 /** Inicia a conexão com o banco de dados */
61
(gdb)
SEGFAULT 出现在 57 行,但该行关闭了 init 函数。
这是init函数的代码:
int init() {
if (read_application_config() == READ_CONFIG_FILE_FAILED)
return INIT_FAILED;
conn = (DBConnection *) malloc(sizeof (DBConnection));
conn->dbname = get_config_str(&conf, "dbname");
conn->host = get_config_str(&conf, "dbserver");
conn->user = get_config_str(&conf, "dbuser");
conn->passwd = get_config_str(&conf, "dbpasswd");
conn->port = *(get_config_int(&conf, "dbport"));
if (open_connection(conn) == DB_CONNECT_FAILED) {
insert_log(FATAL, LOG_KERNEL, "Não foi possivel conectar ao banco de dados");
return INIT_FAILED;
}
Thread thread;
thread.detach = false;
create_thread(&thread, (void *) get_all_system_info);
if ((int) thread.return_value == GET_ALL_INFO_FAILED)
return INIT_FAILED;
if (read_rules(conn) == NULL) {
return 1;
}
return 0;
}
当我调用read_rules()
函数时发生了这个段错误。这是 read_rules() 的代码
Rule * read_rules(DBConnection * conn) {
Query query;
strcpy(query.sql, "SELECT id,table_rule,chain,in_iface,action FROM firewall_rules;");
if (execute_query(conn, &query) == QUERY_EXECUTE_FAILED) {
insert_log(FATAL, LOG_FIREWALL, "Falha na leitura das regras de firewall - firewall.c");
return NULL;
}
Row * row;
row = fetch(&query);
Rule * rules;
rules = (Rule *) malloc(sizeof(Rule));
if (row == NULL) {
return NULL;
}
while (row->next_line != NULL) {
printf("Rule: \n");
printf("ID: %s\n", row->cell[0]);
printf("Table: %s\n", row->cell[1]);
printf("Chain: %s\n", row->cell[2]);
printf("In Iface: %s\n", row->cell[3]);
printf("Action: %s\n", row->cell[4]);
row = row->next_line;
}
clear_query(&query);
free_row(row);
return 0;
}
当我删除 read_rules() 的调用时,段错误没有发生。
现在的问题是:为什么在关闭功能时会出现问题?感谢您的关注。