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

fix: several memory faults in REPL #31

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/linux.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ldadd+=lib/tinycc/libtcc.a

ifdef ASAN
cflags += -O0 -ggdb -DDEBUG=1 -fno-omit-frame-pointer -fsanitize=address
ldflags += -fsanitize=address
ldflags += -fsanitize=address -static-libasan
# tinycc_config += --extra-ldflags="${ldflags}"
endif

Expand Down
33 changes: 23 additions & 10 deletions src/cjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
_err("\n\n\n\nPress any key to continue....\n");
getchar();
disableGetCharMode(STDIN_FILENO);
free(code);

enableRawMode(STDIN_FILENO);
editorRefreshScreen();
Expand All @@ -206,19 +207,23 @@
{
TCCState *TCC = (TCCState *)tcs;
int res = 0;
if(err_msg)
*err_msg = NULL;
// run the code from main
//
disableRawMode(STDIN_FILENO);
res = cjit_compile_and_run(TCC, code, 0, NULL, 0, err_msg);
if (res != 0) {
if(err_msg) {
if(*err_msg) {
if (strlen(err_msg) > ERR_MAX -1) {
err_msg[ERR_MAX - 1] = 0;
(*err_msg)[ERR_MAX - 1] = 0;
}
char *p = strchr(err_msg, '\n');
char *p = strchr(*err_msg, '\n');
if (p) *p = 0;
if (*err_msg)
if (*err_msg) {
editorSetStatusMessage(*err_msg);
}

Check warning on line 226 in src/cjit.c

View workflow job for this annotation

GitHub Actions / 🚨 C lint

[cpplint] reported by reviewdog 🐶 Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3] Raw Output: src/cjit.c:226: Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
}
} else {
editorSetStatusMessage("No errors.");
Expand Down Expand Up @@ -260,14 +265,22 @@
_err(err_msg);
} else {
int row = 0;
int i = 0;
initEditor();

editorInsertRow(row++, "#include <stdio.h>", 18);
editorInsertRow(row++, "#include <stdlib.h>", 19);
editorInsertRow(row++, "", 0);
editorInsertRow(row++, "int main(int argc, char **argv) {", 33);
editorInsertRow(row++, "", 0);
editorInsertRow(row++, "}", 1);
const char editor_rows[6][40] = {
"#include <stdio.h>",
"#include <stdlib.h>",
"",
"int main(int argc, char **argv) {",
"",
"}"
};

for (i = 0; i < 6; i++) {
editorInsertRow(row++, editor_rows[i], strlen(editor_rows[i]));
}

enableRawMode(STDIN_FILENO);
editorSetStatusMessage(
"HELP: Cx-S = save | Cx-Q = quit | Cx-F = find | Cx-R = run | Cx-E = editor");
Expand Down
61 changes: 48 additions & 13 deletions src/kilo.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,15 @@ int is_separator(int c) {
* that starts at this row or at one before, and does not end at the end
* of the row but spawns to the next row. */
int editorRowHasOpenComment(erow *row) {
if (row->hl && row->rsize && row->hl[row->rsize-1] == HL_MLCOMMENT &&
(row->rsize < 2 || (row->render[row->rsize-2] != '*' ||
row->render[row->rsize-1] != '/'))) return 1;
if ((!row->rsize < 3) || (sizeof(row->hl) < row->rsize - 1))
return 0;
if (row->hl) {
if ((row->hl[row->size - 1] == HL_MLCOMMENT)) {
if (row->rsize < 2 || (row->render[row->rsize-2] != '*' ||
row->render[row->rsize-1] != '/'))
return 1;
}
}
return 0;
}

Expand All @@ -447,7 +453,8 @@ void editorUpdateSyntax(erow *row) {
if (E.syntax == NULL) return; /* No syntax, everything is HL_NORMAL. */

int i, prev_sep, in_string, in_comment;
char *p;
int oc = 0;
char *p = NULL;
char **keywords = E.syntax->keywords;
char *scs = E.syntax->singleline_comment_start;
char *mcs = E.syntax->multiline_comment_start;
Expand All @@ -469,7 +476,10 @@ void editorUpdateSyntax(erow *row) {
if (row->idx > 0 && editorRowHasOpenComment(&E.row[row->idx-1]))
in_comment = 1;

while(*p) {
while((i < row->rsize) && *p) {
if (sizeof(row->hl) < row->rsize) {
row->hl = realloc(row->hl, row->rsize);
}
/* Handle // comments. */
if (prev_sep && *p == scs[0] && *(p+1) == scs[1]) {
/* From here to end is a comment */
Expand All @@ -479,8 +489,16 @@ void editorUpdateSyntax(erow *row) {

/* Handle multi line comments. */
if (in_comment) {
if (sizeof(row->hl) < i + 2) {
row->hl = realloc(row->hl, i + 2);
row->hl[i + 1] = 0;
}
row->hl[i] = HL_MLCOMMENT;
if (*p == mce[0] && *(p+1) == mce[1]) {
if (sizeof(row->hl) < i + 2) {
row->hl = realloc(row->hl, i + 2);
row->hl[i + 1] = 0;
}
row->hl[i+1] = HL_MLCOMMENT;
p += 2; i += 2;
in_comment = 0;
Expand All @@ -492,6 +510,10 @@ void editorUpdateSyntax(erow *row) {
continue;
}
} else if (*p == mcs[0] && *(p+1) == mcs[1]) {
if (sizeof(row->hl) < i + 2) {
row->hl = realloc(row->hl, i + 2);
row->hl[i + 1] = 0;
}
row->hl[i] = HL_MLCOMMENT;
row->hl[i+1] = HL_MLCOMMENT;
p += 2; i += 2;
Expand All @@ -502,8 +524,16 @@ void editorUpdateSyntax(erow *row) {


if (in_string) {
if (sizeof(row->hl) < i + 2) {
row->hl = realloc(row->hl, i + 2);
row->hl[i + 1] = 0;
}
row->hl[i] = HL_STRING;
if (*p == '\\') {
if (sizeof(row->hl) < i + 3) {
row->hl = realloc(row->hl, i + 3);
row->hl[i + 2] = 0;
}
row->hl[i+1] = HL_STRING;
p += 2; i += 2;
prev_sep = 0;
Expand All @@ -516,6 +546,10 @@ void editorUpdateSyntax(erow *row) {
} else {
if (*p == '"' || *p == '\'') {
in_string = *p;
if (sizeof(row->hl) < i + 2) {
row->hl = realloc(row->hl, i + 2);
row->hl[i + 1] = 0;
}
row->hl[i] = HL_STRING;
p++; i++;
prev_sep = 0;
Expand Down Expand Up @@ -548,8 +582,9 @@ void editorUpdateSyntax(erow *row) {
int kw2 = keywords[j][klen-1] == '|';
if (kw2) klen--;

if (!memcmp(p,keywords[j],klen) &&
is_separator(*(p+klen)))

if ((strlen(p) >= klen) && (!memcmp(p,keywords[j],klen) &&
is_separator(*(p+klen))))
{
/* Keyword */
memset(row->hl+i,kw2 ? HL_KEYWORD2 : HL_KEYWORD1,klen);
Expand All @@ -572,7 +607,7 @@ void editorUpdateSyntax(erow *row) {
/* Propagate syntax change to the next row if the open commen
* state changed. This may recursively affect all the following rows
* in the file. */
int oc = editorRowHasOpenComment(row);
oc = editorRowHasOpenComment(row);
if (row->hl_oc != oc && row->idx+1 < E.numrows)
editorUpdateSyntax(&E.row[row->idx+1]);
row->hl_oc = oc;
Expand Down Expand Up @@ -631,7 +666,7 @@ void editorUpdateRow(erow *row) {
printf("Some line of the edited file is too long for kilo\n");
exit(1);
}
row->render = malloc(row->size + tabs*8 + nonprint*9 + 1);
row->render = malloc(row->size + tabs*8 + nonprint + 1);
idx = 0;
for (j = 0; j < row->size; j++) {
if (row->chars[j] == TAB) {
Expand Down Expand Up @@ -696,10 +731,8 @@ void editorDelRow(int at) {
/* Clear the entire buffer */
void editorReset(void) {
int j;
for (j = E.numrows - 1; j > 0; j--) {
editorDelRow(E.row+j);
}
E.numrows = 0;
while (E.numrows > 0)
editorDelRow(E.numrows-1);
E.dirty = 0;
}

Expand Down Expand Up @@ -1123,6 +1156,8 @@ void editorRefreshScreen(void) {
memset(E.row[at].hl, HL_ERROR, E.row[at].rsize);
editorUpdateSyntax(row);
}
free(err_msg);
err_msg = NULL;
}
free(buf);
}
Expand Down