From 06a3eb8135ecb748c998e6827d20338158526d12 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 19 Jul 2012 15:51:24 +0800 Subject: [PATCH] Modules/Ext2 - Fixed zero-length filenames and memory leaks - DiskTool - Misc fixes --- KernelLand/Kernel/include/vfs.h | 1 + KernelLand/Modules/Filesystems/Ext2/dir.c | 23 ++++++++++++++-------- KernelLand/Modules/Filesystems/Ext2/ext2.c | 11 +++++++++-- Tools/DiskTool/src/include/acess.h | 8 -------- Tools/DiskTool/src/include/mutex.h | 19 ++++++++++++++++++ Tools/DiskTool/src/logging.c | 3 +++ 6 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 Tools/DiskTool/src/include/mutex.h diff --git a/KernelLand/Kernel/include/vfs.h b/KernelLand/Kernel/include/vfs.h index 6afe309a..98bc0275 100644 --- a/KernelLand/Kernel/include/vfs.h +++ b/KernelLand/Kernel/include/vfs.h @@ -20,6 +20,7 @@ #define _VFS_H #include +#include /** * \brief Thread list datatype for VFS_Select diff --git a/KernelLand/Modules/Filesystems/Ext2/dir.c b/KernelLand/Modules/Filesystems/Ext2/dir.c index 30cc86ed..4039a7c0 100644 --- a/KernelLand/Modules/Filesystems/Ext2/dir.c +++ b/KernelLand/Modules/Filesystems/Ext2/dir.c @@ -61,14 +61,14 @@ char *Ext2_ReadDir(tVFS_Node *Node, int Pos) Ext2_int_ReadInode(disk, Node->Inode, &inode); size = inode.i_size; - LOG("inode.i_block[0] = 0x%x", inode.i_block[0]); + LOG("inode={.i_block[0]= 0x%x, .i_size=0x%x}", inode.i_block[0], inode.i_size); // Find Entry // Get First Block // - Do this ourselves as it is a simple operation Base = inode.i_block[0] * disk->BlockSize; // Scan directory - while(Pos -- && size > 0) + while(Pos -- && size > 0 && size <= inode.i_size) { VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent); ofs += dirent.rec_len; @@ -83,22 +83,29 @@ char *Ext2_ReadDir(tVFS_Node *Node, int Pos) } ofs = 0; Base = Ext2_int_GetBlockAddr( disk, inode.i_block, block ); + if( Base == 0 ) { + size = 0; + break; + } } } // Check for the end of the list - if(size <= 0) { + if(size <= 0 || size > inode.i_size) { LEAVE('n'); return NULL; } // Read Entry VFS_ReadAt( disk->FD, Base+ofs, sizeof(tExt2_DirEnt), &dirent ); - //LOG("dirent.inode = %i", dirent.inode); - //LOG("dirent.rec_len = %i", dirent.rec_len); - //LOG("dirent.name_len = %i", dirent.name_len); + LOG("dirent={.rec_len=%i,.inode=0x%x,.name_len=%i}", + dirent.rec_len, dirent.inode, dirent.name_len); dirent.name[ dirent.name_len ] = '\0'; // Cap off string + if( dirent.name_len == 0 ) { + LEAVE('p', VFS_SKIP); + return VFS_SKIP; + } // Ignore . and .. (these are done in the VFS) if( (dirent.name[0] == '.' && dirent.name[1] == '\0') @@ -342,7 +349,8 @@ tVFS_Node *Ext2_int_CreateNode(tExt2_Disk *Disk, Uint InodeID) if( (tmpNode = Inode_GetCache(Disk->CacheID, InodeID)) ) return tmpNode; - + + memset(&retNode, 0, sizeof(retNode)); // Set identifiers retNode.Inode = InodeID; @@ -350,7 +358,6 @@ tVFS_Node *Ext2_int_CreateNode(tExt2_Disk *Disk, Uint InodeID) // Set file length retNode.Size = inode.i_size; - retNode.Data = NULL; // Set Access Permissions retNode.UID = inode.i_uid; diff --git a/KernelLand/Modules/Filesystems/Ext2/ext2.c b/KernelLand/Modules/Filesystems/Ext2/ext2.c index f8f942d5..5c9f97a5 100644 --- a/KernelLand/Modules/Filesystems/Ext2/ext2.c +++ b/KernelLand/Modules/Filesystems/Ext2/ext2.c @@ -221,9 +221,16 @@ void Ext2_CloseFile(tVFS_Node *Node) } int was_not_referenced = (Node->ImplInt == 0); - if( Inode_UncacheNode(disk->CacheID, Node->Inode) && was_not_referenced ) + tVFS_ACL *acls = Node->ACLs; + if( Inode_UncacheNode(disk->CacheID, Node->Inode) ) { - // Remove inode + if( was_not_referenced ) + { + // Remove inode + } + if( acls != &gVFS_ACL_EveryoneRW ) { + free(acls); + } } return ; } diff --git a/Tools/DiskTool/src/include/acess.h b/Tools/DiskTool/src/include/acess.h index 6b74dfcd..ff2347bd 100644 --- a/Tools/DiskTool/src/include/acess.h +++ b/Tools/DiskTool/src/include/acess.h @@ -49,7 +49,6 @@ typedef uint32_t tGID; typedef uint32_t tTID; // NOTE: Since this is single-threaded (for now) mutexes can be implimented as simple locks -typedef char tMutex; typedef char tShortSpinlock; typedef int64_t tTime; @@ -127,13 +126,6 @@ extern int WriteUTF8(Uint8 *str, Uint32 Val); extern int DivUp(int value, int divisor); extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); -static inline int Mutex_Acquire(tMutex *m) { - if(*m) Log_KernelPanic("---", "Double mutex lock"); - *m = 1; - return 0; -} -static inline void Mutex_Release(tMutex *m) { *m = 0; } - static inline void SHORTLOCK(tShortSpinlock *Lock) { if(*Lock) Log_KernelPanic("---", "Double short lock"); *Lock = 1; diff --git a/Tools/DiskTool/src/include/mutex.h b/Tools/DiskTool/src/include/mutex.h new file mode 100644 index 00000000..9fe1c4f8 --- /dev/null +++ b/Tools/DiskTool/src/include/mutex.h @@ -0,0 +1,19 @@ + +#ifndef _MUTEX_H_ +#define _MUTEX_H_ + +typedef struct { + void *LockerReturnAddr; +} tMutex; + +static inline int Mutex_Acquire(tMutex *m) { + if(m->LockerReturnAddr) + Log_KernelPanic("---", "Double mutex lock of %p by %p (was locked by %p)", + m, __builtin_return_address(0), m->LockerReturnAddr); + m->LockerReturnAddr = __builtin_return_address(0);; + return 0; +} +static inline void Mutex_Release(tMutex *m) { m->LockerReturnAddr = 0; } + +#endif + diff --git a/Tools/DiskTool/src/logging.c b/Tools/DiskTool/src/logging.c index 2b03c6ff..07d7a522 100644 --- a/Tools/DiskTool/src/logging.c +++ b/Tools/DiskTool/src/logging.c @@ -190,6 +190,9 @@ void Debug_TraceLeave(const char *Function, char Type, ...) case 'p': fprintf(stderr, " %p", va_arg(args, const void *)); break; + case 'n': + fprintf(stderr, " NULL"); + break; default: fprintf(stderr, " ?"); break; -- 2.20.1