FAQ "fix" seems very weak 
Author Message
 FAQ "fix" seems very weak

I was reading through the FAQ in the section labeled:

 "When I assign a shorter string to an existing string which contained a
longer string originally the assignment corrupts the heap. How do I fix
this problem?"

It shows the following example which causes the heap to get corrupted when
the last line is executed:

        string  str, str2;
        str = "abcdefghijklmnopqrstuvwxyzabcdefghij" ;
        str2 = str;
                //Workaround, uncomment the following line
                //str.erase() ;
        str = "zyxw" ;

I found the suggested fix rather weak (use xxx.erase()). I poked around in
xstring and found the root cause of this problem in _Copy (copying too
much). This problem could be encountered in other circumstances than the
example shown above so I corrected it in xstring.

This problem is very fundamental and makes the use of strings unreliable.
Certainly others have corrected this problem and presumedly other problems
as well. Could someone be so kind as to direct me to fixes to problems in
this version of the STL.

John Keenan



Tue, 03 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

Quote:

>... so I corrected it in xstring.

Can I just say man, you got cojones!  If my boss found out that I
re-wrote Bill's code without Bill's permission, risking our access to
Bill's technical support...

Well, I would probably have to start that consultancy I dream about.

So I sit and wait for Bill to fix his code for me :(



Tue, 03 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak



Quote:
> >On Fri, 17 Apr 1998 03:29:56 -0700, "John Keenan"

> >... so I corrected it in xstring.
> Can I just say man, you got cojones!  If my boss found out that I
> re-wrote Bill's code without Bill's permission, risking our access to
> Bill's technical support...

> Well, I would probably have to start that consultancy I dream about.

> So I sit and wait for Bill to fix his code for me :(

Your reply plays directly into my fundamental question which I will
elaborate.

The STL files supplied with Visual C++ 5.0 show a copyright of 1995... so
we are not dealing with a hot-off-the-press package here. The particular
problem I originally noted is very fundamental and can crop up in numerous
situations. The bug is in _Copy, which is used by _Grow, which is used by
append, assign, insert, erase, replace, reserve, and _Freeze, which in turn
are used by... identifying all the specific cases which will activate this
bug would be difficult (but admittedly not insurmountable). The point is
there *may* be many. Many user must have stumbled across this problem in
the past several years and I am interested in knowing what action was
taken. I have thought of the following cases:

  1) Everyone knows there is a more recent P.J. Plauger STL implementation.
It is at ?????
  2) Use a different STL implementation with Visual C++ 5.0
  3) Fix the root problem in the Visual C++ 5.0 supplied implementation
  4) Add workarounds in user code on a case by case basis
  5) Don't use STL with Visual C++ 5.0
  6) This is a none-issue, I've made a mountain out of a mole hill, stop
whining

 Actually I am extremelly new to STL (one week) and am trying to figure out
how to proceed. I would appreciate any user's guidance.

John Keenan



Tue, 03 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

I would like to echo John Keenan's query about what to do with Microsoft
code which is buggy and / or not very well implemented, especially as this
relates to the standard c++ classes.  There are a number of problems
I have come across and I too have wondered what other people have done to work
with them.  This xstring problem seems to be the worst of the bunch as it is
a real bug and will trash memory.

In response to John, I would suggest checking out other implementations of
the STL classes, especially one called STLport which is based on the SGI
implementation of the standard containers and algorithms of c++.  I also
think fixing the supplied code is workable, but that depends on the
environment you are in.

I do not know of a later Plauger STL implementation (which is really the HP
implementation just modified somewhat I believe) for MSVC.

I definitely don't think this is a non issue.  If more people complained
about these things, Microsoft might respond a bit more quickly.  I'm still
waiting for a compiler implementation that produces correct floating point
optimized code reliably, for instance.  Even after three iterations (SP3)
there are still problems.

-Eric Twietmeyer



Thu, 05 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

There is a later PJP version, but it's not free - check out
www.dinkumware.com for details.

HTH

: I do not know of a later Plauger STL implementation (which is really the HP
: implementation just modified somewhat I believe) for MSVC.

--
/*  Andrew  */
WWW: http://www.halcyon.com/ast        



Thu, 05 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

Quote:

>The STL files supplied with Visual C++ 5.0 show a copyright of 1995... so
>we are not dealing with a hot-off-the-press package here. The particular
>problem I originally noted is very fundamental and can crop up in numerous
>situations. The bug is in _Copy, which is used by _Grow, which is used by
>append, assign, insert, erase, replace, reserve, and _Freeze, which in turn
>are used by... identifying all the specific cases which will activate this
>bug would be difficult (but admittedly not insurmountable). The point is
>there *may* be many. Many user must have stumbled across this problem in
>the past several years and I am interested in knowing what action was
>taken. I have thought of the following cases:

>  1) Everyone knows there is a more recent P.J. Plauger STL implementation.
>It is at ?????
>  2) Use a different STL implementation with Visual C++ 5.0
>  3) Fix the root problem in the Visual C++ 5.0 supplied implementation
>  4) Add workarounds in user code on a case by case basis
>  5) Don't use STL with Visual C++ 5.0
>  6) This is a none-issue, I've made a mountain out of a mole hill, stop
>whining

> Actually I am extremelly new to STL (one week) and am trying to figure out
>how to proceed. I would appreciate any user's guidance.

>John Keenan


First I would like to point out that the std::string class is NOT part of
STL.  STL (Standard Template Library) consists of the containers, iterators,
and algorithms such as vector<>, map<>, set<>, lower_bound(), sort(),
find(), etc.  std::string came before STL, but has been modified to look
similar to the STL containers.

The reason why this distinction is important to the thread is that looking
for another STL implementation (such as SGI's) will not help solve the
problem John Keenan is talking about.  std::string is not supplied in SGI's
STL implementation.

I have used a version of SGI's STL implementation at home, and I must say
that it is pretty nice.  Although one doesn't typically read library source
code, when the need arises the SGI code is--gasp--readable.  Compared to the
source that MS ships with VC++ there is no contest.  Also, SGI has paid more
attention to exception and thread safety.

The version I use can be found at:  http://www.sirius.com/~ouchida/

It's not the most recent version, but it is the easiest to use with VC++.

John, could you elaborate on the bug fix that you made in xstring.h, I'm
considering implementing it here.

Thanks,

    Chris Hines
    The sooner you start coding, the longer it will take you to finish.



Fri, 06 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak



<snip>

Quote:
> John, could you elaborate on the bug fix that you made in xstring.h, I'm
> considering implementing it here.

The modification was to DevStudio\VC\include\xstring (not xstring.h). I use
// ??????? as my unique marker to help locate modified code.

        void _Copy(size_type _N)

// ?????????????????????
// Modified the following to correctly use the minimum size

//              {size_type _Ns = _N | _MIN_SIZE;

                {size_type _Ns = _N;
                if ( _Ns < _MIN_SIZE ) _Ns = _MIN_SIZE;

// End of modification

                if (max_size() < _Ns)
                        _Ns = _N;
                _E *_S;
                _TRY_BEGIN
                        _S = allocator.allocate(_Ns + 2, (void *)0);
                _CATCH_ALL
                        _Ns = _N;
                        _S = allocator.allocate(_Ns + 2, (void *)0);
                _CATCH_END

// ?????????????????????
// Modified the following to copy the correct amount.
// This problem was identified in the Microsoft STL FAQ. Search for:
// "When I assign a shorter string to an existing string ..." in the FAQ
// for more information.

//              if (0 < _Len)
//                      _Tr::copy(_S + 1, _Ptr, _Len);

                if (0 < _Len) {
                        size_type number_to_copy = _N;
                        if ( number_to_copy > _Len ) {
                                number_to_copy = _Len; }

                        _Tr::copy(_S + 1, _Ptr, number_to_copy); }

// End of modification

//              size_type _Olen = _Len; // This line removed... variable _Olen not
used... see below
                _Tidy(true);
                _Ptr = _S + 1;
                _Refcnt(_Ptr) = 0;
                _Res = _Ns;

// ?????????????????????
// Modified the following to set the end of string correctly.

//              _Eos(_Olen); }

                _Eos(_N); }

// End of modification

The problem is that this method is not completely responsible for making a
copy (as its name implies). The caller of this method is doing part of the
work by calculating the new desired size which is passed in as argument _N.
The allocation of _S is a function of this new size... however, the copying
into _S was a function of the original size. If the new size was smaller
than the original size, memory beyond the end of _S was corrupted. With the
changes shown above, the end of _S is respected.



Fri, 06 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak


Fri, 19 Jun 1992 00:00:00 GMT  
 FAQ "fix" seems very weak

HELP!

I am trying to use STL templates in a MFC 5.0 application under Win95. I
keep getting "Undeclared identifier" even if I #include the STL header
files.

I have previously used STL happily under MFC 5.0 (NT) without the need to
explicitly include STL header files but I am now on a different PC.

RS



Sun, 08 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

perhaps you need to use one of the following?

std::whatever

or

using namespace std;

--
---
Andrew Osman
ViewSoft, Inc.
http://www.viewsoft.com
---



Sun, 08 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

HELP!

I am trying to use STL templates in a MFC 5 application under Win95. The
compiler does not recognise any of the STL template classes even I #include
the STL header files.

I have previously used STL happily under MFC 5 (but under NT) even without
the need to explicitly include STL header files but I am now on a different
PC.

Any ideas?

Ravi Sikka



Tue, 10 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

perhaps you need to use one of the following?

std::whatever

or

using namespace std;

--
---
Andrew Osman
ViewSoft, Inc.
http://www.viewsoft.com
---



Tue, 10 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak


Fri, 19 Jun 1992 00:00:00 GMT  
 FAQ "fix" seems very weak

STLPort (a port of SGI STL) has a rope class, which does what a string
class does and then some.

Check out the documentation found on SGI's STL home page.

Also, AFAIK, string is part of the new "draft" standard of C++, not
necessarily part of the STL (which is part of the "draft" standard).

tom



Quote:

>The reason why this distinction is important to the thread is that looking
>for another STL implementation (such as SGI's) will not help solve the
>problem John Keenan is talking about.  std::string is not supplied in SGI's
>STL implementation.

tom

--
SPAM FILTER ON
remove no and spam from address to email me.



Wed, 11 Oct 2000 03:00:00 GMT  
 FAQ "fix" seems very weak

I don't believe the proposed fix will work.
I think both the allocate and _Tr::copy need an _Ns' which is big enough to accomodate
_Len.

Quote:



> <snip>
> > John, could you elaborate on the bug fix that you made in xstring.h, I'm
> > considering implementing it here.

> The modification was to DevStudio\VC\include\xstring (not xstring.h). I use
> // ??????? as my unique marker to help locate modified code.

>         void _Copy(size_type _N)

> // ?????????????????????
> // Modified the following to correctly use the minimum size

> //              {size_type _Ns = _N | _MIN_SIZE;

>                 {size_type _Ns = _N;
>                 if ( _Ns < _MIN_SIZE ) _Ns = _MIN_SIZE;

> // End of modification

>                 if (max_size() < _Ns)
>                         _Ns = _N;
>                 _E *_S;
>                 _TRY_BEGIN
>                         _S = allocator.allocate(_Ns + 2, (void *)0);
>                 _CATCH_ALL
>                         _Ns = _N;
>                         _S = allocator.allocate(_Ns + 2, (void *)0);
>                 _CATCH_END

> // ?????????????????????
> // Modified the following to copy the correct amount.
> // This problem was identified in the Microsoft STL FAQ. Search for:
> // "When I assign a shorter string to an existing string ..." in the FAQ
> // for more information.

> //              if (0 < _Len)
> //                      _Tr::copy(_S + 1, _Ptr, _Len);

>                 if (0 < _Len) {
>                         size_type number_to_copy = _N;
>                         if ( number_to_copy > _Len ) {
>                                 number_to_copy = _Len; }

>                         _Tr::copy(_S + 1, _Ptr, number_to_copy); }

> // End of modification

> //              size_type _Olen = _Len; // This line removed... variable _Olen not
> used... see below
>                 _Tidy(true);
>                 _Ptr = _S + 1;
>                 _Refcnt(_Ptr) = 0;
>                 _Res = _Ns;

> // ?????????????????????
> // Modified the following to set the end of string correctly.

> //              _Eos(_Olen); }

>                 _Eos(_N); }

> // End of modification

> The problem is that this method is not completely responsible for making a
> copy (as its name implies). The caller of this method is doing part of the
> work by calculating the new desired size which is passed in as argument _N.
> The allocation of _S is a function of this new size... however, the copying
> into _S was a function of the original size. If the new size was smaller
> than the original size, memory beyond the end of _S was corrupted. With the
> changes shown above, the end of _S is respected.



Fri, 13 Oct 2000 03:00:00 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. "Weak injection" bug in VC++ (dbtype)

2. Weak "Read Array" Function

3. CRecordset's Update seems "delayed"

4. "Fixed-precision" math

5. "Fixed-precision" math

6. What is "duff device" FAQ 20.35

7. the "struct hack" - FAQ 2.6

8. "constant expression required" and FAQ 6.11

9. "Take Me to Your FAQ, Sir..."

10. C FAQ For "Queen"

11. Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak

12. remove() vrs fopen("""w")

 

 
Powered by phpBB® Forum Software