From 63bf73f1f2ad6f53be362d92f200ca6d0af284be Mon Sep 17 00:00:00 2001 From: arthur barraux Date: Fri, 5 Jun 2026 15:33:19 +0200 Subject: [PATCH] No leaks for splash screen and text files --- include/data.h | 1 - include/define.h | 1 + main.c | 1 - src/buffer.c | 16 ++++------------ src/builtins.c | 24 +++++++++++++----------- src/completion.c | 3 +-- src/file_io.c | 18 ++++++++++-------- src/init.c | 21 ++------------------- src/input.c | 2 +- src/terminal.c | 2 +- 10 files changed, 33 insertions(+), 56 deletions(-) diff --git a/include/data.h b/include/data.h index 3dd5f47..a5fa82a 100644 --- a/include/data.h +++ b/include/data.h @@ -6,7 +6,6 @@ #include #include "define.h" - #include "lisp.h" typedef struct lsp_client_t LspClient; diff --git a/include/define.h b/include/define.h index b6476ce..b83eaa7 100644 --- a/include/define.h +++ b/include/define.h @@ -27,6 +27,7 @@ #define GUTTER_WIDTH 2 + enum editorKey_e { BACKSPACE = 127, ARROW_LEFT = 1000, diff --git a/main.c b/main.c index f12d7c1..c9597c4 100644 --- a/main.c +++ b/main.c @@ -55,7 +55,6 @@ int main(int argc, char *argv[]) { if (argc >= 2) { active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE); buf = &E.buffers[active->buffer_id]; - appDebug("project root : %s\n", dirname(buf->fullname)); } free(splash_screen); // Now guaranteed safe diff --git a/src/buffer.c b/src/buffer.c index 90480e8..db6479e 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -8,7 +8,6 @@ #include "../include/editor_op.h" #include "../include/data.h" #include "include/split_screen.h" -#include <_string.h> #include #include #include @@ -63,8 +62,9 @@ struct buffer_t* bufferFindById(int buffer_id) int bufferCreate(const char* path, enum bufferStatus_e state) { appDebug("Creating new buffer"); - char* filename = basename((char*)path); - char* fullname; + char *path_cpy = strdup(path); + char* filename = basename((char*)path_cpy); + char fullname[PATH_MAX + 1]; // Check if file is already open const int existing_id = bufferFindByFilename(path); if (existing_id != -1) @@ -86,13 +86,6 @@ int bufferCreate(const char* path, enum bufferStatus_e state) { return -1; } - new_buf->fullname = malloc(1024 * sizeof(char)); - if (!new_buf->fullname) - { - free(new_buf->filename); - return -1; - } - fullname = malloc(PATH_MAX * sizeof(char)); realpath(path, fullname); new_buf->fullname = strdup(fullname); new_buf->path = dirname(fullname); @@ -103,7 +96,6 @@ int bufferCreate(const char* path, enum bufferStatus_e state) new_buf->dirty = 0; // New file starts clean new_buf->b_lsp_open = 0; - free(fullname); // Load file content using existing editorOpen editorOpen(new_buf); @@ -111,7 +103,7 @@ int bufferCreate(const char* path, enum bufferStatus_e state) if (new_buf->filename[strlen(new_buf->filename) - 1] == 'c') { if (E.lsp_client->state == LSP_SHUTDOWN) - lspStart(E.lsp_client, dirname(new_buf->path)); + lspStart(E.lsp_client, new_buf->path); while (E.lsp_client->state != LSP_READY); lspDidOpen(E.lsp_client, new_buf); } diff --git a/src/builtins.c b/src/builtins.c index 98699e1..559a198 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -15,7 +15,6 @@ #include "../include/input.h" #include "../include/terminal.h" #include "../include/split_screen.h" -#include "../include/lisp.h" #include #include @@ -24,6 +23,7 @@ #include "include/completion.h" #include "include/init.h" + /** * @brief Finds a prefix configuration by name * @details Searches the prefix array for a prefix matching the given name. @@ -148,17 +148,17 @@ void bFree_structs(void) // Similar fix for buffers for (i = 0; i < E.number_of_buffer; ++i) { - free(E.buffers[i].filename); - E.buffers[i].filename = NULL; - for (j = 0; j < E.buffers[i].numrows; ++j) { - free(E.buffers[i].row[j].chars); - E.buffers[i].row[j].chars = NULL; - } - free(E.buffers[i].row); - E.buffers[i].row = NULL; + struct buffer_t *b = &E.buffers[i]; + for (j = 0; j < b->numrows; j++) + free(b->row[j].chars); // ← free each row's content + free(b->row); // ← then free the array + if (b->filename) free(b->filename); + // if (b->path) free(b->path); + if (b->fullname) free(b->fullname); } // bFree layout free(E.layout.panes); + free(E.status_msg); free(E.init_file_path); fclose(E.fd_init_file); @@ -188,11 +188,12 @@ Lisp editorQuit(Lisp args, LispError* e, LispContext ctx) } disableRawMode(); bFree_structs(); + deInitEditor(); write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3); lspShutdown(E.lsp_client); + free(E.lsp_client); lisp_shutdown(E.ctx); - // deInitEditor(); exit(0); } @@ -221,6 +222,7 @@ Lisp l_editorSplitScreenVertical(Lisp args, LispError* e, LispContext ctx) Lisp l_editorSave(Lisp args, LispError* e, LispContext ctx) { + appDebug("[EDITOR SAVE]"); editorSave(); return lisp_null(); } @@ -374,10 +376,10 @@ Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx) */ Lisp editorOpenFile(Lisp args, LispError* e, LispContext ctx) { + appDebug("[EDITOR OPEN FILE]\n"); char* filename = editorPrompt("Open : %s", getenv("PWD"), 1); if (filename) { - // editorOpen(filename); EditorPane* active = splitScreenGetActivePane(); active->buffer_id = bufferCreate(filename, READ_AND_WRITE); } diff --git a/src/completion.c b/src/completion.c index a95f444..a40566d 100644 --- a/src/completion.c +++ b/src/completion.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -148,7 +147,7 @@ int lspStart(LspClient *client, const char *root_dir) client->write_fd = stdout_pipe[0]; client->pid = pid; client->next_id = 1; - client->state = LSP_SHUTTING_DOWN; + client->state = LSP_INITIALIZING; // Send initialize request char root_uri[PATH_MAX + 8]; diff --git a/src/file_io.c b/src/file_io.c index 65d2738..0b84367 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -60,7 +60,7 @@ void editorCloseFile(void) { */ void editorOpen(struct buffer_t* buffer) { FILE *fp; - fp = fopen(buffer->fullname, "a+"); + fp = fopen(buffer->fullname, "r"); if (!fp) die("fopen"); @@ -80,6 +80,8 @@ void editorOpen(struct buffer_t* buffer) { line = NULL; line_cap = 0; // Reset for next iteration } + free(line); + line = NULL; fclose(fp); E.dirty = 0; } @@ -98,7 +100,7 @@ void editorSave() { EditorPane *active = splitScreenGetActivePane(); struct buffer_t *buffer = bufferFindById(active->buffer_id); int len; - int fd; + FILE *fd; if (buffer->filename == NULL) { buffer->filename = editorPrompt("Save as: %s (ESC to cancel)", "", 1); if (buffer->filename == NULL) { @@ -106,21 +108,21 @@ void editorSave() { return; } } - fd = open(buffer->fullname, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd != -1) { + fd = fopen(buffer->fullname, "w"); + if (fd != NULL) { for (int i = 0; i < buffer->numrows; ++i) { len = (int) strlen(buffer->row[i].chars); - if (write(fd, buffer->row[i].chars, len) != len) { - close(fd); + if (fwrite(buffer->row[i].chars, 1, len, fd) != len) { + fclose(fd); editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno)); return; } - write(fd, "\n", 1); + fwrite("\n", 1, 1, fd); } buffer->dirty = 0; - close(fd); + fclose(fd); } editorSetStatusMessage("File saved"); } diff --git a/src/init.c b/src/init.c index 48d6986..9583eab 100644 --- a/src/init.c +++ b/src/init.c @@ -149,7 +149,7 @@ void initEditor() { strncpy(E.prefix[0].prefix_name, "no-prefix", 64); E.prefix_state = 0; - E.lsp_client = (LspClient*)malloc(sizeof(LspClient)); + E.lsp_client = (LspClient*)calloc(1,sizeof(LspClient)); E.lsp_client->state = LSP_SHUTDOWN; initConfig(); @@ -171,22 +171,5 @@ void initEditor() { void deInitEditor() { - freeScreenLayout(&E.layout); - free(E.lsp_client); - free(E.status_msg); - free(E.init_file_path); - free(E.key_binds); - for (int i = 0; i < E.number_of_keybinds; i++) - { - free(E.key_binds[i].key_sequence); - } - for (int i = 0; i < E.number_of_buffer; ++i) - { - free(E.buffers[i].filename); - free(E.buffers[i].path); - free(E.buffers[i].fullname); - free(E.buffers[i].row->chars); - free(E.buffers[i].row); - } - + fclose(E.ctx.p->err_port); } diff --git a/src/input.c b/src/input.c index 6db58e8..249032b 100644 --- a/src/input.c +++ b/src/input.c @@ -210,7 +210,7 @@ int executeKeyBind(char *key_sequence) { */ void editorProcessKeypress() { int c = editorReadKey(); - char key_sequence[8]; + char key_sequence[8] = {0}; EditorPane *active = splitScreenGetActivePane(); struct buffer_t *buf = bufferFindById(active->buffer_id); diff --git a/src/terminal.c b/src/terminal.c index 2d4afd2..2f5986b 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -179,7 +179,7 @@ int editorReadKey() if (c == '\x1b') { - char seq[6]; + char seq[6] = {0}; /* try to read escape sequence */ if (read(STDIN_FILENO, &seq[0], 1) != 1) return '\x1b';