From 92f66b7f92fef1e80c101ed61da25f07bce60900 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marc-Andr=C3=A9=20Laverdi=C3=A8re?= <marc-andre@atc.tcs.com>
Date: Thu, 13 Dec 2012 10:40:26 +0530
Subject: [PATCH 1/2] Defaulting to 0 on bad stream read (WIP)

* Updated SvStream::operator>>
* Updated related unit tests
* Added unit tests for extra data types

Change-Id: I2f7535a6f3c0fbad1499f39644e2944c1b735dae
---
 tools/qa/cppunit/test_stream.cxx |   60 ++++++++++++++++++++++++++++++++++----
 tools/source/stream/stream.cxx   |   24 +++++++++++++++
 2 files changed, 79 insertions(+), 5 deletions(-)

diff --git a/tools/qa/cppunit/test_stream.cxx b/tools/qa/cppunit/test_stream.cxx
index b0aeb03..749a99a 100644
--- a/tools/qa/cppunit/test_stream.cxx
+++ b/tools/qa/cppunit/test_stream.cxx
@@ -85,8 +85,11 @@ namespace
         iss >> std_a;
         //so, now eof is set
         CPPUNIT_ASSERT(iss.eof());
-        //a failed read doesn't change the data, it remains unchanged
-        CPPUNIT_ASSERT(std_a == 78);
+        //WARNING WARNING WARNING
+        //Behavior change
+        //Old behavior: a failed read doesn't change the data, it remains unchanged
+        //New behavior: a failed read makes the variable set to zero
+        CPPUNIT_ASSERT(std_a == 0);
         //nothing wrong with the stream, so not bad
         CPPUNIT_ASSERT(!iss.bad());
         //yet, the read didn't succeed
@@ -97,16 +100,23 @@ namespace
         aMemStream >> tools_a;
         //so, now eof is set
         CPPUNIT_ASSERT(aMemStream.eof());
+        //WARNING WARNING WARNING
+        //Behavior change, as above
         //a failed read doesn't change the data, it remains unchanged
-        CPPUNIT_ASSERT(tools_a == 78);
+        CPPUNIT_ASSERT(tools_a == 0);
         //nothing wrong with the stream, so not bad
         CPPUNIT_ASSERT(!aMemStream.bad());
         //yet, the read didn't succeed
         CPPUNIT_ASSERT(!aMemStream.good());
 
-        //set things up so that there is only one byte available on an attempt
+        //WARNING WARNING WARNING
+        //Behavior changed
+        //Old behavior: set things up so that there is only one byte available on an attempt
         //to read a two-byte sal_uInt16.  The byte should be consumed, but the
         //operation should fail, and tools_b should remain unchanged,
+        //New behavior: set things up so that there is only one byte available on an attempt
+        //to read a two-byte sal_uInt16.  The byte should be consumed, but the
+        //operation should fail, and tools_b should be zero
         sal_uInt16 tools_b = 0x1122;
         aMemStream.SeekRel(-1);
         CPPUNIT_ASSERT(!aMemStream.eof());
@@ -114,7 +124,47 @@ namespace
         aMemStream >> tools_b;
         CPPUNIT_ASSERT(!aMemStream.good());
         CPPUNIT_ASSERT(aMemStream.eof());
-        CPPUNIT_ASSERT(tools_b == 0x1122);
+        CPPUNIT_ASSERT(tools_b == 0);
+
+        //Repeat of the above with different data types
+        sal_uInt32 tools_u32 = 123456789;
+        aMemStream.SeekRel(-1);
+        CPPUNIT_ASSERT(!aMemStream.eof());
+        CPPUNIT_ASSERT(aMemStream.good());
+        aMemStream >> tools_u32;
+        CPPUNIT_ASSERT(!aMemStream.good());
+        CPPUNIT_ASSERT(aMemStream.eof());
+        CPPUNIT_ASSERT(tools_u32 == 0);
+
+
+        sal_Int32 tools_s32 = 123456789;
+        aMemStream.SeekRel(-1);
+        CPPUNIT_ASSERT(!aMemStream.eof());
+        CPPUNIT_ASSERT(aMemStream.good());
+        aMemStream >> tools_s32;
+        CPPUNIT_ASSERT(!aMemStream.good());
+        CPPUNIT_ASSERT(aMemStream.eof());
+        CPPUNIT_ASSERT(tools_s32 == 0);
+
+
+        sal_uInt64 tools_u64 = 1234567890123456;
+        aMemStream.SeekRel(-1);
+        CPPUNIT_ASSERT(!aMemStream.eof());
+        CPPUNIT_ASSERT(aMemStream.good());
+        aMemStream >> tools_u64;
+        CPPUNIT_ASSERT(!aMemStream.good());
+        CPPUNIT_ASSERT(aMemStream.eof());
+        CPPUNIT_ASSERT(tools_u64 == 0l);
+
+        sal_Int64 tools_s64 = 1234567890123456;
+        aMemStream.SeekRel(-1);
+        CPPUNIT_ASSERT(!aMemStream.eof());
+        CPPUNIT_ASSERT(aMemStream.good());
+        aMemStream >> tools_s64;
+        CPPUNIT_ASSERT(!aMemStream.good());
+        CPPUNIT_ASSERT(aMemStream.eof());
+        CPPUNIT_ASSERT(tools_s64 == 0l);
+
 
         iss.clear();
         iss.seekg(0);
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx
index 68c9dc9..4ac1139 100644
--- a/tools/source/stream/stream.cxx
+++ b/tools/source/stream/stream.cxx
@@ -909,6 +909,10 @@ SvStream& SvStream::operator>>(sal_uInt16& r)
             SwapUShort(n);
         r = n;
     }
+    else
+    {
+        r = 0;
+    }
     return *this;
 }
 
@@ -922,6 +926,10 @@ SvStream& SvStream::operator>>(sal_uInt32& r)
             SwapULong(n);
         r = n;
     }
+    else
+    {
+        r = 0;
+    }
     return *this;
 }
 
@@ -935,6 +943,10 @@ SvStream& SvStream::operator>>(sal_uInt64& r)
             SwapUInt64(n);
         r = n;
     }
+    else
+    {
+        r = 0l;
+    }
     return *this;
 }
 
@@ -948,6 +960,10 @@ SvStream& SvStream::operator>>(sal_Int16& r)
             SwapShort(n);
         r = n;
     }
+    else
+    {
+        r = 0;
+    }
     return *this;
 }
 
@@ -961,6 +977,10 @@ SvStream& SvStream::operator>>(sal_Int32& r)
             SwapLongInt(n);
         r = n;
     }
+    else
+    {
+        r = 0;
+    }
     return *this;
 }
 
@@ -974,6 +994,10 @@ SvStream& SvStream::operator>>(sal_Int64& r)
             SwapInt64(n);
         r = n;
     }
+    else
+    {
+        r = 0l;
+    }
     return *this;
 }
 
-- 
1.7.10.4

