tdf#150135: Fix OTextInputStream to throw runtime exception when uninitialized
New method checkNull is added to the class to throw a runtime error if mxStream is uninitialized. It is run in the beginning of all methods that require an initialized mxStream.
Change-Id: Ia1f15d90c79c71b2a2350d9b60ef1bf68fb9009c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148819
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
(cherry picked from commit 16242898da50fbf680df558cb47d1978c3304572)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148872
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
diff --git a/io/source/TextInputStream/TextInputStream.cxx b/io/source/TextInputStream/TextInputStream.cxx
index 9f7c884..1ce12a6 100644
--- a/io/source/TextInputStream/TextInputStream.cxx
+++ b/io/source/TextInputStream/TextInputStream.cxx
@@ -72,6 +72,8 @@ class OTextInputStream : public WeakImplHelper< XTextInputStream2, XServiceInfo
/// @throws IOException
/// @throws RuntimeException
sal_Int32 implReadNext();
/// @throws RuntimeException
void checkNull();
public:
OTextInputStream();
@@ -122,22 +124,33 @@ OTextInputStream::~OTextInputStream()
}
}
// Check uninitialized object
void OTextInputStream::checkNull()
{
if (mxStream==nullptr){
throw RuntimeException("Uninitialized object");
}
}
// XTextInputStream
OUString OTextInputStream::readLine( )
{
checkNull();
static Sequence< sal_Unicode > aDummySeq;
return implReadString( aDummySeq, true, true );
}
OUString OTextInputStream::readString( const Sequence< sal_Unicode >& Delimiters, sal_Bool bRemoveDelimiter )
{
checkNull();
return implReadString( Delimiters, bRemoveDelimiter, false );
}
sal_Bool OTextInputStream::isEOF()
{
checkNull();
bool bRet = false;
if( mnCharsInBuffer == 0 && mbReachedEOF )
bRet = true;
@@ -337,26 +350,31 @@ void OTextInputStream::setEncoding( const OUString& Encoding )
sal_Int32 OTextInputStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
{
checkNull();
return mxStream->readBytes( aData, nBytesToRead );
}
sal_Int32 OTextInputStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
{
checkNull();
return mxStream->readSomeBytes( aData, nMaxBytesToRead );
}
void OTextInputStream::skipBytes( sal_Int32 nBytesToSkip )
{
checkNull();
mxStream->skipBytes( nBytesToSkip );
}
sal_Int32 OTextInputStream::available( )
{
checkNull();
return mxStream->available();
}
void OTextInputStream::closeInput( )
{
checkNull();
mxStream->closeInput();
}