From d2f9a689ac0d3f285e776b79f80e42975c90798f Mon Sep 17 00:00:00 2001 From: Mark Cave-Ayland Date: Sat, 1 May 2010 09:48:57 +0000 Subject: [PATCH] Add checks to the read_io, seek_io and close_io routines to ensure that they do not perform any actions if an invalid file handle (-1) is passed. This resolves the issue with extra arguments being left on the Forth stack when an invalid device access is attempted. Signed-off-by: Mark Cave-Ayland git-svn-id: svn://coreboot.org/openbios/trunk/openbios-devel@764 f158a5a8-5612-0410-a976-696ce0be7e32 --- libc/diskio.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/libc/diskio.c b/libc/diskio.c index a204ece..a8aac6d 100644 --- a/libc/diskio.c +++ b/libc/diskio.c @@ -164,27 +164,40 @@ get_fstype( int fd ) int read_io( int fd, void *buf, size_t cnt ) { - priv_fd_t *fdp = file_descriptors[fd]; + priv_fd_t *fdp; ucell ret; - PUSH( (ucell)buf ); - PUSH( cnt ); - call_package( fdp->read_xt, fdp->ih ); - ret = POP(); + if (fd != -1) { + fdp = file_descriptors[fd]; - if( !ret && cnt ) + PUSH( (ucell)buf ); + PUSH( cnt ); + call_package( fdp->read_xt, fdp->ih ); + ret = POP(); + + if( !ret && cnt ) + ret = -1; + } else { ret = -1; + } + return ret; } int seek_io( int fd, llong offs ) { - priv_fd_t *fdp = file_descriptors[fd]; + priv_fd_t *fdp; - DPUSH( offs ); - call_package( fdp->seek_xt, fdp->ih ); - return ((((cell)POP()) >= 0)? 0 : -1); + if (fd != -1) { + fdp = file_descriptors[fd]; + + DPUSH( offs ); + call_package( fdp->seek_xt, fdp->ih ); + return ((((cell)POP()) >= 0)? 0 : -1); + } else { + return -1; + } } llong @@ -203,13 +216,17 @@ tell( int fd ) int close_io( int fd ) { - priv_fd_t *fdp = file_descriptors[fd]; + priv_fd_t *fdp; - if( fdp->do_close ) - close_dev( fdp->ih ); - free( fdp ); + if (fd != -1) { + fdp = file_descriptors[fd]; - file_descriptors[fd]=NULL; + if( fdp->do_close ) + close_dev( fdp->ih ); + free( fdp ); + + file_descriptors[fd]=NULL; + } return 0; }