Compare commits

...

5 Commits

Author SHA1 Message Date
Michael
e9cf02e428 fix: flip decrement/increment on comparison
- this prevents wrapping size_t below 0
2026-10-13 21:11:20 +02:00
Michael
cfe3d40fc6 fix: move ownership of line to oldline
- set line to null when moved
- also, free line when loading from history
2026-10-13 21:09:18 +02:00
Michael
913449e500 fix: memory bug due to use of wrong line size 2026-10-13 21:00:05 +02:00
Michael
6672ecac58 fix: immediately continue when empty line is read 2026-10-13 20:45:29 +02:00
Michael
c8b5621df1 fix: when set, free oldline after reading line 2026-10-13 20:43:44 +02:00

View File

@@ -21,7 +21,6 @@
#define DEFAULTTOKENSCAPACITY 64 #define DEFAULTTOKENSCAPACITY 64
#define DEFAULTHISTORYCAPACITY 128 #define DEFAULTHISTORYCAPACITY 128
const int LINESIZE = 512;
int cd(int argc, char** argv) { int cd(int argc, char** argv) {
if (argc != 1) { if (argc != 1) {
@@ -143,7 +142,7 @@ int main(int argc, char** argv) {
int retval = 0; int retval = 0;
char* line = NULL; char* line = NULL;
size_t linecapacity = 0; size_t linecapacity = 0;
char prompt[LINESIZE] = {}; char prompt[DEFAULTLINECAPACITY] = {};
size_t linelen = 0; size_t linelen = 0;
char* tokenwork = NULL; char* tokenwork = NULL;
size_t tokenworkcapacity = 0; size_t tokenworkcapacity = 0;
@@ -161,7 +160,7 @@ int main(int argc, char** argv) {
int laststatus = 0; int laststatus = 0;
int wstatus; int wstatus;
char pwd[LINESIZE] = {}; char pwd[DEFAULTLINECAPACITY] = {};
char* pwd_disp; char* pwd_disp;
char* user; char* user;
@@ -280,7 +279,7 @@ int main(int argc, char** argv) {
// Main loop // Main loop
while(1) { while(1) {
char* result = getcwd(pwd, LINESIZE - 1); char* result = getcwd(pwd, DEFAULTLINECAPACITY - 1);
if (result == NULL) { if (result == NULL) {
retval = 1; retval = 1;
goto cleanup; goto cleanup;
@@ -293,7 +292,7 @@ int main(int argc, char** argv) {
snprintf( snprintf(
prompt, prompt,
LINESIZE, DEFAULTLINECAPACITY,
ANSI_BG_BLUE ANSI_FG_BLACK ANSI_BG_BLUE ANSI_FG_BLACK
"%s" // status "%s" // status
@@ -358,6 +357,7 @@ int main(int argc, char** argv) {
} }
oldline = line; oldline = line;
line = NULL;
} }
} else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) { } else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) {
historyoffset--; historyoffset--;
@@ -384,6 +384,11 @@ int main(int argc, char** argv) {
if (load_from_hist == 1) { if (load_from_hist == 1) {
char* historyline = history[historyi - (historyoffset)]; char* historyline = history[historyi - (historyoffset)];
size_t historylinelen = strlen(historyline); size_t historylinelen = strlen(historyline);
if (line != NULL) {
free(line);
}
line = malloc(historylinelen + 1); line = malloc(historylinelen + 1);
if (line == NULL) { if (line == NULL) {
retval = 1; retval = 1;
@@ -401,8 +406,9 @@ int main(int argc, char** argv) {
} }
} else if (ansi == ANSI_DELETE_WORD) { } else if (ansi == ANSI_DELETE_WORD) {
if (cursor > 0) { if (cursor > 0) {
// 0123 56
size_t index = strnrcspn(line, cursor, " \t"); size_t index = strnrcspn(line, cursor, " \t");
memmove(line + index, line + cursor, LINESIZE - cursor); memmove(line + index, line + cursor, linecapacity - cursor);
linelen -= cursor - index; linelen -= cursor - index;
cursor = index; cursor = index;
} }
@@ -423,12 +429,12 @@ int main(int argc, char** argv) {
if (cursor == linelen) { if (cursor == linelen) {
line[cursor] = 0; line[cursor] = 0;
} else { } else {
memmove(line + cursor, line + cursor + 1, LINESIZE - (cursor)); memmove(line + cursor, line + cursor + 1, linecapacity - (cursor));
} }
} else { } else {
// We are typing // We are typing
if (cursor != linelen) { if (cursor != linelen) {
memmove(line + cursor + 1, line + cursor, LINESIZE - cursor - 1); memmove(line + cursor + 1, line + cursor, linecapacity - cursor - 1);
} }
line[cursor] = readc; line[cursor] = readc;
@@ -457,16 +463,28 @@ int main(int argc, char** argv) {
fflush(stdout); fflush(stdout);
} }
// oldline is only valid while reading
if (oldline != NULL) {
free(oldline);
oldline = NULL;
}
putc('\n', stdout); putc('\n', stdout);
tcsetattr(fileno(stdin), 0, &orig_term); tcsetattr(fileno(stdin), 0, &orig_term);
line[linelen] = 0; line[linelen] = 0;
if (linelen == 0) {
free(line);
line = NULL;
continue;
}
fprintf(historyfile, "%s\n", line); fprintf(historyfile, "%s\n", line);
fflush(historyfile); fflush(historyfile);
while (tokenworkcapacity < linelen - 1) { while (tokenworkcapacity + 1 < linelen) {
tokenworkcapacity *= 2; tokenworkcapacity *= 2;
tokenwork = realloc(tokenwork, tokenworkcapacity); tokenwork = realloc(tokenwork, tokenworkcapacity);
} }