Skip to content

Commit

Permalink
fix: stability fixes for REPL
Browse files Browse the repository at this point in the history
- fixed NULL dereference in statusbar
- fixed multiple AtExit called from children
- fixed unlinking temp dir when not explicitly asked to keep a copy via
  Cx-S (e.g. when opening the editor during the session): missing
  include for libgen.h.
  • Loading branch information
danielinux committed Oct 2, 2024
1 parent aa469ac commit f72c877
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/cjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ static int cjit_check_buffer(void *tcs, char *code, char **err_msg)
}
char *p = strchr(err_msg, '\n');
if (p) *p = 0;
editorSetStatusMessage(*err_msg);
if (*err_msg)
editorSetStatusMessage(*err_msg);
}
} else {
editorSetStatusMessage("No errors.");
Expand Down Expand Up @@ -269,7 +270,7 @@ static int cjit_cli(TCCState *TCC)
editorInsertRow(row++, "}", 1);
enableRawMode(STDIN_FILENO);
editorSetStatusMessage(
"HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find | Ctrl-R Run");
"HELP: Cx-S = save | Cx-Q = quit | Cx-F = find | Cx-R = run | Cx-E = editor");
while(1) {
editorSetCompilerCallback(cjit_compile_buffer);
editorSetCheckCallback(cjit_check_buffer);
Expand Down
18 changes: 13 additions & 5 deletions src/kilo.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <stdarg.h>
#include <fcntl.h>
#include <signal.h>
#include <libgen.h>

/* Syntax highlight types */
#define HL_NORMAL 0
Expand Down Expand Up @@ -112,6 +113,7 @@ struct editorConfig {
int (*check_cb)(void *, const char *, char *);
void *compiler_cb_ctx;
int keep_scratchpad;
int exiting;
};

static struct editorConfig E;
Expand Down Expand Up @@ -148,14 +150,14 @@ enum KEY_ACTION{
static void editorMakeFilename(void) {
const char scratchpad_fname[]="cjit-scratchpad.c";
char dirname_tmpl[] = "/tmp/cjit-scratchpad.c.XXXXXX";
char *dirname;
char *dir_nm;
size_t filename_sz;
if (E.filename)
return;
dirname = mkdtemp(dirname_tmpl);
filename_sz = strlen(dirname) + 1 + strlen(scratchpad_fname) + 1;
dir_nm = mkdtemp(dirname_tmpl);
filename_sz = strlen(dir_nm) + 1 + strlen(scratchpad_fname) + 1;
E.filename = malloc(filename_sz);
snprintf(E.filename, filename_sz, "%s/%s", dirname, scratchpad_fname);
snprintf(E.filename, filename_sz, "%s/%s", dir_nm, scratchpad_fname);
}

void editorSetStatusMessage(const char *fmt, ...);
Expand Down Expand Up @@ -226,13 +228,17 @@ void disableRawMode(int fd) {

/* Called at exit to avoid remaining in raw mode. */
void editorAtExit(void) {
if (!E.exiting)
return;
disableRawMode(STDIN_FILENO);
if ((!E.keep_scratchpad) && (E.filename)) {
char *cp1, *dir_nm;
cp1 = strdup(E.filename);
dir_nm = dirname(cp1);
unlink(E.filename);
rmdir(dir_nm);
sync();
unlinkat(0, dir_nm, AT_REMOVEDIR);
free(cp1);
}
}

Expand Down Expand Up @@ -1336,6 +1342,7 @@ void editorProcessKeypress(int fd) {
* to the edited file. */
break;
case CTRL_Q: /* Ctrl-q */
E.exiting = 1;
exit(0);
break;
case CTRL_S: /* Ctrl-s */
Expand Down Expand Up @@ -1439,6 +1446,7 @@ void initEditor(void) {
E.check_cb = NULL;
E.compiler_cb_ctx = NULL;
E.keep_scratchpad = 0;
E.exiting = 0;
updateWindowSize();
atexit(editorAtExit);
signal(SIGWINCH, handleSigWinCh);
Expand Down

0 comments on commit f72c877

Please sign in to comment.