Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seg Fault #76

Open
a-p-jo opened this issue Feb 28, 2021 · 4 comments
Open

Seg Fault #76

a-p-jo opened this issue Feb 28, 2021 · 4 comments

Comments

@a-p-jo
Copy link

a-p-jo commented Feb 28, 2021

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
Copy link
Author

a-p-jo commented Feb 28, 2021

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
Copy link
Author

a-p-jo commented Feb 28, 2021

@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

@ghost
Copy link

ghost commented Apr 28, 2023

Ahh, yes I see the problem! You're on a Crapple platform. Crapple only supports what they think their users want. Consider Windows or Linux in the near future.

@CakesStuff
Copy link

CakesStuff commented Jan 14, 2024

memset(row->hl+i,HL_COMMENT,row->size-i);
should be:
memset(row->hl+i,HL_COMMENT,row->rsize-i);

Edit: also causes problems on linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants