473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing Contents of a String with PtrToStringChar s

What should happen if String contents are changed like below?
The contents do change and nothing crashed or anything.

String* str = S"hello";
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(str));
while(ps && *ps != 0)
(*ps++)='0';
Console::WriteL ine(str);
Nov 16 '05 #1
9 3639
What should happen if String contents are changed like below?
The contents do change and nothing crashed or anything.


You could end up chaniging strings you don't expect. Just try

String* str = S"hello";
String* str2 = str;
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(str));
while(ps && *ps != 0)
(*ps++)='0';
Console::WriteL ine(str);
Console::WriteL ine(str2);

The CLR's string interning feature makes this even worse - you could
change strings in parts of the program you don't control.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 16 '05 #2
Thanx Mattias!

I didn't think of interning :)

But let's take this to another plane:

String* password = Console::ReadLi ne();
if(String::IsIn terned(password ) == NULL)
{
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(password)) ;
while(ps && *ps != 0)
(*ps++)='0';
}
Console::WriteL ine(password);

Basically i want to clear the password.

To anyone, So if String was not Interned it seems to be OK
to change it's contents although it's "immutable" ?
Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message news:<#D******* *******@TK2MSFT NGP12.phx.gbl>. ..
What should happen if String contents are changed like below?
The contents do change and nothing crashed or anything.


You could end up chaniging strings you don't expect. Just try

String* str = S"hello";
String* str2 = str;
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(str));
while(ps && *ps != 0)
(*ps++)='0';
Console::WriteL ine(str);
Console::WriteL ine(str2);

The CLR's string interning feature makes this even worse - you could
change strings in parts of the program you don't control.

Mattias

Nov 16 '05 #3
But let's take this to another plane:

String* password = Console::ReadLi ne();
if(String::IsI nterned(passwor d) == NULL)
{
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(password)) ;
while(ps && *ps != 0)
(*ps++)='0';
}
Console::Write Line(password);

Basically i want to clear the password.
Can't you retrieve the password into a Byte[] or Char[] instead? That
would let you clear the content easily.

To anyone, So if String was not Interned it seems to be OK
to change it's contents although it's "immutable" ?


I would never say it's OK to do so.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 16 '05 #4
Yes I can. But TextControl can't. :)
Even if i use GetWindowText myself, i derive the key
using PasswordDeriveB ytes which takes only string!
QAnd there are other cases also.

Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message news:<#Z******* *******@TK2MSFT NGP09.phx.gbl>. ..
But let's take this to another plane:

String* password = Console::ReadLi ne();
if(String::IsI nterned(passwor d) == NULL)
{
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(password)) ;
while(ps && *ps != 0)
(*ps++)='0';
}
Console::Write Line(password);

Basically i want to clear the password.


Can't you retrieve the password into a Byte[] or Char[] instead? That
would let you clear the content easily.

To anyone, So if String was not Interned it seems to be OK
to change it's contents although it's "immutable" ?


I would never say it's OK to do so.

Mattias

Nov 16 '05 #5
A (more?) serious problem is that there's probably no guarantee that there
aren't other, unreachable copies of the password text in the GC heap.
Unless you can clear all of them, you're probably only gaining a false sense
of security.

-cd

cppdev wrote:
Yes I can. But TextControl can't. :)
Even if i use GetWindowText myself, i derive the key
using PasswordDeriveB ytes which takes only string!
QAnd there are other cases also.

Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message
news:<#Z******* *******@TK2MSFT NGP09.phx.gbl>. ..
But let's take this to another plane:

String* password = Console::ReadLi ne();
if(String::IsIn terned(password ) == NULL)
{
Char __pin* ps = const_cast<Char *>(PtrToStringC hars(password)) ;
while(ps && *ps != 0)
(*ps++)='0';
}
Console::WriteL ine(password);

Basically i want to clear the password.


Can't you retrieve the password into a Byte[] or Char[] instead? That
would let you clear the content easily.

To anyone, So if String was not Interned it seems to be OK
to change it's contents although it's "immutable" ?


I would never say it's OK to do so.

Mattias

Nov 16 '05 #6
Hi,

I am reviewing this post. Please feel free to let me know if you have any
problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
Nov 16 '05 #7
Hi!

I think it would be nice if String class provided a Clear method.
My concern is with the .net strings that they can remain
indefinitely in memory.

timhuang (Tian Min Huang) wrote in message news:<ks******* *******@cpmsftn gxa06.phx.gbl>. ..
Hi,

I am reviewing this post. Please feel free to let me know if you have any
problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Nov 16 '05 #8
Hi,

Thanks for your response. As you know, .NET Framework introduces Garbage
Collection to manage the memory, that is, the String memory is also
controled by GC. Although we are able to force GC with explicit System.gc
calls, overuse can severely affect performance. I strongly recommend you
the following articles on GC:

Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/is...I/default.aspx

Garbage Collection¡ªPar t 2: Automatic Memory Management in the Microsoft
.NET Framework
http://msdn.microsoft.com/msdnmag/is...2/default.aspx

I look forward to your feedback.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
Nov 16 '05 #9
Hi,

Thanks a lot for your feedback. Now that I understand your concerns, I will
report it to our Development Team and I believe they will take it into
consideration for the future version of .NET Framewok.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Nov 16 '05 #10

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

Similar topics

1
2542
by: Kieran Benton | last post by:
Hi, Sorry to post this, I feel like a right fool but Im under serious time pressure! Afraid I'm a newbie to managed C++ (Ive had to resort to it as Im wrapping some COM objects for C# use). Any ideas how to get this working? void Configure(int port,String* filename,int maxclients) { //const WCHAR* fn = const_cast<__wchar_t*>(PtrToStringChars(filename));
15
10821
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
8
12148
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to 'LPCWSTR' I really get mad with that !!!
20
3201
by: Peteroid | last post by:
How the heck does one convert a String* to char? Specifically: String* my_string = "HELLO" ; char m_char_array ; m_char_array = my_string ; // error strcpy( m_char_array, my_string ) ; // error strcpy( m_char_array, my_string.ToCharArray( ) ) ; // error strcpy( m_char_array, my_string.ToCharArray(0,32 ) ) ; // error strcpy( m_char_array, my_string.ToCharArray(0,0) ) ; // error
9
3574
by: blair | last post by:
Hi I am using VS 2005 to upgrade a demo project and I came across this error in a bunch of code which compiles fine on VC++ 2003. <code> wchar_t __pin* pVal = PtrToStringChars( val ); </code> <error> Error 1 error C2440: 'initializing' : cannot convert from '__const_Char_ptr' to 'wchar_t __pin *'
3
3331
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
0
3695
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged code). I get the following compilation error: C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\ cstringt.h(875): error C2664: 'PtrToStringChars' : cannot convert parameter 1 from 'unsigned char *' to 'const System::String __gc *' The code segemnt is:
30
3967
by: nano2k | last post by:
Hi I need an efficient method to convert a string object to it's byte equivalent. I know there are LOTS of methods, but they lack in efficiency. All methods allocate new memory to create the byte array. Of course, when memory allocation occurs, then naturally extra processing power is needed. To more explicit, MFC introduced a super-efficient method of dealing with this situation. As far as I remember (I switched from MFC to .NET few...
14
10620
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right? But I only get the first character. And when I try to do std::wstring l_s(convert(somestring)) I get really strange characters into l_s string representation, but when I check l_s individual characters they look ok. const wchar_t* convert(...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.