GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 6:19 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 7:07 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
I have a function:

Code:
//In main.c   
char output_entry () {
  extern  FILE* yyin;
  extern int yyparse (void);
  yyin=fmemopen(buffer,strlen(buffer),"r");
  return yyparse();
}


which works fine when called from

Code:
//Open file
gchar *filename;
void open_file(GtkWidget *widget, gpointer data)
{
  GError* error=NULL;
  GtkWidget *dialog;
  GtkFileFilter *filter;
  dialog = gtk_file_chooser_dialog_new("Open File", NULL,
      GTK_FILE_CHOOSER_ACTION_OPEN,
      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
      NULL);
  filter = gtk_file_filter_new();
  gtk_file_filter_set_name(filter, "All files (*.*)");
  gtk_file_filter_add_pattern(filter, "*");
  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

  filter = gtk_file_filter_new();
  gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
  gtk_file_filter_add_pattern(filter, "*.bib");
  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
  gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

  if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
  {
    gtk_list_store_clear (store);
    filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    g_file_get_contents(filename, &buffer, &length , &error);
    g_assert(!error);
    buf_mod=FALSE;

    char* markup=g_markup_printf_escaped ("<span style=\"italic\">%s</span>", filename);
    gtk_label_set_markup(GTK_LABEL(flabel), markup);
    gtk_widget_destroy(dialog);

    output_entry();
  }
  else{
    gtk_widget_destroy(dialog);
//    g_free(buffer);
  }
}


buffer is `extern gchar *buffer; `, initialized as buffer="";


This part of the code is working fine. I tried to use output_entry with some other source as well:

Code:
void gs_open(GtkWidget *window, gpointer data) {
  GScanner *gs_scanner;
  GHashTable *gs_table;
  GError* error=NULL;
  GtkTextIter start, end;
  GtkListStore *gs_store;
  GtkTreeIter siter;
  GtkWidget *gs_tree;
  gboolean valid;
  GString *ustring = g_string_new ("");
  GString *str=g_string_new(NULL);

  GtkTextBuffer *gs_buf=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gs_txt));
  gtk_text_buffer_get_start_iter (gs_buf, &start);
  gtk_text_buffer_get_end_iter (gs_buf, &end);
  gchar *gs_text = gtk_text_buffer_get_text (gs_buf, &start, &end, FALSE);
  strcat(buffer, gs_text);
  gtk_list_store_clear(store);
  output_entry ();
  buf_mod=TRUE;
  gtk_widget_destroy(gtk_widget_get_toplevel (window));
}


And this gives seg fault. Running with gdb shows:

Quote:
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff16fc700 (LWP 6178)]

Program received signal SIGSEGV, Segmentation fault.
0x0000003b94097261 in __strcat_sse2_unaligned () from /lib64/libc.so.6
bt
.......
.......
up
#1 0x0000000000407195 in gs_open (window=0x863a80, data=<optimized out>)
at src/search.c:103
103 strcat(buffer, gs_text);

Kindly help (I am a novice in C). And google is not helping much on the strcat_sse2_unaligned


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 7:37 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Hello,

It is very unlikely that you will find any information on _strcat_sse2_unaligned() as this is an internal function used within the C library.

Your error is in the use of strcat() in gs_open() and how you declared buffer.

If you declared buffer as
Code:
char *buffer = "";
Your use of strcat() would case a buffer overflow. The initial buffer is only 1 byte in size and that contains the '\0' terminator. Also if buffer has been used before the contents or even the pointer could also be mangled giving you more problems.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 7:44 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
errol,
So what is the better way of initializing the buffer?
Yesterday I tried with uninitialised buffer and then
Code:
if (strlen(buffer)=0){
strcpy(buffer,gs_text)
}
else {
strcat(buffer,gs_text)
}


But that was not working either.


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 8:01 am 
Offline
Familiar Face

Joined: Tue Jun 11, 2013 7:08 am
Posts: 8
Hi,

In that line of code : "if (strlen(buffer)=0){"
you may write : "if (strlen(buffer)==0){"

This is a first problem.
Then, buffer is allocated with only one char into (see errol response)... so,
a strcpy or a strcat will automatically generate an overflow....

I probably would initialize buffer with

Code:
char *buffer = NULL;


and then

Code:
if (buffer == NULL){
buffer = (char *) malloc(strlen(gs_text) + 1);
strcpy(buffer,gs_text);
}


and in the "else" part you may look for sufficient place in buffer to add gs_text, or reallocate it...

Regards


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 9:20 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Ruda your original code does have many other memory leak problems, so these also need fixing.

Michel is close to what is needed to be done.

The global buffer should be set up like this
Code:
gchar *buffer;
There is no need to set the value as it will be NULL, this is part of the C standard.

Unless you are keeping track of the size of the buffer the simple way is to do this for each new memory allocation of the buffer
Code:
    g_free (buffer);
    buffer = g_strdup (gs_text);


if you just need to free the buffer use
Code:
    g_free (buffer);
    buffer = NULL;
This avoids double freeing a memory area.

If you do keep track of the buffer size then you can do more by reusing the memory area.

One way to do this is to use GString but this does add a little bit more complexity such as when you use g_file_get_contents () as you will need to have a temporary buffer first to load the contents then pass the data to your GString.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 9:46 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Errol,
Thanks for your explanation.
As you can see, when the buffer exists,
Code:
  if (buffer == NULL){
    buffer = (char *) malloc(strlen(gs_text) + 1);
    strcpy(buffer,gs_text);
  }
  else{
    buffer = (char *) malloc(strlen(gs_text) + strlen(buffer) + 1);
    strcat(buffer,gs_text);
  }

So, the idea is, 1. if there is nothing in buffer, i.e. I am starting a new file, copy gs_text to buffer
2. if buffer!= NULL, i.e. I am appending a already existing file. I will append buffer by gs_text.

So, probably, I cann't use g_free (buffer).
NB: I can only g_free the buffer after the last edit, save->gfree->close. else, i have to save->gfree->reopen the buffer, right? (I asked this here, went unanswered: viewtopic.php?f=3&t=178496)


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 9:51 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Michel SIMIAN ,
Thanks for your reply.
the strcpy is working, but else part (with strcat) seems to be overwriting the buffer, instead of appending(kindly see the reply above).
possibly
Code:
buffer = (char *) malloc(strlen(gs_text) + strlen(buffer) + 1);
is not the line I am looking for (i.e. allocate memory to buffer).
Any help?

Asking for spoonfeeding


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 10:46 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
I have done this!

Code:
  if (buffer == NULL){
    buffer = (char *) malloc(strlen(gs_text) + 1);
    strcpy(buffer,gs_text);
  }
  else{
    buffer=g_strconcat(buffer,gs_text,NULL);
  }

please comment


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 10:58 am 
Offline
Familiar Face

Joined: Tue Jun 11, 2013 7:08 am
Posts: 8
rudra wrote:
Michel SIMIAN ,
but else part (with strcat) seems to be overwriting the buffer, instead of appending(kindly see the reply above).
possibly
Code:
buffer = (char *) malloc(strlen(gs_text) + strlen(buffer) + 1);
is not the line I am looking for (i.e. allocate memory to buffer).
Any help?

Asking for spoonfeeding


for the 'else' part
If you redo an malloc() on buffer, you loose the previous content.

You will better prefer realloc() instead....


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Wed Jun 12, 2013 11:07 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Your latest example will lead to a memory leak. To correct this try.

Code:
  if (buffer == NULL) {
    buffer = g_strdup(gs_text);
  } else {
    gchar *t = buffer;
    buffer = g_strconcat(buffer, gs_text, NULL);
    g_free(t);
  }


Note you must not mix calls to malloc()/free() with g_malloc()/g_free() on a memory area. Since you are using g_file_get_contents() you must use the GLib memory functions.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Segmentation fault while calling a function
PostPosted: Thu Jun 13, 2013 2:40 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
errol wrote:
Your latest example will lead to a memory leak.


Thanks Errol.
I have corrected this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group