Segfault - strcpy() copying into array

B

Ben Bacarisse

arnuld said:
Function list_remove_element_by_id() was one which was causing problems,
I think now it is working fine. See any problem ?

Yes.

struct sq_struct
{
char title[100+1];
struct sq_struct* next;
};

struct sq_list
{
struct sq_struct* head;
struct sq_struct* tail;
};

This I spotted in passing:
strcpy(p->title, t);

You can't just assume that there's room.

This is the function you are asking about:
/* Ugliest of the Hacks I have evr done. Queue (FIFO) is not the right
data structure to use if I am
removing elements from anywhere but top */
struct sq_list* list_remove_element_by_id(struct sq_list* s, const char*
title )
{
struct sq_struct* h;
struct sq_struct* prv;
struct sq_struct* nx;

if( NULL == s || NULL == title)
{
printf("IN: %s @%d: Invalid Args\n", __func__, __LINE__);
return NULL;
}
else if( NULL == s->head && NULL == s->tail )
{
printf("IN: %s @%d: Well, List is empty\n", __func__, __LINE__);
return NULL;
}
else if( NULL == s->head || NULL == s->tail )
{
printf("IN: %s @%d: ", __func__, __LINE__);
printf("There is something seriously wrong with your list\n");
printf("One of the head/tail is empty while other is not \n");
return NULL;
}

for(h = s->head, prv = NULL; h; h = h->next)
{
if(NULL == h->title)
{
printf("IN: %s @%d: *ERROR* struct exists but ID is NULL :-/",
__func__, __LINE__);
}
else
{
if(0 == strcmp(h->title, title))
{
printf("IN: %s @ %d: Removing %s\n", __FILE__, __LINE__, h-
nx = h->next;
free(h);
if(NULL == prv) /* means we are deleting head */
{
s->head = nx;
}
else
{
prv->next = nx;
}
break;
}
}

prv = h;
}

return s;
}

When I look at suspect code (you'd done most of the work by telling me
this was the function to look in) I check all the boundary cases;
i.e. all the cases other than the "run of the mill case" which is
removal of an element from the middle of normal list.

Can you think of any cases that are special other than the ones that you
have dealt with? You've covered NULL arguments, empty lists and
mall-formed lists, but there is another special case when you have a
list like this with a head and a tail pointer.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top