From c91586a66f18ca4f53855a75966ea9de0156956b Mon Sep 17 00:00:00 2001 From: georges <> Date: Fri, 20 Oct 2000 04:59:33 +0000 Subject: [PATCH] more error checking --- panda/src/ipc/ipc_nspr_traits.h | 46 +++++++++++++++++---------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/panda/src/ipc/ipc_nspr_traits.h b/panda/src/ipc/ipc_nspr_traits.h index bb15a85c75..eabcfb34b2 100644 --- a/panda/src/ipc/ipc_nspr_traits.h +++ b/panda/src/ipc/ipc_nspr_traits.h @@ -225,36 +225,38 @@ public: INLINE void close(void) { if (_fhandle != (PRFileDesc *)NULL) { PR_Close(_fhandle); _fhandle = (PRFileDesc *)NULL; } } - INLINE bool empty(void){ - return (PR_Available(_fhandle) <= 0); - } - INLINE int readin(string &block, int numBytes) { + INLINE bool empty(void){ return (PR_Available(_fhandle) <= 0); } + + INLINE int readin(string &block, int numBytes) { //Don't try and read less than nothing + if (_fhandle == (PRFileDesc *)NULL) + return 0; + nassertr(numBytes >= 0, 0); - int numRead; + char* temp = new char[numBytes]; - if (_fhandle != (PRFileDesc *)NULL){ - numRead = PR_Read(_fhandle, temp, numBytes); - if(numRead<=0) { + int numRead = PR_Read(_fhandle, temp, numBytes); + if(numRead<=0) { cerr << "PR_Read() in libnspr failed!\n"; + delete temp; return 0; - } - block.assign(temp, numRead); - delete temp; - return numRead; - } - return 0; - } + } + block.assign(temp, numRead); + delete temp; + return numRead; + } + INLINE int writeout(const string &block) { - //Make sure there is something to write out - nassertr(block.size() > 0, 0); - if (_fhandle != (PRFileDesc *)NULL) { + nassertr(block.size() > 0, 0); //Make sure there is something to write out + nassertr(block.data() != NULL, 0); + + if (_fhandle == (PRFileDesc *)NULL) + return 0; return PR_Write(_fhandle, (void*)block.data(), block.size()); - } - return 0; } - private: - string _filename; + + private: + string _filename; PRFileDesc* _fhandle; };