<?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: Safe replacement for gets</title>
	<atom:link href="http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/feed/" rel="self" type="application/rss+xml" />
	<link>http://wesley.vidiqatch.org</link>
	<description>This blog does not need a smart-ass tagline</description>
	<lastBuildDate>Tue, 09 Mar 2010 12:01:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: James</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22090</link>
		<dc:creator>James</dc:creator>
		<pubDate>Mon, 10 Aug 2009 21:12:30 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22090</guid>
		<description>I strongly recommend you #define LINEBUFFERLEN 100 
and then replace every instance of 100 with LINEBUFFERLEN so that you (or another programmer) don&#039;t change your buffer length, forget to change it in every single location and end up with another stack overflow just like the one you were trying to fix in the first place.</description>
		<content:encoded><![CDATA[<p>I strongly recommend you #define LINEBUFFERLEN 100<br />
and then replace every instance of 100 with LINEBUFFERLEN so that you (or another programmer) don&#8217;t change your buffer length, forget to change it in every single location and end up with another stack overflow just like the one you were trying to fix in the first place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mirabilos</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22089</link>
		<dc:creator>mirabilos</dc:creator>
		<pubDate>Mon, 10 Aug 2009 08:16:13 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22089</guid>
		<description>Uhm… fgetln(3) has been part of the BSDs for ages (there are GNU/Linux implementations too, e.g. in libbsd in Debian), and getline(3) from glibc has made it into SUSv3 (POSIX) 2008. So what’s the point?</description>
		<content:encoded><![CDATA[<p>Uhm… fgetln(3) has been part of the BSDs for ages (there are GNU/Linux implementations too, e.g. in libbsd in Debian), and getline(3) from glibc has made it into SUSv3 (POSIX) 2008. So what’s the point?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22088</link>
		<dc:creator>Jon</dc:creator>
		<pubDate>Mon, 10 Aug 2009 05:10:51 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22088</guid>
		<description>&lt;a href=&quot;#comment-22087&quot; rel=&quot;nofollow&quot;&gt;@Jon &lt;/a&gt; 
*smacks head against wall*
length = 0;
finished = 1;

I&#039;m going to sleep now!</description>
		<content:encoded><![CDATA[<p><a href="#comment-22087" rel="nofollow">@Jon </a><br />
*smacks head against wall*<br />
length = 0;<br />
finished = 1;</p>
<p>I&#8217;m going to sleep now!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22087</link>
		<dc:creator>Jon</dc:creator>
		<pubDate>Mon, 10 Aug 2009 05:10:17 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22087</guid>
		<description>&lt;a href=&quot;#comment-22086&quot; rel=&quot;nofollow&quot;&gt;@Jon &lt;/a&gt; 
err... the above should read
{
length = 1;
finished = 0;
}

Brain failure while typing.</description>
		<content:encoded><![CDATA[<p><a href="#comment-22086" rel="nofollow">@Jon </a><br />
err&#8230; the above should read<br />
{<br />
length = 1;<br />
finished = 0;<br />
}</p>
<p>Brain failure while typing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22086</link>
		<dc:creator>Jon</dc:creator>
		<pubDate>Mon, 10 Aug 2009 05:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22086</guid>
		<description>&lt;a href=&quot;#comment-22080&quot; rel=&quot;nofollow&quot;&gt;@Wesley &lt;/a&gt; 

Your fix isn&#039;t quite right:
if (fgets(buffer, 100, stdin)) {

fgets returns buffer on success, so your logic is backwards.  Also, the EOF case isn&#039;t an error, but normal operation.  Something like:

if (!fgets(buf, 100, stdin))
{
   if (feof(stdin))
      length = finished = 0;
   else
      error = ... blah;
}
else
   length = strlen(buf);</description>
		<content:encoded><![CDATA[<p><a href="#comment-22080" rel="nofollow">@Wesley </a> </p>
<p>Your fix isn&#8217;t quite right:<br />
if (fgets(buffer, 100, stdin)) {</p>
<p>fgets returns buffer on success, so your logic is backwards.  Also, the EOF case isn&#8217;t an error, but normal operation.  Something like:</p>
<p>if (!fgets(buf, 100, stdin))<br />
{<br />
   if (feof(stdin))<br />
      length = finished = 0;<br />
   else<br />
      error = &#8230; blah;<br />
}<br />
else<br />
   length = strlen(buf);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wesley</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22081</link>
		<dc:creator>Wesley</dc:creator>
		<pubDate>Sun, 09 Aug 2009 19:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22081</guid>
		<description>&lt;a href=&quot;#comment-22079&quot; rel=&quot;nofollow&quot;&gt;@Justin &lt;/a&gt; why is that bad? realloc behaves just like malloc when the pointer to the memory block is NULL according to &lt;a href=&quot;http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/&quot; rel=&quot;nofollow&quot;&gt;this link&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p><a href="#comment-22079" rel="nofollow">@Justin </a> why is that bad? realloc behaves just like malloc when the pointer to the memory block is NULL according to <a href="http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/" rel="nofollow">this link</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wesley</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22080</link>
		<dc:creator>Wesley</dc:creator>
		<pubDate>Sun, 09 Aug 2009 19:43:06 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22080</guid>
		<description>&lt;a href=&quot;#comment-22078&quot; rel=&quot;nofollow&quot;&gt;@Jon &lt;/a&gt; Okay. I modified the example code again. I hope that it&#039;s less error-prone now. Thanks for your tips.</description>
		<content:encoded><![CDATA[<p><a href="#comment-22078" rel="nofollow">@Jon </a> Okay. I modified the example code again. I hope that it&#8217;s less error-prone now. Thanks for your tips.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Justin</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22079</link>
		<dc:creator>Justin</dc:creator>
		<pubDate>Sun, 09 Aug 2009 18:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22079</guid>
		<description>Trace through the execution of that function in the case where the user enters a string &lt; 100 chars.  You call realloc even when you don&#039;t need to.

Then you do

            temp = (char *)realloc(out, count * 99 + length + 1);

where count is 0 so that becomes

            temp = (char *)realloc(out, 0 * 99 + length + 1);

so you are calling

            temp = (char *)realloc(out, length + 1);</description>
		<content:encoded><![CDATA[<p>Trace through the execution of that function in the case where the user enters a string &lt; 100 chars.  You call realloc even when you don&#039;t need to.</p>
<p>Then you do</p>
<p>            temp = (char *)realloc(out, count * 99 + length + 1);</p>
<p>where count is 0 so that becomes</p>
<p>            temp = (char *)realloc(out, 0 * 99 + length + 1);</p>
<p>so you are calling</p>
<p>            temp = (char *)realloc(out, length + 1);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22078</link>
		<dc:creator>Jon</dc:creator>
		<pubDate>Sun, 09 Aug 2009 18:03:26 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22078</guid>
		<description>Check the return value of fgets!  It will return NULL on an EOF read (which happens all the time... user types and then hits ctrl-D, or input is piped to you).  In your example, you&#039;ll continue the loop, using the last contents of buffer.  Over and over and over, with no end.

Check the return value of getLine!  Your docstring indicates it can return NULL, but then you go ahead and try to use it without checking for that.

Beginning C coders do this sort of thing all the time.  It is a very fast, very powerful language, and consequently it will allow you to load the gun and blow your foot right off.

Side note: you can gain all sorts of little speed-ups by thinking like a computer:
  buffer[length - 1] = &#039;&#039;;
  --length;
becomes:
  buffer[--length] = &#039;&#039;;</description>
		<content:encoded><![CDATA[<p>Check the return value of fgets!  It will return NULL on an EOF read (which happens all the time&#8230; user types and then hits ctrl-D, or input is piped to you).  In your example, you&#8217;ll continue the loop, using the last contents of buffer.  Over and over and over, with no end.</p>
<p>Check the return value of getLine!  Your docstring indicates it can return NULL, but then you go ahead and try to use it without checking for that.</p>
<p>Beginning C coders do this sort of thing all the time.  It is a very fast, very powerful language, and consequently it will allow you to load the gun and blow your foot right off.</p>
<p>Side note: you can gain all sorts of little speed-ups by thinking like a computer:<br />
  buffer[length - 1] = &#8221;;<br />
  &#8211;length;<br />
becomes:<br />
  buffer[--length] = &#8221;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wesley</title>
		<link>http://wesley.vidiqatch.org/code-snippets/safe-replacement-for-gets/comment-page-1/#comment-22077</link>
		<dc:creator>Wesley</dc:creator>
		<pubDate>Sun, 09 Aug 2009 17:28:56 +0000</pubDate>
		<guid isPermaLink="false">http://wesley.vidiqatch.org/?page_id=448#comment-22077</guid>
		<description>&lt;a href=&quot;#comment-22075&quot; rel=&quot;nofollow&quot;&gt;@vart &lt;/a&gt; You are absolutely right of course. I have modified the example code slightly so that it shouldn&#039;t crash when realloc fails. Thanks!</description>
		<content:encoded><![CDATA[<p><a href="#comment-22075" rel="nofollow">@vart </a> You are absolutely right of course. I have modified the example code slightly so that it shouldn&#8217;t crash when realloc fails. Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

