From 73e8ed89c011abce9b0ae2c5a3eb232bdbe8660e Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 20 Aug 2010 18:53:48 +0800 Subject: [PATCH] Fixed heap troubles (and bugs in VFS_GetTruePath) --- Kernel/heap.c | 20 ++++++++++++++++---- Kernel/vfs/open.c | 29 +++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/Kernel/heap.c b/Kernel/heap.c index 97f2d9f1..3f767eb7 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -318,6 +318,9 @@ void Heap_Deallocate(void *Ptr) // Mark as free head->Magic = MAGIC_FREE; + head->File = NULL; + head->Line = 0; + head->ValidSize = 0; // Merge blocks Heap_Merge( head ); @@ -326,6 +329,11 @@ void Heap_Deallocate(void *Ptr) } /** + * \brief Increase/Decrease the size of an allocation + * \param File Calling File + * \param Line Calling Line + * \param __ptr Old memory + * \param __size New Size */ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) { @@ -351,6 +359,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) // Exact Fit if(size == newSize) { head->Size = newSize; + head->ValidSize = __size; head->File = File; head->Line = Line; foot->Head = head; @@ -366,6 +375,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) head->Size = newSize; // Edit first header head->File = File; head->Line = Line; + head->ValidSize = __size; // Create new footer foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) ); foot->Head = head; @@ -395,6 +405,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) nextHead->Size = newSize; nextHead->File = File; nextHead->Line = Line; + nextHead->ValidSize = __size; // Get 2nd (old) footer foot = (void*)( (Uint)nextHead + newSize ); foot->Head = nextHead; @@ -416,6 +427,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) nextHead -= 1; nextHead->File = File; nextHead->Line = Line; + nextHead->ValidSize = __size; memcpy( nextHead->Data, @@ -473,8 +485,8 @@ void Heap_Dump(void) while( (Uint)head < (Uint)gHeapEnd ) { foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) ); - Log_Log("Heap", "%p (0x%llx): 0x%08lx %4C", - head, MM_GetPhysAddr((Uint)head), head->Size, &head->Magic); + Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C", + head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic); Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic); if(head->File) { Log_Log("Heap", "%sowned by %s:%i", @@ -523,8 +535,8 @@ void Heap_Dump(void) head = foot->Head; while( (tVAddr)head >= (tVAddr)badHead ) { - Log_Log("Heap", "%p (0x%llx): 0x%08lx %4C", - head, MM_GetPhysAddr((Uint)head), head->Size, &head->Magic); + Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C", + head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic); Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic); if(head->File) Log_Log("Heap", "%sowned by %s:%i", diff --git a/Kernel/vfs/open.c b/Kernel/vfs/open.c index a396465b..118380fd 100644 --- a/Kernel/vfs/open.c +++ b/Kernel/vfs/open.c @@ -191,8 +191,8 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) LEAVE('p', curNode); return curNode; } - // For root we always fast return + // For root we always fast return if(Path[0] == '/' && Path[1] == '\0') { if(TruePath) { *TruePath = malloc( gVFS_RootMount->MountPointLen+1 ); @@ -202,7 +202,7 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) return gVFS_RootMount->RootNode; } - // Check if there is anything mounted + // Check if there is an`ything mounted if(!gVFS_Mounts) { Warning("WTF! There's nothing mounted?"); return NULL; @@ -260,7 +260,7 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) curNode->ReferenceCount ++; // Parse Path ofs = mnt->MountPointLen+1; - for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs = nextSlash + 1) + for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1) { char pathEle[nextSlash+1]; @@ -319,11 +319,19 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) free(*TruePath); *TruePath = NULL; } - tmp = malloc( curNode->Size + 1 ); if(!curNode->Read) { Warning("VFS_ParsePath - Read of node %p is NULL (%s)", curNode, Path); if(curNode->Close) curNode->Close(curNode); + // No need to free *TruePath, see above + LEAVE('n'); + return NULL; + } + + tmp = malloc( curNode->Size + 1 ); + if(!tmp) { + Log_Warning("VFS", "VFS_ParsePath - Malloc failure"); + // No need to free *TruePath, see above LEAVE('n'); return NULL; } @@ -333,12 +341,13 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) // Parse Symlink Path curNode = VFS_ParsePath(tmp, TruePath); if(TruePath) - Log_Debug("VFS", "*TruePath='%s'", *TruePath); + LOG("VFS", "*TruePath='%s'", *TruePath); // Error Check if(!curNode) { - Log("Symlink fail '%s'", tmp); + Log_Debug("VFS", "Symlink fail '%s'", tmp); free(tmp); // Free temp string + if(TruePath) free(TruePath); LEAVE('n'); return NULL; } @@ -367,11 +376,12 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) if(!TruePath) continue; // Increase buffer space - tmp = realloc( *TruePath, retLength + strlen(&Path[ofs]) + 1 + 1 ); + tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 ); // Check if allocation succeeded if(!tmp) { Warning("VFS_ParsePath - Unable to reallocate true path buffer"); free(*TruePath); + *TruePath = NULL; if(curNode->Close) curNode->Close(curNode); LEAVE('n'); return NULL; @@ -380,8 +390,11 @@ tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath) // Append to path (*TruePath)[retLength] = '/'; strcpy(*TruePath+retLength+1, pathEle); + + LOG("*TruePath = '%s'\n", *TruePath); + // - Extend Path - retLength += nextSlash; + retLength += nextSlash + 1; } // Get last node -- 2.20.1