summaryrefslogtreecommitdiff
path: root/src/ulib
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-01-03 02:13:08 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-01-03 02:13:08 +0800
commit10614e04929a6d109a5e90524e446b637aaf0c13 (patch)
treeeb11aeb7b641187f207e3b572c5184cf3526b463 /src/ulib
parent1c8e37f63d413a559825020289178d442537449e (diff)
downloadlume-10614e04929a6d109a5e90524e446b637aaf0c13.tar.gz
lume-10614e04929a6d109a5e90524e446b637aaf0c13.tar.xz
Fix: critical malloc size calculation bug
Summary: - Fixed an integer arithmetic error in malloc() where 'nunits' was initialized to 0, causing incorrect heap sizing. - This bug caused malloc to return pointers pointing to unmapped memory pages, leading to Store Page Fault (scause 15) at process boundaries. - Switched memory allocator logic to use byte-based size calculation instead of K&R style units to prevent off-by-one errors. Improvements: - Shell now starts successfully without segfaults.
Diffstat (limited to 'src/ulib')
-rw-r--r--src/ulib/stdlib.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/src/ulib/stdlib.c b/src/ulib/stdlib.c
index 30aa3a9..89d5a08 100644
--- a/src/ulib/stdlib.c
+++ b/src/ulib/stdlib.c
@@ -134,15 +134,12 @@ static header_t *morecore(size_t nu)
void *malloc(size_t size)
{
header_t *p, *prevp;
- size_t nunits = 0;
if (size == 0)
return NULL;
size = ALIGN(size + sizeof(header_t));
- nunits = (nunits * sizeof(header_t) + ALIGNMENT - 1) / ALIGNMENT * ALIGNMENT / sizeof(header_t);
-
LOCK();
if ((prevp = freep) == NULL)
@@ -161,9 +158,11 @@ void *malloc(size_t size)
}
else
{
- p->size -= (nunits * sizeof(header_t));
+ p->size -= size;
+
p = (header_t *)((char *)p + p->size);
- p->size = (nunits * sizeof(header_t));
+
+ p->size = size;
}
freep = prevp;
UNLOCK();
@@ -172,7 +171,7 @@ void *malloc(size_t size)
if (p == freep)
{
- if ((p = morecore(nunits * sizeof(header_t))) == NULL)
+ if ((p = morecore(size)) == NULL)
{
UNLOCK();
errno = ENOMEM;