From 5bde040295f3c50da86fbe18d61bacc71d8e436a Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 17 Nov 2011 13:11:09 +0100 Subject: [PATCH 1/2] Fixed randomness-related crash in snake. --- games/snake.cpp | 10 ++++++---- libmaxsi/random.cpp | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/games/snake.cpp b/games/snake.cpp index c1ac17e1..bea1690e 100644 --- a/games/snake.cpp +++ b/games/snake.cpp @@ -56,8 +56,8 @@ void Reset() case 3: velx = 0; vely = -1; break; } - animalx = 2 + (rand() % width-4); - animaly = 2 + (rand() % height-4); + animalx = 2 + (rand() % (width-4)); + animaly = 2 + (rand() % (height-4)); taillen = 0; tailmax = 3; @@ -141,8 +141,10 @@ void Update() if ( newx == animalx && newy == animaly ) { tailmax++; - animalx = 2 + (rand() % width-4); - animaly = 2 + (rand() % height-4); + animalx = 2 + (rand() % (width-4)); + animaly = 2 + (rand() % (height-4)); + ASSERT(0 <= animalx && animalx < width); + ASSERT(0 <= animaly && animaly < height); if ( maxspeed < speed ) { speed += speedincrease; } } diff --git a/libmaxsi/random.cpp b/libmaxsi/random.cpp index 63daa8a4..9bbef8de 100644 --- a/libmaxsi/random.cpp +++ b/libmaxsi/random.cpp @@ -28,11 +28,11 @@ namespace Maxsi { namespace Random { - int random_seed=1337; + unsigned random_seed = 1337; extern "C" int rand() { random_seed = random_seed + 37 * 1103515245 + 12345; - return random_seed / 65536; + return random_seed >> 16; } } } From 05196f49b25d704b27e5e97b3af978e945c43d7f Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 17 Nov 2011 20:34:04 +0100 Subject: [PATCH 2/2] Added dup(2). --- libmaxsi/c/hsrc/unistd.h | 2 +- libmaxsi/io.cpp | 5 +++++ sortix/io.cpp | 9 +++++++++ sortix/syscallnum.h | 3 ++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index f150ce2b..25e3b506 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -84,7 +84,6 @@ int close(int); size_t confstr(int, char*, size_t); char* crypt(const char*, const char*); char* ctermid(char*); -int dup(int); int dup2(int, int); void encrypt(char [64], int); int execl(const char*, const char*, ...); @@ -159,6 +158,7 @@ extern char* optarg; extern int opterr, optind, optopt; #endif +int dup(int); void _exit(int); pid_t fork(void); pid_t getpid(void); diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index 6217652c..0e6126cc 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -33,6 +33,7 @@ namespace Maxsi DEFN_SYSCALL3(ssize_t, SysRead, 18, int, void*, size_t); DEFN_SYSCALL3(ssize_t, SysWrite, 19, int, const void*, size_t); DEFN_SYSCALL1(int, SysPipe, 20, int*); + DEFN_SYSCALL1(int, SysDup, 21, int); size_t Print(const char* Message) { @@ -78,6 +79,10 @@ namespace Maxsi return SysPipe(pipefd); } + extern "C" int dup(int fd) + { + return SysDup(fd); + } #endif } diff --git a/sortix/io.cpp b/sortix/io.cpp index 75bba0f4..f0f7b0be 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -117,10 +117,19 @@ namespace Sortix return 0; } + int SysDup(int fd) + { + Process* process = CurrentProcess(); + Device* dev = process->descriptors.Get(fd); + if ( !dev ) { return -1; /* TODO: EBADF */ } + return process->descriptors.Allocate(dev); + } + void Init() { Syscall::Register(SYSCALL_WRITE, (void*) SysWrite); Syscall::Register(SYSCALL_READ, (void*) SysRead); + Syscall::Register(SYSCALL_DUP, (void*) SysDup); } } } diff --git a/sortix/syscallnum.h b/sortix/syscallnum.h index c420ea7f..73b7708d 100644 --- a/sortix/syscallnum.h +++ b/sortix/syscallnum.h @@ -46,7 +46,8 @@ #define SYSCALL_READ 18 #define SYSCALL_WRITE 19 #define SYSCALL_PIPE 20 -#define SYSCALL_MAX_NUM 21 /* index of highest constant + 1 */ +#define SYSCALL_DUP 21 +#define SYSCALL_MAX_NUM 22 /* index of highest constant + 1 */ #endif