lundi 30 mars 2015

Segfaul out from debugger



I have a big problem with this program that simulates a database, everytime that i call the Database_set function, it return segfaul, but when i use gdb for debugging, everythings runs fine.


With more test, i found that everything work well during debugging if i create a new database (a file used for store datas) without reusing the already created databases, if i do things in this way, the call to Database_set won't return segfaul.


Out from debugger, this trick doesn't work and i get segfaul every time i try to access at line 122 addr->set, even if addr isn't a NULL pointer, because the previous call to malloc doesn't get error.


Here is the code :



#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

struct Address {
int id;
int set;
char *name;
char *email;
};

struct Database {
int MAX_DATA;
int MAX_ROWS;
struct Address *rows;
};

struct Connection {
FILE *file;
struct Database *db;
};

void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}

exit(1);
}

void Address_print(struct Address *addr)
{
printf("%d %s %s\n",
addr->id, addr->name, addr->email);
}


void Database_load(struct Connection *conn)
{
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to load database.");
}


struct Connection *Database_open(const char *filename, char mode)
{
struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error");

conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error");

if(mode == 'c') {
conn->file = fopen(filename, "w");
} else {
conn->file = fopen(filename, "r+");

if(conn->file) {
Database_load(conn);
}
}

if(!conn->file) die("Failed to open the file");

return conn;
}

void Database_close(struct Connection *conn)
{
if(conn) {
if(conn->file) fclose(conn->file);
if(conn->db) free(conn->db);
free(conn);
}
}

void Database_write(struct Connection *conn)
{
rewind(conn->file);

int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to write database.");

rc =fflush(conn->file);
if(rc == -1) die("Cannot flush database.");
}

void Database_create(struct Connection *conn)
{
printf("Please, insert max database rows limit .....");
scanf("%d",&conn->db->MAX_ROWS);

int i = 0;

conn->db->rows = malloc(sizeof(struct Address)*conn->db->MAX_ROWS);
if (!conn->db->rows) die("Memory error !");

for (i = 0; i < conn->db->MAX_ROWS; i++) {

/* struct Address addr = {.id = i, .set = 0};
conn->db->rows[i] = addr; */

conn->db->rows[i].id = 1;
conn->db->rows[i].set = 0;
}
}

void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{

printf("Please, insert max data limit ....");
scanf("%d", &conn->db->MAX_DATA);


struct Address *addr = &conn->db->rows[id];

if(addr->set) die("Already set, delete it first");

addr->set = 1;

addr->name = malloc(conn->db->MAX_DATA);
if(!addr->name) die("Memory error !");

char *res = strcpy(addr->name, name);
if(!res) die("Name copy failed");


addr->email =malloc(conn->db->MAX_DATA);
if(!addr->email) die("Memory error !");

res = strcpy(addr->email, email);
if(!res) die("Email copy failed");


}

void Database_get(struct Connection *conn, int id)
{
struct Address *addr = &conn->db->rows[id];

if(addr->set) {
Address_print(addr);
} else {
die("ID is not set");
}
}

void Database_delete(struct Connection *conn, int id)
{
conn->db->rows[id].id = id;
conn->db->rows[id].set = 0;
}

void Database_list(struct Connection *conn)
{
int i = 0;
struct Database *db = conn->db;

for(i = 0; i < conn->db->MAX_ROWS; i++) {
struct Address *cur = &db->rows[i];

if(cur->set) {
Address_print(cur);
}
}
}

int main(int argc, char *argv[])
{

if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");

char *filename = argv[1];
char action = argv[2][0];

struct Connection *conn = Database_open(filename, action);

int id = 0;

if (action != 'c')
{
if (argc > 3) id = atoi(argv[4]);
if (id >= conn->db->MAX_ROWS) die("There's not that many records.");
}

switch(action) {
case 'c':
Database_create(conn);
Database_write(conn);
break;

case 'g':
if(argc != 4) die("Need an id to get");
Database_get(conn, id);
break;

case 's':
if(argc != 6) die("Need id, name, email to set");

Database_set(conn, id, argv[4], argv[5]);
Database_write(conn);
break;

case 'd':
if(argc != 4) die("Need id to delete");

Database_delete(conn, id);
Database_write(conn);
break;

case 'l':
Database_list(conn);
break;
default:
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
}

Database_close(conn);

return 0;
}



Aucun commentaire:

Enregistrer un commentaire