@@ -16,6 +16,7 @@ limitations under the License.
1616#include < cstdio>
1717#include < fstream>
1818#include < iostream>
19+ #include < limits>
1920#include " ../io/CompressedInputStream.hpp"
2021#include " ../io/CompressedOutputStream.hpp"
2122#include " ../io/IOException.hpp"
@@ -202,6 +203,43 @@ uint64 compress5(kanzi::byte block[], uint length)
202203 return res;
203204}
204205
206+ uint64 compress6 (kanzi::byte block[])
207+ {
208+ CompressedOutputStream* cos = nullptr ;
209+ CompressedInputStream* cis = nullptr ;
210+ uint64 res;
211+
212+ try {
213+ cout << " Test - large read request does not overflow streamsize" << endl;
214+ stringbuf buffer;
215+ iostream ios (&buffer);
216+ cos = new CompressedOutputStream (ios, 1 , " HUFFMAN" , " TEXT" );
217+ cos->write ((const char *)block, 1 );
218+ cos->close ();
219+ ios.seekg (0 );
220+ cis = new CompressedInputStream (ios, 1 );
221+ char decoded[1 ] = { 0 };
222+ const streamsize requested = streamsize (numeric_limits<int >::max ()) + streamsize (1 );
223+ cis->read (decoded, requested);
224+
225+ if ((cis->gcount () != 1 ) || (decoded[0 ] != char (block[0 ]))) {
226+ cout << " Failure: invalid read result for large request" << endl;
227+ res = 1 ;
228+ }
229+ else {
230+ res = 0 ;
231+ }
232+ }
233+ catch (const ios_base::failure& e) {
234+ cout << " Failure: unexpected exception " << e.what () << endl;
235+ res = 1 ;
236+ }
237+
238+ delete cos;
239+ delete cis;
240+ return res;
241+ }
242+
205243int testCorrectness (int , const char *[])
206244{
207245 // Test correctness
@@ -240,6 +278,9 @@ int testCorrectness(int, const char*[])
240278 cres = compress5 (values, length);
241279 cout << ((cres == 0 ) ? " Success" : " Failure" ) << endl;
242280 res &= (cres == 0 );
281+ cres = compress6 (values);
282+ cout << ((cres == 0 ) ? " Success" : " Failure" ) << endl;
283+ res &= (cres == 0 );
243284 }
244285 }
245286
0 commit comments