Re: friend CStringT operator+(const CStringT& str1, const CStringT& st

From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 11/02/04


Date: Tue, 2 Nov 2004 09:59:14 -0800

I don't see anything wrong with this code. Your asserts clearly
indicate that the second argument has been corrupted before
this code is reached.

-- 
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"TonyM" <Tony_Morris @at Teledyne .dot com> wrote in message 
news:E0C38338-0056-4D63-8181-8C46CCB6346B@microsoft.com...
> Q: Is there a good way to overcome this apparent bug without modifying the
> mfc code?
> ___________________________________
>
> Info:  Although it is NOT a good idea, I seemed to have perhaps located a
> bug in this function with the GetLength call whose results are sent to the
> sub-call to Concatenate.  So I added the following and it asserts on
> particular strings that are used.  (I am not sure if this makes any
> difference, but I am using class derived from CString that ends up calling
> this code...  However, that class never does anything remotely like 
> touching
> any private CString data without going through  the very normal exposed
> methods.)
>
> Code as modified in CStringT.h file (line# 2013):
> friend CStringT operator+( const CStringT& str1, const CStringT& str2 )
> {
>  CStringT strResult( str1.GetManager() );
>
>  int str1_GetLength = str1.GetLength();
>  int str1_lstrlen   = lstrlen(str1);
>  int str2_GetLength = str2.GetLength();
>  int str2_lstrlen   = lstrlen(str2);
>  ASSERT ( str1_GetLength == str1_lstrlen );
>  ASSERT ( str2_GetLength == str2_lstrlen );
>
>  Concatenate( strResult, str1, str1.GetLength(), str2, str2.GetLength() );
>
>  return( strResult );
> }
> ______________________________
> Values are as follows as the debugger is about to eexecute the first 
> ASSERT
> statement:
>
> str1 {0x0127a658 "<Message>"} const
> ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &
>
> str2 {0x0127a4a8 "chris logged off."}const
> ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &
>
> str1_GetLength  9  int
>
> str1_lstrlen  9  int
>
> str2_GetLength  18  int
>
> str2_lstrlen  17  int
>
> Ignore the assert and the result is:
> strResult  {0x0127a5a8 "<Message>chris logged off."}
> ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >
>
> ________________________________
>
> Notice that in this instance, the second assert will fail.  If I ignore 
> it,
> then then next time I enter this function on this string to concatenate 
> more,
> the str1 length is wrong and the concantenation writes the added string 
> just
> after the null so that the result is incorrect.
>
> ___________________________________
>
> str1_GetLength  27  int
>
> str1_lstrlen  26  int
>
> str2_GetLength  2  int
>
> str2_lstrlen  2  int
>
> str1  {0x0127a5a8 "<Message>chris logged off."}  const
> ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &
>
> str2  {0x0127a610 "</"}  const
> ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > &
>
> strResult {0x012525b8 "<Message>chris logged off."}
> ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >
> _____________________________
>
> Tony_Morris at Teledyne dot com 


Relevant Pages

  • Re: Segfault City
    ... Richard Heathfield wrote: ... assert(isdigit(ch)); ... int ctoi ... The C language has Macros, ...
    (comp.lang.c)
  • Re: memcmp() checker: memory access errors
    ... First, you malloc 'data' and never free it; ... files opened in binary mode, ... Never put anything inside 'assert' that you want to be evaluated. ... int probe_img ...
    (comp.lang.c)
  • Re: Converting Bitmap into 2D-Array
    ... // (Does bounds-checking in debug builds, using ASSERT macro.) ... explicit Matrix2D(int rows, int cols) ... ASSERT(cols> 0); ... int Columnsconst ...
    (microsoft.public.vc.language)
  • Re: any function to handle this kind of counting?
    ... > int letter; ... In debug builds "ASSERT" will bomb if the condition in parens is true. ...
    (comp.programming)
  • Re: WebRequest problems
    ... Michael Kremser wrote: ... > I use the following code to get something from a webserver: ... > int b; ... > return strResult; ...
    (microsoft.public.dotnet.framework.compactframework)