<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Capi's Corner &#187; c#</title>
	<atom:link href="http://www.dont-panic.cc/capi/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dont-panic.cc/capi</link>
	<description>Development, Network, Security, Ideas &#038; Opinions</description>
	<lastBuildDate>Sat, 10 Dec 2011 19:31:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.NET strings are not always immutable!</title>
		<link>http://www.dont-panic.cc/capi/2007/10/03/net-strings-are-not-always-immutable/</link>
		<comments>http://www.dont-panic.cc/capi/2007/10/03/net-strings-are-not-always-immutable/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 14:17:45 +0000</pubDate>
		<dc:creator>Martin Carpella</dc:creator>
				<category><![CDATA[computer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.dont-panic.cc/capi/2007/10/03/net-strings-are-not-always-immutable/</guid>
		<description><![CDATA[Strings are immutable. If you want to modify a sequence of characters, use StringBuilder. At least, that&#8217;s whats officially said. But in the framework there is at least one method that does modify a string: TextRenderer.MeasureText() with ModifyString and EndEllipses will modify your string to match the ellipsed text if ellipsing happens. You can look [...]]]></description>
			<content:encoded><![CDATA[<p><code>Strings</code> are immutable. If you want to modify a sequence of characters, use <code>StringBuilder</code>. At least, that&#8217;s whats officially said. But in the framework there is at least one method that does modify a string:</p>
<p><code>TextRenderer.MeasureText()</code> with <code>ModifyString</code> and <code>EndEllipses</code>  will modify your string  to match the ellipsed text if ellipsing happens. You can look at this <a href="http://www.codeproject.com/useritems/NewPathCompactPath.asp" aiotitle="VB# example on codeproject using TextRenderer.MeasureText() for trimming text">VB# example on codeproject using <code>TextRenderer.MeasureText()</code> for trimming text</a> on how it is used.</p>
<p>The string seems to be modified directly in native code by <code>DrawTextEx</code> from <code>user32.dll</code>. Additionally to the scary fact that strings are not immutable, the length of the string is not updated, regardless if the resulting string is shorter!</p>
<p>For instance if you have a string &#8220;<code>aaaaaaa</code>&#8221; which will be truncated to &#8220;<code>aa...</code>&#8220;, the <code>Length</code> property will still return 7 for the shortened string.  The debugger shows that the string will in fact be &#8220;aa&#8230;\0a&#8221; after the operation. So maybe it might be right that the string is still 7 characters long but most outputting functionality like <code>Console.Out.WriteLine()</code> gets confused sometimes and stops any further output to the debugger or console under certain conditions.</p>
<p>A very quick investigation of the System.Drawing assembly using Lutz Roeder&#8217;s fabulous <a href="http://www.aisto.com/roeder/dotnet">.NET Reflector</a> showed that at least there should be no memory corruption in case &#8220;<code>WW</code>&#8221; would get ellipsed to &#8220;<code>W...</code>&#8220;, as <code>DrawTextEx</code> takes the length of the buffer and should result only in &#8220;<code>W.</code>&#8220;.</p>
<p>Summing up, I find the <em>corruption</em> of an immutable string <em>by an official Microsoft API</em> very troubling.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dont-panic.cc/capi/2007/10/03/net-strings-are-not-always-immutable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

