maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   [solved] Problems with crashes in C-Code (https://talk.maemo.org/showthread.php?t=83629)

electroaudio 2012-04-14 13:51

[solved] Problems with crashes in C-Code
 
I dont understand this error, but i dont understand C so well either...
When the code in pink is commented out, then the routine seem to work very well, but when the part in pink is part of the compilation, then the program will crash (sort of..) randomly with error sigsegv-11

Code:

GdkPixbuf *get_pixbuf_file (gchar *fullpath)


    GdkPixbuf *pixbuf=NULL;
        struct stat statbuf;
        int counter=0;
        char mychar;
        char xxx[50];
        char *mime_icon_name=NULL;
        gchar *mime_icon_file=NULL;
        gchar *test;
        gchar *file_open;
        GtkIconTheme *icon_theme;
        char *ext = strrchr(fullpath, '.');

        if (ext==NULL)
                ...
        if (strstr(ext,".desktop") && strstr(".desktop",ext))
                ...
        if (strstr(fullpath, "/folder.png") && (fast_mode==0))
                ...
        mime_icon_name=NULL;
        if ((mime_icon_name=strrchr(fullpath, '.'))==0)
                ..
            if ((statbuf.st_mode &S_IXUSR))
              ...


        memset (xxx,0,sizeof(xxx));
          for ( counter = 0 ;  (counter<= (sizeof(xxx)-0)) && (mime_icon_name[counter]!=0) ; counter++ ){
                  mychar=mime_icon_name[counter];
                xxx[counter]=(tolower(mychar));
                }


        mime_icon_file=g_strconcat ("/home/user/.deskypplet/icons/",xxx+1,".png",NULL);

        if ((pixbuf=gdk_pixbuf_new_from_file_at_size(mime_icon_file,app->icon_x_size,app->icon_y_size,NULL))!=0)
                {
                return pixbuf;
                }
        else
                {
                pixbuf= gdk_pixbuf_new_from_file_at_size("/home/user/.deskypplet/icons/dummy.png",app->icon_x_size,app->icon_y_size,NULL);
                return pixbuf;
                }               
}

Ps. I have deleted code that i dont believe has anything to do with it and replaced it with "..." to make it more readable ;)

daperl 2012-04-14 14:18

Re: Problems with crashes in C-Code
 
Maybe change

Code:

counter<=
to

Code:

counter<

daperl 2012-04-14 14:29

Re: Problems with crashes in C-Code
 
xxx must be NULL terminated for the line after the pink code to not randomly crash. The pink code doesn't guarantee that. So, you might actually want to change:

Code:

counter<= (sizeof(xxx)-0
to

Code:

counter< (sizeof(xxx)-1

electroaudio 2012-04-14 16:10

Re: Problems with crashes in C-Code
 
Quote:

Originally Posted by daperl (Post 1192043)
xxx must be NULL terminated for the line after the pink code to not randomly crash. The pink code doesn't guarantee that. So, you might actually want to change:

Code:

counter<= (sizeof(xxx)-0
to

Code:

counter< (sizeof(xxx)-1

First time i have tried to compile over a 3G network from a cafe :)
Unfourtunately that didnt help.

But what about the definitions of the string? is it a guarantee that a declared pointer has any space to hold a string?
Like the mime_icon_name , how can it have any allocated space to it to hold the 50 chars?

daperl 2012-04-14 18:28

Re: Problems with crashes in C-Code
 
Yes, exactly, define it the same as xxx. That should get you to point B. It's not very well written.

So, instead of:

Code:

char *mime_icon_name=NULL;
make it

Code:

char mime_icon_name[50];
There are possibly other memory problems, but start there.

electroaudio 2012-04-14 19:21

Re: Problems with crashes in C-Code
 
That feels much better, but now i get a lot of "incompatible types in assignment" errors from the compiler when the string is used instead.

Is it possible to rewrite the functions below to work again with this stringdefinition?

Code:

mime_icon_name=NULL;
if ((mime_icon_name=strrchr(fullpath, '.'))==0)
mime_icon_name=g_strconcat (".noext-exe","\0",NULL);
mime_icon_name=g_strconcat (".noext","\0",NULL);

It didnt complain about these lines, but do they still work as expected?

Code:

.. && (mime_icon_name[counter]!=0)
mychar=mime_icon_name[counter];
xxx[counter]=(tolower(mychar));


electroaudio 2012-04-14 23:53

Re: Problems with crashes in C-Code
 
Sorry, it should be a pointer as it was.
It got its value from the parts that i had edited out.
But after adding:
Code:

if (mime_icon_name!=NULL){ ...}
Around it, it works perfectly. :D

Now i only need to add a timer to prevent readdir() from getting stuck in /dev/ for example. :eek:

daperl 2012-04-15 00:05

Re: [solved] Problems with crashes in C-Code
 
That's what I figured.


All times are GMT. The time now is 09:28.

vBulletin® Version 3.8.8