fix: memory bug due to use of wrong line size

This commit is contained in:
2026-10-13 21:00:05 +02:00
parent 6672ecac58
commit 913449e500

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
@@ -401,8 +400,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 +423,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;