commit 1d9b67184ca1e9c1ffadbeab5b82fb9a25a50f81
parent 2f30a4b356e9cf8e60802c9f885a7bf71da20681
Author: Jan P. Pasierb <me@janpasierb.com>
Date: Wed, 20 Dec 2023 22:27:29 +0000
debug makefile target now builds debugging symbols specifically for gdb
bugfix: segfaults are now prevented when removing last character/line from the file
Diffstat:
4 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
@@ -20,7 +20,7 @@ install: jim
rm -f $(DESTDIR)$(MANPREFIX)/man1/jim.1
chmod 644 $(DESTDIR)$(MANPREFIX)/man1/jim.1.gz
-debug: CFLAGS += -g
+debug: CFLAGS += -ggdb
debug: jim
jim: main.o appbuf.o editor.o file.o find.o input.o output.o row.o syntax.o term.o
diff --git a/input.c b/input.c
@@ -234,8 +234,9 @@ void editorProcessKeypress() {
break;
case DEL_NOR:
editorDelChar();
- if(E.cx == E.row[E.cy].size)
- E.cx--;
+ if(E.row)
+ if(E.cx == E.row[E.cy].size)
+ E.cx--;
break;
case REPLACE:
E.isreplace = 1;
diff --git a/row.c b/row.c
@@ -90,16 +90,30 @@ void editorFreeRow(erow *row) {
free(row->hl);
}
+void editorClearRow(erow *row) {
+ row->size = 0;
+ row->chars = realloc(row->chars, 1);
+ row->chars[0] = '\0';
+ editorUpdateRow(row);
+}
+
void editorDelRow(int at) {
- if(at < 0 || at >= E.numrows) return;
- editorFreeRow(&E.row[at]);
- memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
- for(int j = at; j < E.numrows - 1; j++)
- E.row[j].idx--;
- E.numrows--;
- if(E.cy == E.numrows)
- E.cy--;
- E.dirty++;
+ if(at < 0 || at >= E.numrows || !E.row) return;
+
+ if(E.numrows > 1)
+ {
+ editorFreeRow(&E.row[at]);
+ memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
+ for(int j = at; j < E.numrows - 1; j++)
+ E.row[j].idx--;
+ E.numrows--;
+ if(E.cy == E.numrows)
+ E.cy--;
+ } else {
+ editorClearRow(&E.row[0]);
+ }
+
+ E.dirty++;
}
void editorRowInsertChar(erow* row, int at, char c) {
diff --git a/test b/test
@@ -0,0 +1 @@
+