From e72d086a8f4231a4ed6c49a9dc5fc97b4c807f5e Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 23 Nov 2011 17:51:18 +0100 Subject: [PATCH 1/3] Disallow / in filenames in ramfs. --- sortix/fs/ramfs.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sortix/fs/ramfs.cpp b/sortix/fs/ramfs.cpp index b3e36401..f29ab6bc 100644 --- a/sortix/fs/ramfs.cpp +++ b/sortix/fs/ramfs.cpp @@ -250,6 +250,14 @@ namespace Sortix return NULL; } + if ( *path++ != '/' ) { Error::Set(ENOENT); return NULL; } + + size_t pathlen = String::Length(path); + for ( size_t i = 0; i < pathlen; i++ ) + { + if ( path[i] == '/' ) { Error::Set(ENOENT); return NULL; } + } + DevBuffer* file = OpenFile(path, flags, mode); if ( !file ) { return NULL; } Device* wrapper = new DevFileWrapper(file, flags); @@ -259,8 +267,6 @@ namespace Sortix DevBuffer* DevRAMFS::OpenFile(const char* path, int flags, mode_t mode) { - if ( *path++ != '/' ) { Error::Set(ENOENT); return NULL; } - // Hack to prevent / from being a filename. if ( path == 0 ) { Error::Set(ENOENT); return NULL; } @@ -314,6 +320,7 @@ namespace Sortix return false; } + if ( !files ) { Error::Set(ENOENT); return false; } size_t index = files->Search(LookupFile, path); if ( index == SIZE_MAX ) { Error::Set(ENOENT); return false; } From 4890c306c4a4f1b615638d24641b29be2757731b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 24 Nov 2011 10:26:36 +0100 Subject: [PATCH 2/3] printf(3) now writes to fd 1. --- libmaxsi/c/hsrc/features.h | 10 ++++++++-- libmaxsi/c/hsrc/unistd.h | 5 ++++- libmaxsi/io.cpp | 25 ++++++++++++++++++++++--- utils/cat.cpp | 16 +--------------- utils/cp.cpp | 16 +--------------- utils/editor.cpp | 16 +--------------- utils/init.cpp | 5 +++++ 7 files changed, 42 insertions(+), 51 deletions(-) diff --git a/libmaxsi/c/hsrc/features.h b/libmaxsi/c/hsrc/features.h index 8d0e28fc..4a2c9c26 100644 --- a/libmaxsi/c/hsrc/features.h +++ b/libmaxsi/c/hsrc/features.h @@ -43,9 +43,15 @@ #define __POSIX_OBSOLETE 200112L #endif -#include +/* Whether sortix-specific extensions to the C library are available. */ +#ifndef SORTIX_NO_EXTENSIONS +#define SORTIX_EXTENSIONS +#endif -// Don't provide things from standard headers that is not implemented in libmaxsi/sortix. +/* Don't provide things from standard headers that is not implemented. */ #define SORTIX_UNIMPLEMENTED +#include + + #endif diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index 079f7d4b..d538dfd8 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -15,7 +15,7 @@ more details. You should have received a copy of the GNU Lesser General Public License - along with LibMaxsi. If not, see . + along with LibMaxsi. If not, see . unistd.h The header defines miscellaneous symbolic constants and types, @@ -170,6 +170,9 @@ int usleep(useconds_t useconds); #endif int unlink(const char*); ssize_t write(int, const void*, size_t); +#ifdef SORTIX_EXTENSIONS +int writeall(int fd, const void* buffer, size_t len); +#endif __END_DECLS diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index 1eb06f3f..cd907fe1 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -26,7 +26,9 @@ #include "syscall.h" #include "io.h" #include "format.h" +#include "string.h" #include +#include namespace Maxsi { @@ -42,14 +44,17 @@ namespace Maxsi DEFN_SYSCALL2(char*, SysGetCWD, 26, char*, size_t); DEFN_SYSCALL1(int, SysUnlink, 27, const char*); - size_t Print(const char* Message) + size_t Print(const char* string) { - return SysPrint(Message); + size_t stringlen = String::Length(string); + if ( writeall(1, string, stringlen) ) { return 0; } + return stringlen; } size_t PrintCallback(void* user, const char* string, size_t stringlen) { - return SysPrint(string); + if ( writeall(1, string, stringlen) ) { return 0; } + return stringlen; } size_t PrintF(const char* format, ...) @@ -81,6 +86,20 @@ namespace Maxsi return SysWrite(fd, buf, count); } + extern "C" int writeall(int fd, const void* buffer, size_t len) + { + const char* buf = (const char*) buffer; + while ( len ) + { + ssize_t byteswritten = write(fd, buf, len); + if ( byteswritten < 0 ) { return (int) byteswritten; } + buf += byteswritten; + len -= byteswritten; + } + + return 0; + } + extern "C" int pipe(int pipefd[2]) { return SysPipe(pipefd); diff --git a/utils/cat.cpp b/utils/cat.cpp index 8722535e..e98475b5 100644 --- a/utils/cat.cpp +++ b/utils/cat.cpp @@ -5,20 +5,6 @@ #include #include -bool writeall(int fd, const void* buffer, size_t len) -{ - const char* buf = (const char*) buffer; - while ( len ) - { - ssize_t byteswritten = write(fd, buf, len); - if ( byteswritten < 0 ) { return false; } - buf += byteswritten; - len -= byteswritten; - } - - return true; -} - int cat(int argc, char* argv[]) { int result = 0; @@ -48,7 +34,7 @@ int cat(int argc, char* argv[]) result = 1; break; } - if ( !writeall(outfd, buffer, bytesread) ) + if ( writeall(outfd, buffer, bytesread) ) { printf("%s: /dev/tty: %s\n", argv[0], strerror(errno)); result = 1; diff --git a/utils/cp.cpp b/utils/cp.cpp index 974235ca..acbbba4f 100644 --- a/utils/cp.cpp +++ b/utils/cp.cpp @@ -4,20 +4,6 @@ #include #include -bool writeall(int fd, const void* buffer, size_t len) -{ - const char* buf = (const char*) buffer; - while ( len ) - { - ssize_t byteswritten = write(fd, buf, len); - if ( byteswritten < 0 ) { return false; } - buf += byteswritten; - len -= byteswritten; - } - - return true; -} - const char* basename(const char* path) { size_t len = strlen(path); @@ -62,6 +48,6 @@ int main(int argc, char* argv[]) ssize_t bytesread = read(fromfd, buffer, BUFFER_SIZE); if ( bytesread < 0 ) { printf("%s: %s: %s\n", argv[0], frompath, strerror(errno)); return 1; } if ( bytesread == 0 ) { return 0; } - if ( !writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; } + if ( writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; } } } diff --git a/utils/editor.cpp b/utils/editor.cpp index a242d906..ac16bf34 100644 --- a/utils/editor.cpp +++ b/utils/editor.cpp @@ -192,20 +192,6 @@ unsigned confirmquit() } } -bool writeall(int fd, const void* buffer, size_t len) -{ - const char* buf = (const char*) buffer; - while ( len ) - { - ssize_t byteswritten = write(fd, buf, len); - if ( byteswritten < 0 ) { return false; } - buf += byteswritten; - len -= byteswritten; - } - - return true; -} - bool savetofile(const char* path) { int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0777); @@ -215,7 +201,7 @@ bool savetofile(const char* path) { size_t len = strlen(buffers[y]); buffers[y][len] = '\n'; - bool result = writeall(fd, buffers[y], len+1); + bool result = !writeall(fd, buffers[y], len+1); buffers[y][len] = 0; if ( !result ) { printf("%s: %s\n", path, strerror(errno)); close(fd); return false; } } diff --git a/utils/init.cpp b/utils/init.cpp index 1c83839c..b9aaf4c4 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,10 @@ int child() int main(int argc, char* argv[]) { + if ( open("/dev/tty", O_RDONLY) != 0 ) { return 2; } + if ( open("/dev/tty", O_WRONLY | O_APPEND) != 1 ) { return 2; } + if ( open("/dev/tty", O_WRONLY | O_APPEND) != 2 ) { return 2; } + // Reset the terminal's color and the rest of it. printf("\r\e[m\e[J"); From 7aff4761177c09eaa2c4b4fa8f776f950769a2b8 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 24 Nov 2011 10:41:13 +0100 Subject: [PATCH 3/3] Fixed bug in the remove function of the sorted list class. --- libmaxsi/hsrc/sortedlist.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libmaxsi/hsrc/sortedlist.h b/libmaxsi/hsrc/sortedlist.h index 4d57e67f..036a7e57 100644 --- a/libmaxsi/hsrc/sortedlist.h +++ b/libmaxsi/hsrc/sortedlist.h @@ -147,8 +147,9 @@ namespace Maxsi T Remove(size_t index) { if ( !(flags & FLAG_SORTED) ) { Sort(); } + ASSERT(index < listused); - T result = list[listused-1]; + T result = list[index]; if ( index == listused-1 ) {