FIX: strstream May Overwrite Adjacent Data
PSS ID Number: Q104680
Article last modified on 12-21-1993

7.00

MS-DOS


----------------------------------------------------------------------
The information in this article applies to:

 - Microsoft C/C++ compiler for MS-DOS, version 7.0
----------------------------------------------------------------------

SYMPTOMS
========

Writing to an strstream object in a C++ program may overwrite data
without any warning. The sample code below writes to an strstream and
continually checks to see whether the write has failed. Although no
failure is indicated, the strstream overwrites its buffer and writes
over adjacent data.

RESOLUTION
==========

You can work around this problem by calling the strstream constructor
that takes a buffer as an argument rather than the strstream
constructor that dynamically allocates a buffer. This allows you to
allocate a buffer of the size that your program needs, thus preventing
strstream from overwriting the dynamically allocated buffer.

STATUS
======

Microsoft has confirmed this to be a problem in Microsoft C/C++
version 7.0. This problem was corrected in Microsoft C/C++ version
8.0.

Sample Code
-----------

/* Compile options needed: /AL
*/

// WARNING: This sample program may hang your computer.

#include <strstrea.h>
#include <assert.h>
#include <stdlib.h>

int index;

void main ()
  {
  int * ptr1= new int[25000];
  assert(ptr1);
  strstream stream;
  int * ptr2= new int[25000];
  assert(ptr2);

  for (index= 0; index< 25000; index++)
    ptr2[index]= 0;

  cout << "\nWriting data to strstream object--please wait" << endl;
  for (index= 0; index< 2000; index++)
    {
    stream.write ("abcdefgh", 8);
    if (stream.fail ())
      {
      cout << "write failed" << endl;
      break;
      }
    }

  cout << "Done writing. Checking whether block2 is zero" << endl;
  for (index= 0; index< 25000; index++)
    {
    if (ptr2[index]!= 0)
      {
      cout << "Error! Strstream has written over data" << endl;
      exit(0);
      }
    }
  }

Additional reference words: 7.00
KBCategory: Tls
KBSubcategory:

=============================================================================

Copyright Microsoft Corporation 1993.
