473,467 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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::ChTra itsCRT<char> > > &

str2 {0x0127a4a8 "chris logged off."}const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<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::ChTra itsCRT<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::ChTra itsCRT<char> > > &

str2 {0x0127a610 "</"} const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<char> > > &

strResult {0x012525b8 "<Message>chris logged off."}
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<char> > >
_____________________________

Tony_Morris at Teledyne dot com
Nov 17 '05 #1
2 2158
The bug is probably in the construction of str2. Can you show us that code?

TonyM wrote:
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::ChTra itsCRT<char> > > &

str2 {0x0127a4a8 "chris logged off."}const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<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::ChTra itsCRT<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::ChTra itsCRT<char> > > &

str2 {0x0127a610 "</"} const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<char> > > &

strResult {0x012525b8 "<Message>chris logged off."}
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<char> > >
_____________________________

Tony_Morris at Teledyne dot com

Nov 17 '05 #2
Thanks Doug!

I am not sure why, but my single post to the "VC.ATL" group has been posted
here too...???

Anyhow, your question is a copy of the answer from that group:
>>>>>>>>>>>>>>>>>>>>>>>>>>>
"doug mansell" wrote:
The bug is probably in the construction of str2. Can you show us that code?>>>>>>>>>>>>>>>>>>>>>>>>>>>

Right on the mark! I had a transfer function that was doing a += into a
CString that ended up placing one extra NULL in the string. I would have
never guessed that CString would support additional NULLs in the data but
sure enough, the following has the output of incrementing the length even
though the string passed to lstrlen() returns 0 every time!
_____________________________________

CString str("");
TCHAR ch = '\0';

MessageBox(::GetActiveWindow(), "Length: " + CTekString(str.GetLength()) ,
"str.GetLength()", MB_OK);
MessageBox(::GetActiveWindow(), "Length: " + CTekString(lstrlen(str)) ,
"str.GetLength()", MB_OK);

str += ch;

MessageBox(::GetActiveWindow(), "Length: " + CTekString(str.GetLength()) ,
"str.GetLength()", MB_OK);
MessageBox(::GetActiveWindow(), "Length: " + CTekString(lstrlen(str)) ,
"str.GetLength()", MB_OK);

str += ch;

MessageBox(::GetActiveWindow(), "Length: " + CTekString(str.GetLength()) ,
"str.GetLength()", MB_OK);
MessageBox(::GetActiveWindow(), "Length: " + CTekString(lstrlen(str)) ,
"str.GetLength()", MB_OK);
________________________________________
Output:

0
0
1
0
2
0
__________________________________________________ _

Thank you profusely for the insight!!!

TonyM
"Igor Tandetnik" wrote:
"TonyM" <Tony_Morris @at Teledyne .dot com> wrote in message
news:E0**********************************@microsof t.com
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::ChTra itsCRT<char> > > &

str2 {0x0127a4a8 "chris logged off."}const
ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTra itsCRT<char> > > &

str1_GetLength 9 int

str1_lstrlen 9 int

str2_GetLength 18 int

str2_lstrlen 17 int


CString supports embedded NULs in strings. In the presence of embedded
NULs, GetLength differs from lstrlen. So the question is, how did you
set up str2 so that it ended up with an extra NUL at the end? Show a
complete program, where you set up the arguments and then invoke the
operator.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance...
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
1
by: maadhuu | last post by:
what is the use of returning "const & "from a function in C++ ?? Is it used somewhere at all ?? thank you, Ranjan.
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
2
by: ek | last post by:
This first example does not work (cannot be overloaded): int& operator()(int a) { // (1) return a; } int const& operator()(int a) { // (2) return a; }
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.