<?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’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx</link><description>I was working on a new feature earlier today and I discovered that while the code worked just fine when run as a 32bit app, it failed miserably when run as a 64bit app. If I was writing code that used polymorphic types (like DWORD_PTR) or something that</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10382275</link><pubDate>Fri, 04 Jan 2013 04:58:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10382275</guid><dc:creator>alegr1</dc:creator><description>&lt;p&gt;And, by the way, using reinterpret_cast is a bad habit. It&amp;#39;s even worse than using a C-style cast indiscriminately.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10382275" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10382274</link><pubDate>Fri, 04 Jan 2013 04:55:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10382274</guid><dc:creator>alegr1</dc:creator><description>&lt;p&gt;Prefast would find that a declaration is hidden by a nested declaration of the same name.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10382274" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10376888</link><pubDate>Wed, 12 Dec 2012 18:06:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10376888</guid><dc:creator>Larry Osterman [MSFT]</dc:creator><description>&lt;p&gt;@Jon: Yes it does make debugging crashes harder. But it helps make the produce more reliable.&lt;/p&gt;
&lt;p&gt;@Richard: We have a different mechanism other than /analyze - it runs the same tool chain but slightly differently&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10376888" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10376056</link><pubDate>Mon, 10 Dec 2012 14:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10376056</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;Do you not compile with /analyze? (implied by Ramesh&amp;#39;s post)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10376056" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10375407</link><pubDate>Thu, 06 Dec 2012 23:23:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10375407</guid><dc:creator>Jon</dc:creator><description>&lt;p&gt;&amp;quot;our debug builds are built with full optimization enabled&amp;quot;&lt;/p&gt;
&lt;p&gt;@Larry,&lt;/p&gt;
&lt;p&gt;Doesn&amp;#39;t this make it harder to debug crashes? &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10375407" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10373635</link><pubDate>Fri, 30 Nov 2012 20:51:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10373635</guid><dc:creator>Ramesh Dharan</dc:creator><description>&lt;p&gt;The GCC compiler has -Wshadow for catching this kind of thing. This StackOverflow article has a couple of interesting tidbits on the topic: &lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://stackoverflow.com/questions/6225070/is-there-an-equivalent-of-gccs-wshadow-in-visual-c"&gt;stackoverflow.com/.../is-there-an-equivalent-of-gccs-wshadow-in-visual-c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;Check out warnings C6244 and C6246&lt;/p&gt;
&lt;p&gt;But you will need to enable automatic code analysis to get them.&amp;quot;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10373635" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10373477</link><pubDate>Fri, 30 Nov 2012 13:09:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10373477</guid><dc:creator>Karellen</dc:creator><description>&lt;p&gt;This is also an argument for turning on warnings for &amp;quot;shadow&amp;quot;d variables/functions. That&amp;#39;s caught a number of bugs in some of my code before.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10373477" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10373248</link><pubDate>Thu, 29 Nov 2012 21:01:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10373248</guid><dc:creator>Larry Osterman [MSFT]</dc:creator><description>&lt;p&gt;@JM: Yes, but this was built from the NT build system - our debug builds are built with full optimization enabled - this ensures that our debug and release builds are as close to identical as possible - the only difference between the two is the asserts in the debug build.&lt;/p&gt;
&lt;p&gt;And yes, I&amp;#39;m working with the compiler folks to figure out why a warning wasn&amp;#39;t generated - there are other uninitialized local variable errors that are generated.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10373248" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10373010</link><pubDate>Thu, 29 Nov 2012 09:53:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10373010</guid><dc:creator>JM</dc:creator><description>&lt;p&gt;Aside from bitness, another way that would hopefully expose this is building and testing the code as a debug build first, with optimizations turned off. That should encourage the compiler to not do storage folding. The 32-bit vs. 64-bit is really just you getting lucky (or unlucky, depending on your perspective) -- any change in compiler setting or minor tweak to the code could have exposed the bug.&lt;/p&gt;
&lt;p&gt;And I agree with Jon, and I would put effort into tracking down why you didn&amp;#39;t get a warning. &amp;quot;Uninitialized local variable&amp;quot; is *not* a warning you want suppressed for any reason other than locally, as a workaround for a compiler limitation where it can&amp;#39;t see the variable really is initialized (and you can&amp;#39;t add explicit initialization yourself, for some reason). If people protest a change in build settings, tell them ignoring these warnings is a potential security risk. Because it is.&lt;/p&gt;
&lt;p&gt;If it&amp;#39;s a compiler bug, the static_cast&amp;lt;&amp;gt; is not the reason, if my little test program is any indication (apologies if the blog formatting turns this into mush):&lt;/p&gt;
&lt;p&gt; &amp;nbsp;unsigned int foo(unsigned int x) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return x;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int main() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;unsigned int x = 3.0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;unsigned int x = foo(static_cast&amp;lt;int&amp;gt;(x));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;This produces a warning when compiled with CL 16.0.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10373010" width="1" height="1"&gt;</description></item><item><title>re: What’s wrong with this code–a real world example</title><link>http://blogs.msdn.com/b/larryosterman/archive/2012/11/28/what-s-wrong-with-this-code-a-real-world-example.aspx#10372986</link><pubDate>Thu, 29 Nov 2012 08:33:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10372986</guid><dc:creator>Jonathan</dc:creator><description>&lt;p&gt;In C#, you&amp;#39;d also get:&lt;/p&gt;
&lt;p&gt;error CS0136: A local variable named &amp;#39;cchString&amp;#39; cannot be declared in this scope because it would give a different meaning to &amp;#39;cchString&amp;#39;, which is already used in a &amp;#39;parent or current&amp;#39; scope to denote something else&lt;/p&gt;
&lt;p&gt;Of course, in C# you&amp;#39;d seldom need to manually calculate storage...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10372986" width="1" height="1"&gt;</description></item></channel></rss>