From d7dc1418746f052bc6c7c7ed5823cb0ca894b957 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Thu, 5 Mar 2026 21:52:36 +0100 Subject: [PATCH] CE fix strange characters appearing at the end of file --- ce/context.c | 2 ++ ce/edit.c | 2 +- ce/interp.c | 1 + ce/mprintf.c | 2 ++ kernel/fs/tarfs.c | 48 ++++++++++++++++++++++++++++++---------------- libstring/string.c | 7 +++---- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/ce/context.c b/ce/context.c index d7f1603..cf90ba5 100644 --- a/ce/context.c +++ b/ce/context.c @@ -4,6 +4,7 @@ #include #include #include +#include void cprintf (struct context* context, const char* fmt, ...) { va_list args; @@ -16,6 +17,7 @@ void cprintf (struct context* context, const char* fmt, ...) { return; } + memset (buf, 0, CPRINTF_BUF_MAX); int len = vsnprintf (buf, CPRINTF_BUF_MAX, fmt, args); va_end (args); diff --git a/ce/edit.c b/ce/edit.c index b60a491..929a10c 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -138,7 +138,7 @@ void edit_start (const char* file_path, const char* text) { bb_remaining -= w; } - w = snprintf (bbptr, bb_remaining, ANSIQ_SCR_CLR2LEND "\n"); + w = snprintf (bbptr, bb_remaining, ANSIQ_SCR_CLR2END "\n"); bbptr += w; bb_remaining -= w; diff --git a/ce/interp.c b/ce/interp.c index a499b85..f1287fc 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -2,6 +2,7 @@ #include "arena_alloc.h" #include "context.h" #include "edit.h" +#include "mprintf.h" #include "parser.h" #include "self.h" #include diff --git a/ce/mprintf.c b/ce/mprintf.c index 999103d..6d87fc4 100644 --- a/ce/mprintf.c +++ b/ce/mprintf.c @@ -3,6 +3,7 @@ #include #include #include +#include #include void putchar_ (char ch) { (void)ch; } @@ -18,6 +19,7 @@ void mprintf (const char* fmt, ...) { return; } + memset (buf, 0, MPRINTF_BUF_MAX); int len = vsnprintf (buf, MPRINTF_BUF_MAX, fmt, args); va_end (args); diff --git a/kernel/fs/tarfs.c b/kernel/fs/tarfs.c index 119e952..a3e8b57 100644 --- a/kernel/fs/tarfs.c +++ b/kernel/fs/tarfs.c @@ -25,31 +25,39 @@ static struct tar_file* tar_get_file (struct tarfs* tarfs, const char* filename) static size_t tar_get_size (uint8_t* in) { size_t size = 0; - size_t j; - size_t count = 1; + for (size_t i = 0; i < 11; i++) { + if (in[i] < '0' || in[i] > '7') + break; - for (j = 11; j > 0; j--, count *= 8) - size += ((in[j - 1] - '0') * count); + size = (size * 8) + (in[i] - '0'); + } return size; } -static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr) { +static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr, size_t max_size) { size_t i; + uint8_t* ptr = addr; for (i = 0; i < TARFS_FILES_MAX; i++) { - struct tar_header* hdr = (struct tar_header*)addr; + struct tar_header* hdr = (struct tar_header*)ptr; if (hdr->filename[0] == '\0') break; + if ((size_t)(ptr - addr) + 512 > max_size) + break; + size_t size = tar_get_size (hdr->size); tarfs->tarfs_files[i].header = hdr; - tarfs->tarfs_files[i].content = (uint8_t*)((uintptr_t)hdr + 512); - tarfs->tarfs_files[i].size = tar_get_size ((uint8_t*)hdr->size); + tarfs->tarfs_files[i].content = ptr + 512; + tarfs->tarfs_files[i].size = size; - addr += 512 + ((size + 511) & ~511); + ptr += 512 + ((size + 511) & ~511); + + if ((size_t)(ptr - addr) > max_size) + break; } return i; @@ -92,6 +100,7 @@ int tarfs_mount (struct vfs_volume* volume, bool format) { if (buffer == NULL) { spin_unlock (&back_device->lock); free (volume->udata); + volume->udata = NULL; return ret; } @@ -105,12 +114,14 @@ int tarfs_mount (struct vfs_volume* volume, bool format) { if (ret < 0) { free (buffer); + free (volume->udata); + volume->udata = NULL; return ret; } tarfs->buffer = buffer; - tar_parse (tarfs, tarfs->buffer); + tar_parse (tarfs, tarfs->buffer, total_size); return ret; } @@ -166,6 +177,9 @@ int tarfs_read_file (struct vfs_volume* volume, const char* path, uint8_t* buffe if (file == NULL) return -ST_NOT_FOUND; + if (off >= file->size) + return -ST_OOB_ERROR; + memcpy (buffer, (void*)((uintptr_t)file->content + off), min (size, file->size)); return ST_OK; @@ -183,19 +197,21 @@ int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct di size_t entry_counter = 0; for (size_t i = 0; i < TARFS_FILES_MAX; i++) { - if ((tarfs->tarfs_files[i].header != NULL) && (entry_num == entry_counter)) { - tar_file = &tarfs->tarfs_files[i]; - break; + if (tarfs->tarfs_files[i].header != NULL) { + if (entry_num == entry_counter) { + tar_file = &tarfs->tarfs_files[i]; + break; + } + entry_counter++; } - entry_counter++; } if (tar_file != NULL) { sprintf (entry->path, "/%s", tar_file->header->filename); - return ST_NOT_DIR; + return ST_OK; } - return ST_DIR_NO_ENTRIES; + return -ST_DIR_NO_ENTRIES; } int tarfs_write_file (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off, diff --git a/libstring/string.c b/libstring/string.c index e78eea5..8775ceb 100644 --- a/libstring/string.c +++ b/libstring/string.c @@ -49,17 +49,16 @@ int memcmp (const void* s1, const void* s2, size_t n) { void strtokenize (const char* str, char delim, void* ctx, strtokenize_cb_func_t cb) { const char* start = str; - const char* end; while (*start) { - while (*start == delim && *start) + while (*start && *start == delim) start++; if (!*start) break; - end = start; - while (*end != delim && *end) + const char* end = start; + while (*end && *end != delim) end++; if (!cb (ctx, start, (size_t)(end - start)))