From 1743bd0eacec94c6f6e3c89e16d0bd6d301017e8 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 29 Dec 2008 21:46:03 +0000 Subject: Corrected the premature end of stream read corner case in Taf...it was freaking out and allocating all memory, now it just throws an exception. --- src/tafreader.cpp | 23 ++++++++++++++++++++--- src/tafreader.h | 1 + src/unit/taf.cpp | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/tafreader.cpp b/src/tafreader.cpp index 7bb0933..4408cce 100644 --- a/src/tafreader.cpp +++ b/src/tafreader.cpp @@ -209,15 +209,32 @@ void Bu::TafReader::next() iCol++; if( c == '}' ) { - sIn.read( &c, 1 ); + rawread( &c ); if( c != '}' ) - sIn.read( &la, 1 ); + rawread( &la ); } else { c = la; if( c != '}' ) - sIn.read( &la, 1 ); + rawread( &la ); + } +} + +void Bu::TafReader::rawread( char *c ) +{ + if( sIn.read( c, 1 ) < 1 ) + { + if( sIn.isEOS() ) + { + throw TafException("%d:%d: Premature end of stream.", + iLine, iCol, c ); + } + else + { + throw TafException("%d:%d: No data read, but not end of stream?", + iLine, iCol, c ); + } } } diff --git a/src/tafreader.h b/src/tafreader.h index ce67a5f..53ab6d4 100644 --- a/src/tafreader.h +++ b/src/tafreader.h @@ -35,6 +35,7 @@ namespace Bu bool isws(); void next(); Bu::FString readStr(); + void rawread( char *c ); char c, la; Bu::Stream &sIn; int iLine, iCol; diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp index 3bf7cc6..e4b3ccc 100644 --- a/src/unit/taf.cpp +++ b/src/unit/taf.cpp @@ -23,6 +23,7 @@ public: addTest( Unit::read1 ); addTest( Unit::encode1 ); addTest( Unit::emptyStr1 ); + addTest( Unit::incomplete1 ); } virtual ~Unit() @@ -108,6 +109,21 @@ public: unitTest( mb.getString() == "{\"Test Group\":\n \"Lame\"=\"\"\n}\n" ); } + + void incomplete1() + { + try + { + Bu::MemBuf mb("{Lame: \"Hello=\""); + Bu::TafReader tr( mb ); + delete tr.readGroup(); + unitFailed("Should have thrown an exception, didn't."); + } + catch( Bu::TafException &e ) + { + // Woot + } + } }; int main( int argc, char *argv[] ) -- cgit v1.2.3