简体   繁体   中英

Segmentation fault and I don't know what's causing it

main.c:132:26: warning: "/*" within comment
main.c: In function ‘importSettings’:
main.c:152: warning: control reaches end of non-void function
main.c: In function ‘cs_init_missions’:
main.c:84: warning: control reaches end of non-void function
j@jonux:~/Projects/csgtk/c$ ./a.out 
Segmentation fault
j@jonux:~/Projects/csgtk/c$ 

This is the output from a compile with -Wall and running the program. Not ideal.

The error seems to involve the code below.

static xmlDocPtr importSettings(char file[], GtkBuilder *builder) {
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
        if(xmlStrEqual(cur->name, (const xmlChar *) "options")) {
            cur = cur->xmlChildrenNode;
            while (cur != NULL) {
                cur = cur->next; 
            }
        }
        cur = cur->next;// <- Segfault is here
    }
}

It seems obvious that the outer loop attempting to set cur to cur->next when cur == NULL is causing the segfault. Is it possible to reconstruct the loop to avoid this? (I thought of a do-while loop but that didn't pan out)

Is the only way to avoid this to encase the statement in an if statement?

I have tried something to fix the problem. I understand why it was failing in the first place, but it is still failing given the output below:

static xmlDocPtr importSettings(char file[], GtkBuilder *builder){
        if (file == NULL) {
            file = "CsSettings.xml";
        }
        //commented stuff here
        settingsTree = xmlParseFile(file);
        //commented stuff here
        cur = xmlDocGetRootElement(settingsTree);
        cur = cur->xmlChildrenNode;
        while (cur != NULL){

            if(xmlStrEqual(cur->name, (const xmlChar *) "options")){ 
                cur = cur->xmlChildrenNode;
                while (cur != NULL){
        //commented stuff here
                    if(cur->next == NULL)
                        cur = cur->parent;
                    else
                        cur = cur->next;

                }
            }
            cur = cur->next;
        }
}

Is there any way to get printf() to give output near here? The segmentation fault is somehow stopping it from running even before the fault occurs.

First of all I fixed your indentation a little and added some annotations.

static xmlDocPtr importSettings(char file[], GtkBuilder *builder){
    cur = cur->xmlChildrenNode;
    while (cur != NULL){
        if(xmlStrEqual(cur->name, (const xmlChar *) "options")){
            cur = cur->xmlChildrenNode;
            while (cur != NULL){
                cur = cur->next;
            }
            //at this point, cur == NULL
        }
        cur = cur->next;//seg fault here
    }
}

The problem is that when the if statement is true you run a second while loop which leaves cur equal to NULL .

The subsequent attempt to de-reference is the seg fault.

The problem is likely to be at the 2nd cur = cur->next; , which follows the while loop - at that point the loop has guaranteed that cur == NULL .

I'm not sure what you're trying to do in that function (has some code been elided for the example?), so don't really have a suggesting at the moment.

Also, as your compiler warnings indicate, nothing is being returned by the function even though it's declared to do so. I would have assumed again that this is because something is removed for the purposes of the example in the question, but then again your compiler is complaining as well. Any caller that uses whatever the function is supposed to return is in for a surprise. That surprise may also be a segfault.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM