kilo icon indicating copy to clipboard operation
kilo copied to clipboard

Seg Fault

Open a-p-jo opened this issue 3 years ago • 4 comments

When trying to do ./kilo clip.c , I get zsh: segmentation fault kilo clip.c. View clip.c.

macOS 11.2.2, on x86_64 Intel CPU, compiled with clang and gcc (tried both, tried all optimisation levels), using default terminal and zsh.

Will try on linux and update.

EDIT 1 : Possibly due to one/some of the errors posted in #77 .

Most likely:

kilo.c:1211:9: warning: 'break' will never be executed [-Wunreachable-code-break]
        break;
kilo.c:1307:12: warning: 'return' will never be executed [-Wunreachable-code-return]
    return 0;

or some other UB / EB ?

EDIT 2: It does not instantly seg fault. It takes quite a few seconds. And it seems to use the CPU highly during this. it is read and writing Gigabytes of data during this ! WTF

a-p-jo avatar Feb 28 '21 07:02 a-p-jo

Changed main() to be such :

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr,"Usage: kilo <filename>\n");
        exit(1);
    }

    fprintf(stderr, "1.Got Arguments, checked them.\n2.Initing... ");
    initEditor();
    fprintf(stderr, "Inited\n3.Sending filename to select syntax highlighting... ");
    editorSelectSyntaxHighlight(argv[1]);
    fprintf(stderr, "Sent\n4.Opening editor with filename ... ");
    editorOpen(argv[1]);
    fprintf(stderr, "Opened\n5.Enabling raw mode on stdin... ");
    enableRawMode(STDIN_FILENO);
    fprintf(stderr, "Enabled\n6.Setting status message... ");
    editorSetStatusMessage(
        "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find");
    fprintf(stderr, "Set\n7. Begining loop of editor... ");
    while(1) {
        editorRefreshScreen();
        editorProcessKeypress(STDIN_FILENO);
    }
    return 0;
}

Printf debugging :)

Ran kilo clip.c 2> err.txt

Ran cat err.txt after segmentation fault message :

1.Got Arguments, checked them.
2.Initing... Inited
3.Sending filename to select syntax highlighting... Sent
4.Opening editor with filename ...

Error lies somewhere in editorOpen(argv[1]);

EDIT 1 : In editorOpen(argv[1]);

There is a while((linelen = getline(&line,&linecap,fp)) != -1) {...}

Things were working till there, but not after.

So I ran a loop counter uint64_t i = 0; inside it, and wrote to stderr it's count.

It crashes at 90th iteration , when clip.c has the line : if(fclose(to)) // Close this clipboard

More printing shows that in the 90th iteration , the while loop starts , but crashes in editorInsertRow(E.numrows,line,linelen);

Edit 2:

At the 90th line , the values being sent to editorInsertRow(...) seem okay :

E.numrows = 89
line = 				if(fclose(to)) // Close this clipboard
linelen = 42   

Edit 3: At the 90th line, inside editorInsertRow(...) , it crashes at editorUpdateRow(E.row+at);

Edit 4: This is getting quite tiring. Nearly every function I see is using malloc(...) , realloc(...) and such and running with the returned pointers without checking for NULL !!!

Edit 5: Continuing from 3, it seems editorUpdateSyntax(row); is failing at the 90th line It appears to segfault at the 90th line's comment's second / , like :

if(fclose(to)) // Close this clipboard
~~~~~~~~~~~~~~^(segfaults here)

EDIT 6: Found it !

Looking in detail, in particular investigating the while(*p){...} loop that handles characters, in :

/* Handle // comments. */
        if (prev_sep && *p == scs[0] && *(p+1) == scs[1]) {...}

The memset(row->hl+i,HL_COMMENT,row->size-i); is causing a segfault.

a-p-jo avatar Feb 28 '21 08:02 a-p-jo

@antirez and any other maintainers here, please note :

Summary

When opening clip.c in kilo, it segfaults at the 90th line.

The origin of this failure is in the order :

In main() , at editorOpen(argv[1]); 's while((linelen = getline(&line,&linecap,fp)) != -1) {...} at it's 90th iteration, at function editorInsertRow(E.numrows,line,linelen); , in which it crashes at call editorUpdateSyntax(row); Where, there is a while(*p){...} inside which the segfault occurs at memset(row->hl+i,HL_COMMENT,row->size-i); After *p is the first / in // comment

The 90th line :

				if(fclose(to)) // Close this clipboard

Structure of line : \t\t\t\tif(fclose(to)) // Close this clipboard\n , i.e., 4 \t level of indentation.

Please see what's up with memset() and what it's doing

a-p-jo avatar Feb 28 '21 11:02 a-p-jo