<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Peter Ritchie's MVP Blog : C++</title><link>http://msmvps.com/blogs/peterritchie/archive/tags/C_2B002B00_/default.aspx</link><description>Tags: C++</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Writing Solid Code</title><link>http://msmvps.com/blogs/peterritchie/archive/2006/10/16/Writing-Solid-Code.aspx</link><pubDate>Mon, 16 Oct 2006 20:14:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:145154</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=145154</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2006/10/16/Writing-Solid-Code.aspx#comments</comments><description>&lt;p&gt;My apologies to Steve Maguire for &amp;quot;borrowing&amp;quot; a title.&lt;/p&gt;&lt;p&gt;I constantly see code, examples, and advice that perpetuate unsafe coding practices.&amp;nbsp; As programmers we have the habit of getting something to a &amp;quot;working&amp;quot; state and call it &amp;quot;done&amp;quot;.&amp;nbsp; This is especially true in processes that have no real architecture or design phases.&amp;nbsp; Over the years, as a society, programmers have begun to realize some of the obvious flaws and have been perpetuating practices and code checkers to avoid such flaws.&amp;nbsp; But, there&amp;#39;s still the mentality of &amp;quot;but it works [in my limited tests], how could it be wrong&amp;quot;.&amp;nbsp; &lt;/p&gt;&lt;p&gt;For example, I don&amp;#39;t know of any programmers that would sanction the following C++ code:&lt;/p&gt;&lt;div style="font-size:10pt;background:cornsilk;margin:2px;color:black;font-family:Courier New;border:silver 1px solid;padding:2px;"&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; * &lt;span style="color:blue;"&gt;const&lt;/span&gt; p = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt;[10];&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p[1] = 10;&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;delete&lt;/span&gt;[] p;&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt;(p[1] == 10)&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; puts(&lt;span style="color:maroon;"&gt;&amp;quot;ten&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; puts(&lt;span style="color:maroon;"&gt;&amp;quot;not ten&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;/div&gt;&lt;p&gt;But, &amp;quot;it works&amp;quot; in a release build.&lt;/p&gt;&lt;p&gt;There are many, many similar examples of code that &amp;quot;works&amp;quot; in limited circumstances and this is deemed acceptable by, what seems to be, a majority of programmers.&amp;nbsp; I&amp;#39;ve seen many discussions of programming constructs that can&amp;#39;t work 100% of the time; with impassioned participants that will always argue that either they can prove it works with an example and simply ignore proofs where it fails as &amp;quot;contrived&amp;quot; or statistically insignificant.&amp;nbsp; Although, I don&amp;#39;t know of a single programmer that can claim they&amp;#39;ve never been guilty of this.&lt;/p&gt;&lt;p&gt;From a bricks-and-mortar building standpoint; we, as a society, realized the errors of assuming just 99% is good enough.&amp;nbsp; From this there were the instigation of Engineering certifications/licensing, building standards, etc.&amp;nbsp; All to ensure that 1% was equally as important as the other 99%; to ensure&amp;nbsp;engineers don&amp;#39;t unintentionally kill someone.&amp;nbsp; Even with this we&amp;#39;re still reminded how important it is to abide by these standards and what happens when we don&amp;#39;t (like the Hyatt Regency Walkway Collapse, or the Sampoong Department Store Collapse), despite the likelihood.&lt;/p&gt;&lt;p&gt;To a certain extent, our tools, processes, training, all seem to perpetuate the &amp;quot;good enough&amp;quot; mentality.&amp;nbsp; The ANSI C library is a prime example.&amp;nbsp; Largely designed in the 70&amp;#39;s when security wasn&amp;#39;t an issue yet, it&amp;#39;s rife with functionality to let programmer write buffer overflow code to their heart&amp;#39;s content.&amp;nbsp; For example:&lt;/p&gt;&lt;div style="font-size:10pt;background:cornsilk;margin:2px;color:black;font-family:Courier New;border:silver 1px solid;padding:2px;"&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;pack&lt;/span&gt;(1)&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;struct&lt;/span&gt; MyStruct&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;char&lt;/span&gt; s[10];&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; i;&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } myStruct = {&lt;span style="color:maroon;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;, 1};&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&lt;span style="color:blue;"&gt;#pragma&lt;/span&gt; &lt;span style="color:blue;"&gt;pack&lt;/span&gt;(&lt;span style="color:blue;"&gt;pop&lt;/span&gt;)&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sprintf(myStruct.s, &lt;span style="color:maroon;"&gt;&amp;quot;1234567890&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin:0px;font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&lt;span style="color:maroon;"&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;, myStruct.i);&lt;/p&gt;&lt;/div&gt;&lt;p&gt;...where the output is &amp;quot;0&amp;quot;, not &amp;quot;1&amp;quot;; with nary a compiler warning or runtime error.&amp;nbsp; It&amp;#39;s APIs like these and the mentality of &amp;quot;when is that ever going to happen&amp;quot; that lead to software security flaws.&amp;nbsp; Even with continual bombardment of security patches, developers still can&amp;#39;t get past the &amp;quot;works 99% of the time&amp;quot; hurdle.&lt;/p&gt;&lt;p&gt;Here is&amp;nbsp;small list of some of the &amp;quot;hot spots&amp;quot; that will still cause heated discussions even amongst experienced developers:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;.NET: Avoid catch(Exception) in anything other than a last-chance handler.&lt;/li&gt;&lt;li&gt;C++: Avoid catch(...) in anything other than a last-chance handler.&lt;/li&gt;&lt;li&gt;Windows: Don&amp;#39;t access windows data from a thread that didn&amp;#39;t create the window.&lt;/li&gt;&lt;li&gt;.NET: Avoid Control.Invoke.&lt;/li&gt;&lt;li&gt;.NET/VB: Avoid DoEvents.&lt;/li&gt;&lt;li&gt;Performing potentially lengthy operations on the main/GUI thread.&lt;/li&gt;&lt;li&gt;Testing for valid pointers and IsBad*Ptr().&lt;/li&gt;&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=145154" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category></item><item><title>Has ANSI C/C++ Been Perpetuating Unsecure Computing?</title><link>http://msmvps.com/blogs/peterritchie/archive/2006/09/28/Has-ANSI-C_2F00_C_2B002B00_-Been-Perpetuating-Unsecure-Computing_3F00_.aspx</link><pubDate>Thu, 28 Sep 2006 18:19:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:145118</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=145118</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2006/09/28/Has-ANSI-C_2F00_C_2B002B00_-Been-Perpetuating-Unsecure-Computing_3F00_.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/michael_howard/archive/2006/09/28/775780.aspx"&gt;Whatever Happened to sprintf(..., &amp;quot;%n&amp;quot;, ...)?&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsecure/html/strsafe.asp"&gt;Strsafe.h: Safer String Handling in C&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/8ef0s5kh.aspx"&gt;Security Enhancements in the CRT&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Many&amp;nbsp;3rd party libraries model their API around the ANSI C/C++ libraries, introducing the capability of buffer overruns.&amp;nbsp; Is using a ANSI C++&amp;nbsp;library/compiler a good thing anymore?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=145118" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2B002B00_/default.aspx">C++</category></item><item><title>Save CBitmap to File</title><link>http://msmvps.com/blogs/peterritchie/archive/2006/09/25/Save-CBitmap-to-File.aspx</link><pubDate>Mon, 25 Sep 2006 14:48:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:139771</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=139771</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2006/09/25/Save-CBitmap-to-File.aspx#comments</comments><description>&lt;p&gt;It has always astounded me why the CBitmap class never implemented saving to a file.&amp;nbsp; Here&amp;#39;s a nice and tidy way:&lt;/p&gt;&lt;div style="font-size:10pt;background:cornsilk;color:black;font-family:Courier New;border:black 1px solid;padding:4px;"&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;atlimage.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt; &lt;span style="color:maroon;"&gt;&amp;lt;Gdiplusimaging.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;//...&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CBitmap bitmap;&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; bitmap.CreateBitmap(width, height, 1, 32, rgbData);&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CImage image;&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; image.Attach(bitmap);&lt;/p&gt;&lt;p style="margin:0px;font-family:courier new;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; image.Save(_T(&lt;span style="color:maroon;"&gt;&amp;quot;C:\\test.bmp&amp;quot;&lt;/span&gt;), Gdiplus::ImageFormatBMP);&lt;/p&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=139771" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/MFC/default.aspx">MFC</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2B002B00_/default.aspx">C++</category></item></channel></rss>