<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx</link><description>It depends on the function.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5143194</link><pubDate>Wed, 26 Sep 2007 15:17:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5143194</guid><dc:creator>AndyB</dc:creator><description>&lt;p&gt;Dagwood, C was foisted upon us by the folks at MS, not Scott McNealy. Scott foisted Java upon us, though I concur that Java and C are practically the same thing.&lt;/p&gt;
&lt;p&gt;I think I may have misplaced a # character in there somewhere though :-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5143194" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5084386</link><pubDate>Mon, 24 Sep 2007 03:39:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5084386</guid><dc:creator>John Hensley</dc:creator><description>&lt;p&gt;C wasn't &amp;quot;foisted&amp;quot; on anyone, and certainly not by Sun. Programmers took it up willingly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5084386" width="1" height="1"&gt;</description></item><item><title>If it isn't Unicode, it isn't ANY code!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5070938</link><pubDate>Sun, 23 Sep 2007 12:51:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5070938</guid><dc:creator>Sorting It All Out</dc:creator><description>&lt;p&gt;Raymond makes a good point in What happens if you pass a source length greater than the actual string&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5070938" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5060156</link><pubDate>Sun, 23 Sep 2007 00:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5060156</guid><dc:creator>Dagwood</dc:creator><description>&lt;p&gt;This entire scenario and discusion supports my contention that when C was foisted on us by Scott McNeely, et al in the 80's it was a very tragic day for IT.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5060156" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5041070</link><pubDate>Sat, 22 Sep 2007 01:49:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5041070</guid><dc:creator>IgorD</dc:creator><description>&lt;p&gt;This post brought tears to my eyes.&lt;/p&gt;
&lt;p&gt;Few weeks ago I found a bug that &amp;quot;sometimes&amp;quot; crashed my application.&lt;/p&gt;
&lt;p&gt;The code (simplified ) at first went something like this:&lt;/p&gt;
&lt;p&gt;void DrawingFunction (..., char *someText)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; int len = strlen (someText);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; DrawText (hdc, someText, len, txtRect, DT_LEFT | ...);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and everything was fine, but at some point I changed it to this:&lt;/p&gt;
&lt;p&gt;void DrawingFunction (..., char *someText)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; int &amp;nbsp;len = strlen (someText); &amp;nbsp;// HERE'S THE DEVIL!&lt;/p&gt;
&lt;p&gt; &amp;nbsp;char tmpBuff[256];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if (somethin) &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;len = GetAltText (tmpBuff, someText);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;sprintf (tmpBuff, &amp;quot;%.*s&amp;quot;, 255, someText); &amp;nbsp;// NORMAL CASE&lt;/p&gt;
&lt;p&gt; &amp;nbsp; DrawText (hdc, tmpBuff, len, txtRect, DT_LEFT | ...);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and it would crash every time 'someText' was longer than 255+ characters (and 'somethin' was false).&lt;/p&gt;
&lt;p&gt;I was chasing that bug for two years (because 'someText' was usualy very short). I don't know, I must have done it in a hurry or asleap or ...&lt;/p&gt;
&lt;p&gt; Horror! Horror!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5041070" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5038944</link><pubDate>Fri, 21 Sep 2007 22:53:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5038944</guid><dc:creator>Gazpacho</dc:creator><description>&lt;p&gt;&amp;quot;You're kidding. No one who writes C code would ever write that.&amp;quot;&lt;/p&gt;
&lt;p&gt;I think it is common knowledge among Windows programmers that the use of MAX_PATH anywhere in string-related code makes it crash-proof.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5038944" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5034241</link><pubDate>Fri, 21 Sep 2007 17:43:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5034241</guid><dc:creator>Frederik Slijkerman</dc:creator><description>&lt;p&gt;@Tim Smith: You're right, I forgot about that. :-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5034241" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5022208</link><pubDate>Fri, 21 Sep 2007 03:26:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5022208</guid><dc:creator>Tim Smith</dc:creator><description>&lt;p&gt;Frederik: I think for a function called invariant_strnicmp with char* arguments, you can assume that these are valid null-terminated strings...&lt;/p&gt;
&lt;p&gt;Absolutely not. &amp;nbsp;The history of the strnicmp routines date back to the old unix file systems where you would pack those file named in a structure that has enough space to hold a string up to the max length NOT including the NUL. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;For many, strncmp has become a method of doing substring matching, but it was originally more about space savings and buffer padding (see strncpy.)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5022208" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5011922</link><pubDate>Thu, 20 Sep 2007 16:13:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5011922</guid><dc:creator>mccoyn</dc:creator><description>&lt;p&gt;No one who writes C code would ever write that on purpose.&lt;/p&gt;
&lt;p&gt;The problem is that if your team cranks out 100,000 lines of code for a product can you be sure that none of your developers made that mistake just once, even though there are 1000 places where it could have been made? &amp;nbsp;Are your developers mistake rates less than 0.1% ?&lt;/p&gt;
&lt;p&gt;You might be able to double check everything, but it will take a lot of work.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5011922" width="1" height="1"&gt;</description></item><item><title>re: What happens if you pass a source length greater than the actual string length?</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2007/09/19/4988589.aspx#5009529</link><pubDate>Thu, 20 Sep 2007 12:26:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5009529</guid><dc:creator>Adam</dc:creator><description>&lt;p&gt;If you want it to work with strings that may or may not be null terminated, I think you need something like this:&lt;/p&gt;
&lt;p&gt;int invariant_strnicmp(char *s1, char *s2, size_t n)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; size_t n1 = strnlen(s1, n);&lt;/p&gt;
&lt;p&gt; size_t n2 = strnlen(s2, n);&lt;/p&gt;
&lt;p&gt; return CompareStringA(LOCALE_INVARIANT, NORM_IGNORECASE,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; s1, n1, s2, n2) - CSTR_EQUAL;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5009529" width="1" height="1"&gt;</description></item></channel></rss>