Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segfault - strcpy() copying into array

Reply
Thread Tools

Segfault - strcpy() copying into array

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-11-2011
arnuld <(E-Mail Removed)> writes:
<snip>
> Function list_remove_element_by_id() was one which was causing problems,
> I think now it is working fine. See any problem ?


Yes.

<snip>
> 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-
>>title);

> 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>
--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Copying file lines into an array [ newbie ] inyc163 C Programming 2 05-25-2004 09:09 PM
Copying file lines into an array [ newbie ] jj C Programming 4 05-24-2004 05:19 PM



Advertisments