I have the following struct and a pointer to the struct:
struct myInfo
{
uint16_t router_id;
uint16_t router_port;
uint16_t data_port;
uint32_t router_ip_t;
char router_ip[16];
};
struct myInfo *self;
I have the following functions:
void assignToSelf()
{
printf("%s\n", "in assignToSelf");
router_conn = malloc(sizeof(struct RouterConn));
LIST_FOREACH(router_conn, &router_list, next)
{
if(router_conn->cost == 0)
{
self->router_id = router_conn->router_id;
self->router_port = router_conn->router_port;
self->data_port = router_conn->data_port;
self->router_ip_t = router_conn->router_ip_t;
strcpy(self->router_ip, router_conn->router_ip);
int timer_fd;
struct itimerspec timer;
timer_fd = timerfd_create(CLOCK_REALTIME, 0);
timer.it_value.tv_sec = update_interval;
timer.it_value.tv_nsec = 0;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 0;
if(timerfd_settime(timer_fd, 0, &timer, NULL) < 0)
ERROR("Error setting timer value for router!");
router_conn->timer_val = timer_fd;
addToList(timer_fd);
}
}
printf("self->router_id: %u\n", self->router_id);
printf("%s\n", "leaving assignToSelf");
}
void update_cost_matrix()
{
printf("%s\n", "in update_cost_matrix");
printf("self->router_id: %u\n", self->router_id);
int dest, neighbor;
uint16_t cost, mincost, nexthop;
dest = 0;
router_conn = malloc(sizeof(struct RouterConn));
LIST_FOREACH(router_conn, &router_list, next)
{
printf("*\n");
neighbor = 0;
mincost = INF;
nexthop = INF;
if(router_conn->router_id == self->router_id)
{
dest++;
continue;
}
router_conn_temp = malloc(sizeof(struct RouterConn));
LIST_FOREACH(router_conn_temp, &router_list, next)
{
if(router_conn_temp->isNeighbor)
{
if(cost_mat[self->router_id][neighbor] != INF && cost_mat[neighbor][dest] != INF)
{
cost = cost_mat[self->router_id][neighbor] + cost_mat[neighbor][dest];
if(cost < mincost)
{
mincost = cost;
nexthop = router_conn_temp->router_id;
}
}
}
neighbor++;
}
router_conn->nexthop = nexthop;
router_conn->cost = mincost;
cost_mat[self->router_id][dest] = mincost;
dest++;
}
printf("%s\n", "leaving update_cost_matrix");
}
Now, I am getting a seg fault when I am trying to access self->router_id
in update_cost_matrix()
, BUT only when the value of self->router_id
is 5. However, it is not giving a seg fault when I am accessing the same value in the function above, that is, assignToSelf()
, even though the value might be 5.
This is a code for a software defined routing, and the same code is running on various servers (the servers act as routers). The code is working fine for all routers whose self->router_id
is less than 5. I am getting a seg fault only in case of a value greater than or equal to 5, that too only in the update_cost_matrix()
function.
I cannot figure out a reason for this and it comes off as pretty weird. Has it got to do anything with the data type of self->router_id
being uint16_t
?
The output that I am getting is as below:
in assignToSelf
self->router_id: 5
leaving assignToSelf
in update_cost_matrix
Segmentation fault
EDIT : I have initialized the pointer to the struct self
prior to this function call, at the very beginning, in the following way:
self = (struct myInfo*)malloc(sizeof(struct myInfo));