<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Effective C Tips #1 &#8211; Using vsprintf()</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Sun, 06 May 2012 10:34:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-4691</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Thu, 16 Jun 2011 10:56:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-4691</guid>
		<description>Apologies for the delay in moderating the comment - I have been away on holiday. Anyway, thanks for sharing this, as I wasn&#039;t aware of the sign extension issue. As a matter of interest, what happens if you use the C99 printf formatters, as described &lt;a href=&quot;http://embeddedgurus.com/stack-overflow/2011/02/formatted-output-when-using-c99-data-types/&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Apologies for the delay in moderating the comment &#8211; I have been away on holiday. Anyway, thanks for sharing this, as I wasn&#8217;t aware of the sign extension issue. As a matter of interest, what happens if you use the C99 printf formatters, as described <a href="http://embeddedgurus.com/stack-overflow/2011/02/formatted-output-when-using-c99-data-types/" rel="nofollow">here</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Atlant</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-4567</link>
		<dc:creator>Atlant</dc:creator>
		<pubDate>Thu, 09 Jun 2011 12:43:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-4567</guid>
		<description>I&#039;ll second, err, &quot;third&quot; the comment to always use (v)snprintf(). Without that, it&#039;s too easy for a buffer overflow to sneak up on you.

We had a case recently that Grammatech&#039;s CodeSonar caught for us. We were doing (simplified):

sprintf( five_byte_buffer, &quot;%04X&quot;, uint16_value );

and Code Sonar was telling us that the conversion could overflow the buffer. &quot;Nonsense!&quot; we said, &quot;knowing&quot; that 1) the four bytes output by the &quot;%04X&quot; conversion plus the trailing null character couldn&#039;t be more than five bytes and 2) we&#039;d actually tested all reasonable values of uint16_value from 0x0000 to 0xFFFF and our conversion routine had worked fine.

But it turns out that CodeSonar knew something we didn&#039;t know: the &quot;%x&quot; conversion in some libraries would sign-extend into 32 bits any value being &quot;%x&quot; formatted before doing the formatting and on systems that used *THAT* sort of conversion, &quot;negative&quot; values would then be formatted as eight characters (e.g. &quot;FFFFFFFF&quot;) and *THAT* would blow our buffer. We switched to using snprintf() and that eliminated the buffer overflow. (Code Sonar was still unhappy  that on some systems, the conversion would not fit in the buffer (and would be truncated) but since it did fit on our system (and wasn&#039;t truncated), we decided we could live with that.)

Always use the ...nprintf() version of the functions!</description>
		<content:encoded><![CDATA[<p>I&#8217;ll second, err, &#8220;third&#8221; the comment to always use (v)snprintf(). Without that, it&#8217;s too easy for a buffer overflow to sneak up on you.</p>
<p>We had a case recently that Grammatech&#8217;s CodeSonar caught for us. We were doing (simplified):</p>
<p>sprintf( five_byte_buffer, &#8220;%04X&#8221;, uint16_value );</p>
<p>and Code Sonar was telling us that the conversion could overflow the buffer. &#8220;Nonsense!&#8221; we said, &#8220;knowing&#8221; that 1) the four bytes output by the &#8220;%04X&#8221; conversion plus the trailing null character couldn&#8217;t be more than five bytes and 2) we&#8217;d actually tested all reasonable values of uint16_value from 0&#215;0000 to 0xFFFF and our conversion routine had worked fine.</p>
<p>But it turns out that CodeSonar knew something we didn&#8217;t know: the &#8220;%x&#8221; conversion in some libraries would sign-extend into 32 bits any value being &#8220;%x&#8221; formatted before doing the formatting and on systems that used *THAT* sort of conversion, &#8220;negative&#8221; values would then be formatted as eight characters (e.g. &#8220;FFFFFFFF&#8221;) and *THAT* would blow our buffer. We switched to using snprintf() and that eliminated the buffer overflow. (Code Sonar was still unhappy  that on some systems, the conversion would not fit in the buffer (and would be truncated) but since it did fit on our system (and wasn&#8217;t truncated), we decided we could live with that.)</p>
<p>Always use the &#8230;nprintf() version of the functions!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: e</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-123</link>
		<dc:creator>e</dc:creator>
		<pubDate>Tue, 02 Feb 2010 19:26:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-123</guid>
		<description>I agree wholeheartedly with Mans: use (v)snprintf(). A public domain version is part of &lt;a href=&quot;http://www.sqlite.org/src/artifact/2c2702dc49&quot; rel=&quot;nofollow&quot;&gt;sqlite&lt;/a&gt;. -- e</description>
		<content:encoded><![CDATA[<p>I agree wholeheartedly with Mans: use (v)snprintf(). A public domain version is part of <a href="http://www.sqlite.org/src/artifact/2c2702dc49" rel="nofollow">sqlite</a>. &#8212; e</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mans</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-122</link>
		<dc:creator>Mans</dc:creator>
		<pubDate>Mon, 01 Feb 2010 22:24:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-122</guid>
		<description>I would argue vsprintf() should never be used due to the risk of buffer overflow.  One should always use (v)snprintf().  If your system doesn&#039;t have it, steal a BSD licensed one somewhere.</description>
		<content:encoded><![CDATA[<p>I would argue vsprintf() should never be used due to the risk of buffer overflow.  One should always use (v)snprintf().  If your system doesn&#39;t have it, steal a BSD licensed one somewhere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-121</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Fri, 10 Apr 2009 01:15:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-121</guid>
		<description>oops, I forgot to add that I use stdin/stdout redirection to a custom putchar function that actually does the work of putting the code on to a serial port or LCD screen.</description>
		<content:encoded><![CDATA[<p>oops, I forgot to add that I use stdin/stdout redirection to a custom putchar function that actually does the work of putting the code on to a serial port or LCD screen.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-120</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Fri, 10 Apr 2009 01:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-120</guid>
		<description>What I&#039;ve done in the past is to use a macro to achieve the roughly the same result.I use this for my DEBUG statements while I&#039;m developing.#if defined(DEBUG_GLOBAL) &amp;&amp; defined(DEBUG_LOCAL) #define DEBUG(__format, __args...) do { printf_P( PSTR(__format), ## __args ); } while (0) #else #define DEBUG(__format, __args...)#endif</description>
		<content:encoded><![CDATA[<p>What I&#39;ve done in the past is to use a macro to achieve the roughly the same result.I use this for my DEBUG statements while I&#39;m developing.#if defined(DEBUG_GLOBAL) &amp;&amp; defined(DEBUG_LOCAL) #define DEBUG(__format, __args&#8230;) do { printf_P( PSTR(__format), ## __args ); } while (0) #else #define DEBUG(__format, __args&#8230;)#endif</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-119</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Tue, 31 Mar 2009 12:13:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-119</guid>
		<description>Mike is quite right. Of course I&#039;m not sure what MISRA expects one to do when one is writing code for a GUI. Although it&#039;s possible to write string formatters without using variable length argument lists, the results typically aren&#039;t pretty. I think this is one of those cases where the cure may be worse than the illness.</description>
		<content:encoded><![CDATA[<p>Mike is quite right. Of course I&#8217;m not sure what MISRA expects one to do when one is writing code for a GUI. Although it&#8217;s possible to write string formatters without using variable length argument lists, the results typically aren&#8217;t pretty. I think this is one of those cases where the cure may be worse than the illness.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Barr</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/02/effective-c-tips-1-using-vsprintf/comment-page-1/#comment-118</link>
		<dc:creator>Michael Barr</dc:creator>
		<pubDate>Thu, 12 Feb 2009 08:33:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/02/10/effective-c-tips-1-using-vsprintf/#comment-118</guid>
		<description>FYI to readers: MISRA-C Rule 16.1 prohibits the creation of functions with variable numbers of arguments.  There are risks involved in doing this in a safety-critical system.</description>
		<content:encoded><![CDATA[<p>FYI to readers: MISRA-C Rule 16.1 prohibits the creation of functions with variable numbers of arguments.  There are risks involved in doing this in a safety-critical system.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

