0

I have a problem, a strange problem, where I get a segfault, this is the output of 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 appears at 57 line, but this line close the init function.

This is the code of init function:

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;
}

This segfault happened when i call the read_rules() function. This is the code of 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;
}

When i remove the call of read_rules(), the segfault don't happened.

Now the question is: Why is giving problem in closing function? Thanks for your attention.

Vitor Villar
  • 1,855
  • 18
  • 35

0 Answers0